原创

使用Shell脚本一键部署项目

环境:

  1.     Amazon服务器
  2.     Docker容器
  3.     SSH
Shell文件作用:项目部署到了Amazon的Ubuntu中的Docker容器中,Docker容器和宿主机之间建立了一个挂载目录/opt/software/upload/xx/,目录中放待部署的程序代码,上传完代码后在Docker容器中执行sh /bin/xxx.sh即可实现自动部署后查看项目日志

#! / bin/sh

# Author : zhyd
# Script follows here:

# 声明变量:Mount the directory挂载目录,此处放置待部署的程序代码压缩文件
mountTheDirectory="/opt/software/upload/xx/xx.zip"
# Web site root directory 网站根目录
webSiteRootDirectory="/var/www/xx/"
# tomcat地址
tomcatRootPath="/xx/x/tomcat7"
# 输出当前日期
nowDate=$(date +%c)
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "当前系统时间:${nowDate}"
echo "待部署文件地址:${mountTheDirectory}"
echo "网站根目录地址:${webSiteRootDirectory}"
echo "tomcat地址:${tomcatRootPath}"
echo "确定上方信息正确?确认继续执行[y/n]"
read CONFIRM
# 判断字符串。[-o 或 ,-a 与,! 非]
if [ $CONFIRM = "y" -o $CONFIRM = "Y" ];then
	# [ -f FILE ]  如果 FILE 存在且是一个普通文件则为真
	if [ -f $mountTheDirectory ]; then
		echo "文件存在!开始部署"
		# 将挂载目录中待部署的程序压缩包移动到网站根目录下
		mv ${mountTheDirectory} ${webSiteRootDirectory}
		# 解压程序包
		unzip -d ${webSiteRootDirectory} -o ${webSiteRootDirectory}xx.zip
		# 关闭tomcat
		echo "停止tomcat..."
		# 用 ` 号将需要获取输出的操作括起来,并赋值给一个变量,则 Shell 会在等待命令执行完成后
		# 把输出内容用于赋值。实现等待命令执行完成的效果
		output=`sh ${tomcatRootPath}/bin/shutdown.sh`
		echo $output
		#启动tomcat
		echo "启动tomcat..."
		output=`sh ${tomcatRootPath}/bin/startup.sh`
		echo $output
		# 查看tomcat日志
		tail -f ${tomcatRootPath}/logs/catalina.out
	else
		echo "文件不存在:${mountTheDirectory}"
		echo "exit!"
	fi
else
	echo "exit!"
fi
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"


正文到此结束