云效自动化部署+部署包备份
背景:公司引入云效一个月,我作为云效的主要宣导者,在此有很多的赞美话想要说,以往在Dev和Qa环境下,大多是开发人员去部署,我自己就是这样过来的,有时不熟很苦恼,很影响工作效率。
期望:这次我们引用云效后,期望能有走正常的CICD流程,由于时间的关系,我现在还没有花蛮多的精力去美化自己的篇幅和言语,就用最粗暴的方式做些说明吧
我们通过拉取云效的一个示例代码,发布到一台有外网的机器,同时通过这台机器把包传递到其他机器,同时在这台机器也会把源包在服务器上备份。
构建命令
#!/bin/bash # 修改APP_NAME为云效上的应用名 APP_NAME=application APP=spring-boot APP_PORT=18888 PROG_NAME=$0 ACTION=$1 APP_START_TIMEOUT=20 # 等待应用启动的时间 HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT} # 应用健康检查URL #HEALTH_CHECK_FILE_DIR=/home/admin/status # 脚本会在这个目录下生成nginx-status文件 APP_HOME=/data/code/${APP} JAR_NAME=${APP_HOME}/${APP_NAME}.jar # jar包的名字 JAVA_OUT=${APP_HOME}/logs/start.log #应用的启动日志 DATA_NOW=`date +%Y%m%d%H%M%S` #当前时间 BAK_DATE=/data/code/bak/date/${DATA_NOW}_${APP} BAK_APP=/data/code/bak/${APP}/${DATA_NOW} BAK_TMP=/data/code/bak/tmp/${DATA_NOW}_${APP} echo "备份当前项目,其中tmp下是新包" mkdir -p ${BAK_TMP} cp -r /data/code/release/${APP}/ ${BAK_TMP}/ mv -f ${BAK_TMP}/${APP}/package.tgz ${BAK_TMP}/${APP}/tmp/package.tgz #调换新旧包 echo "备份文件" mkdir -p ${BAK_DATE} # 创建时间排序目录 cp -r ${BAK_TMP}/${APP} ${BAK_DATE}/ # 备份1 echo "${BAK_TMP}/ ${BAK_DATE}/" mkdir -p ${BAK_APP} # 创建名称排序目录 cp -r ${BAK_TMP}/${APP} ${BAK_APP}/ # 备份2 echo "${BAK_TMP}/ ${BAK_APP}/" echo "创建出相关目录" mkdir -p ${APP_HOME} mkdir -p ${APP_HOME}/logs # mkdir -p ${APP_HOME}/bak mkdir -p /data/code/release/${APP}/tmp echo "拷贝包" cp /data/code/release/${APP}/tmp/package.tgz /data/code/release/${APP}/package.tgz # 拷贝文件 echo "清空临时文件夹" cd /data/code/release/${APP}/tmp/ shopt -s extglob rm -fr !(package.tgz) # 排除package.tgz echo "解压包进行发布" tar zxf /data/code/release/${APP}/package.tgz -C /data/code/release/${APP}/tmp/ # 解压包 echo "删除无用包" rm -fr !(${APP_NAME}.jar|package.tgz) echo "只留一个源包和启动包" cd /data/code/release/${APP}/tmp/ rm -fr !(${APP_NAME}.jar|package.tgz) echo "清空启动位置的文件夹" cd ${APP_HOME} rm -fr !(logs) # 排除logs日志文件夹 echo "拷贝包到启动的位置" cp /data/code/release/${APP}/tmp/${APP_NAME}.jar ${APP_HOME}/ # 移动包到启动位置 ln -sf ${APP_HOME}/${APP_NAME}.jar ${JAR_NAME} echo "文件操作完成,请等待启动..." usage() { echo "Usage: $PROG_NAME {start|stop|restart}" exit 2 } health_check() { exptime=0 echo "checking ${HEALTH_CHECK_URL}" while true do status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code} ${HEALTH_CHECK_URL}` if [ "$?" != "0" ]; then echo -n -e "\rapplication not started" else echo "code is $status_code" echo "判断是否等于200" if [ "$status_code" == "200" ];then break fi fi sleep 1 ((exptime++)) echo -e "\rWait app to pass health check: $exptime..." if [ $exptime -gt ${APP_START_TIMEOUT} ]; then echo 'app start failed' echo '启动失败,请查看日志信息' cat ${APP_HOME}/logs/start.log exit 1 fi done echo "check ${HEALTH_CHECK_URL} success" } start_application() { echo "starting java process" #nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 & echo "nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 &" nohup java -jar ${JAR_NAME} > ${JAVA_OUT} 2>&1 & echo "started java process" } stop_application() { checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'` if [[ ! $checkjavapid ]];then echo -e "\rno java process" return fi echo "stop java process" times=60 for e in $(seq 60) do sleep 1 COSTTIME=$(($times - $e )) checkjavapid=`ps -ef | grep java | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'` if [[ $checkjavapid ]];then kill -9 $checkjavapid echo -e "\r -- stopping java lasts `expr $COSTTIME` seconds." else echo -e "\rjava process has exited" break; fi done echo "" } start() { start_application health_check } stop() { stop_application } case "$ACTION" in start) start ;; stop) stop ;; restart) stop start ;; *) usage ;; esac