1.脚本发布
发布服务器:山田master
脚本路径:/server/tools
Master脚本结构:
master备份文件在:/server/tools/backup
backup备份都在文件在:/server/backup
脚本使用方法:
[root@iZ6weirh18lb3sov6mfxftZ tools]# sh release.sh
Usage: release.sh [ deploy | rollback ] 目前可选择deokiy
[root@iZ6weirh18lb3sov6mfxftZ tools]# sh release.sh deploy 该脚本存在加锁限制 只可同时执行一个任务
[root@iZ6weirh18lb3sov6mfxftZ tools] # cat release.sh #!/bin/bash #v1 # Date/Time Veriables LOG_DATE= 'date "+%Y-%m-%d"' LOG_TIME= 'date "+%H-%M-%S"' CDATE=$( date "+%Y-%m-%d" ) CTIME=$( date "+%H-%M-%S" ) NODE_LIST= "8.209.252.22 47.91.31.1" #Shell Env SHELL_NAME= "release.sh" SHELL_DIR= "/server/tools" SHELL_LOG= "${SHELL_DIR}/${SHELL_NAME}.log" app_groups= "/server/tools/app" LOGS= "/server/tools/logs/${SHELL_NAME}.log" #Code Enb index= "/usr/share/nginx/html/build" app_jar= "/usr/local/esl/app" start_app= "/usr/local/esl" LOCK_FILE= "/tmp/release.lock" usage(){ echo $ "Usage: $0 [ deploy | rollback ]" } writelog(){ LOGINFO=$1 echo "${CDATE} ${CTIME}: ${SHELL_NAME} : ${LOGINFO}" >> ${LOGS} } shell_lock(){ touch ${LOCK_FILE} } shell_unlock(){ rm -f ${LOCK_FILE} } rollback(){ echo rollback } code_get(){ writelog "Front and back end backup of master" ; echo Front and back end backup of master BACK= "/server/tools/backup/html" cd $index tar zcf dist-$CDATE. tar .gz dist && mv dist-$CDATE. tar .gz $BACK Jar_Back= "/server/tools/backup/jar" mv $app_jar /zk_esl_release .jar $Jar_Back /zk_esl_release $CDATE.jar } release_push(){ writelog "Master front and back end Publishing" ; echo "Master front and back end Publishing " #rm -rf $app_jar/zk_esl_release.jar cp $app_groups /zk_esl_release .jar $app_jar #echo "Master发布前端" cd $index pwd && rm -rf dist tar xf $app_groups /dist . tar .gz -C $index } code_scp(){ writelog "Start synchronously from the server jar package" ; ansible app -m copy -a "src=/usr/local/esl/app/zk_esl_release.jar dest=/server/backup backup=yes" ansible app -m file -a 'path=/usr/local/esl/app/zk_esl_release.jar state=absent' echo code_scp for node in $NODE_LIST; do scp $app_groups /zk_esl_release .jar $node: /usr/local/esl/app/ done } eslapp_start(){ writelog "master backup01 backup02 Start Application" ansible start -m shell -a 'cd /usr/local/esl/ && docker-compose up -d --build' } main(){ if [ -f ${LOCK_FILE} ]; then echo "Deploy is running" && exit ; fi DEPLOY_METHOO=$1 case $DEPLOY_METHOO in deploy) shell_lock; code_get; release_push; code_scp; eslapp_start; shell_unlock; ;; rollback) rollback; ;; *) usage; esac } main $1 |
2.jenkins 调用脚本发布
通过Jenkins执行前后端job即可