正文
shell的运用 : jenkins 编译 打包前端发布 生产(tomcat)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
生产隔离做得非常....。文件上传只能通过固定ip机器的sftp账户上传,账户密码每个月要写申请才能获得。
登陆生产服务只能通过浏览器登陆!!!
发布一次生产,很痛苦。
做了简单的shell来减轻痛苦。
思路:
Jenkins--->编译打包--->shell脚本上传到sftp时带上项目名称和时间戳--->浏览器上登陆到生产服务器--->执行shell脚本发布。
第一步,配置Jenkins
两个关键脚本:
sh /data/jenkins_home/sftp_mput.sh ${SFTP_NAME} dist.tar.gz
Jenkins编译打包后看控制台输出,根据提示进行发布:
登陆到服务器
按照Jenkins提示执行命令既可以发布生产了。
第二个脚本:saas_web.sh
#/bin/sh
SFTP_NAMR="$1"
FILE="dist.tar.gz"
TOMCAT_DIR="/apache-tomcat-8.5.34-saas"
BACKUP_DIR=/web-bak/${SFTP_NAMR}
#
${SFTP_NAMR##*Home-}获取项目名称,取Home-后面的字符串
WEB_DIR=${TOMCAT_DIR}/webapps/${SFTP_NAMR##*Home-}
if [ $# == ];then
echo "usage: sh saas_web.sh 项目名称"
exit
else
#停服务
source /etc/profile
sh ${TOMCAT_DIR}/bin/shutdown.sh
/usr/sbin/lsof -i: -t | xargs kill -#备份
mkdir ${BACKUP_DIR}
mv ${WEB_DIR}/* ${BACKUP_DIR} #进入工作目录
cd ${WEB_DIR}
#下载代码包
lftp -u 账户,密码 sftp://IP << EOF
cd ${SFTP_NAMR}/
mget ${FILE:-"*"}
bye
EOF #解压包,前端代码打包,可能是包含目录,所以解压时需要进行判断
# awk 是获取解压后以/为分隔,第一列是否一样,若一样则n会等于1,1就判断其是有目录
n=`tar ztf dist.tar.gz |awk -F / '{a[$1]++} END{for(i in a){print i}} '|wc -l`
#若有目录,则解压后mv 目录/* ./;若没有目录则只需要直接解压
if [ "$n" -eq "1" ];then
filename=`tar ztf dist.tar.gz |awk -F / '{a[$1]++} END{for(i in a){print i}} '`
tar zxvf dist.tar.gz
mv $filename/* ./
else
tar zxvf dist.tar.gz
fi
#启动服务
sh ${TOMCAT_DIR}/bin/startup.sh
fi