1、發佈環境
發佈腳本node
#!/bin/bash #node group1="172.16.8.11 172.16.8.12" group2="172.16.8.13 172.16.8.14" #deloy tomcat1_dir=/app/tomcat1 tomcat2_dir=/app/tomcat2 tomcat3_dir=/app/tomcat3 tomcat4_dir=/app/tomcat4 #BACKUP backup_dir1=/app/backup1 backup_dir2=/app/backup2 backup_dir3=/app/backup3 backup_dir4=/app/backup4 TMP_DIR1=/app/deploy1 TMP_DIR2=/app/deploy2 TMP_DIR3=/app/deploy3 TMP_DIR4=/app/deploy4 CTIME=$(date +"%H-%M-%S") DATE=$(date "+%Y-%m-%d") SHELL_NAME="deploy.sh" SHELL_LOG="/app/${SHELL_NAME}.log" DATE_N="$(date +'%Y%m%d%H%M%S')" USER_N=`whoami` echo " ${DATE_N} ${USER_N} execute $0 [INFO] $@" >>${SHELL_LOG} writelog(){ LOGINFO=$1 echo "${CDATE} ${CTIME} : ${SEHLL_NAME} : ${LOGINFO}" >> ${SHELL_LOG} } ghsmp_deploy_group1 (){ #echo " ${DATE_N} ${USER_N} execute $0 [INFO] $@" >>${SHELL_LOG} DEPLOY=$1 tomcat_dir=$2 if [ $DEPLOY = "deploy1" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR1 for node in $group1;do if [ $tomcat_dir = "$tomcat1_dir" ];then ssh $node "/app/tomcat1/bin/shutdown.sh" ssh $node mkdir $backup_dir1/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir1/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR1}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat1/bin/startup.sh" else echo "${node} TOMCA××× DEPLOY Failed!" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy2" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR2 for node in $group1;do if [ $tomcat_dir = "$tomcat2_dir" ];then ssh $node "/app/tomcat2/bin/shutdown.sh" ssh $node mkdir $backup_dir2/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir2/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR2}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat2/bin/startup.sh" else echo "${node} TOMCAT2 DEPLOY Failed!" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy3" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR3 for node in $group1;do if [ $tomcat_dir = "$tomcat3_dir" ];then ssh $node "/app/tomcat3/bin/shutdown.sh" ssh $node mkdir $backup_dir3/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir3/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR3}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat3/bin/startup.sh" else echo "${node} TOMCAT3 DEPLOY Failed!" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy4" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR4 for node in $group1;do if [ $tomcat_dir = "$tomcat4_dir" ];then ssh $node "/app/tomcat4/bin/shutdown.sh" ssh $node mkdir $backup_dir4/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir4/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR4}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat4/bin/startup.sh" else echo "${node} TOMCA××× DEPLOY Failed!" cat TOMCAT4_DEPLOY_Failed? fi done else echo "DEPLOY ERROR" fi } ghsmp_deploy_group2 (){ DEPLOY=$1 tomcat_dir=$2 if [ $DEPLOY = "deploy1" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR1 for node in $group2;do if [ $tomcat_dir = "$tomcat1_dir" ];then ssh $node "/app/tomcat1/bin/shutdown.sh" ssh $node mkdir $backup_dir1/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir1/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR1}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat1/bin/startup.sh" else echo "TOMCA××× DEPLOY ERROR?" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy2" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR2 for node in $group2;do if [ $tomcat_dir = "$tomcat2_dir" ];then ssh $node "/app/tomcat2/bin/shutdown.sh" ssh $node mkdir $backup_dir2/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir2/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR2}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat2/bin/startup.sh" else echo "TOMCAT2 DEPLOY ERROR?" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy3" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR3 for node in $group2;do if [ $tomcat_dir = "$tomcat3_dir" ];then ssh $node "/app/tomcat3/bin/shutdown.sh" ssh $node mkdir $backup_dir3/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir3/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR3}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/ ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat3/bin/startup.sh" else echo "TOMCAT3 DEPLOY ERROR?" cat TOMCAT4_DEPLOY_Failed? fi done elif [ $DEPLOY = "deploy4" ]; then scp -r root@172.16.8.67:/app/$DEPLOY/* $TMP_DIR4 for node in $group2;do if [ $tomcat_dir = "$tomcat4_dir" ];then ssh $node "/app/tomcat4/bin/shutdown.sh" ssh $node mkdir $backup_dir4/$DATE ssh $node mv $tomcat_dir/webapps/* $backup_dir4/$DATE ssh $node rm -rf $tomcat_dir/webapps/* scp -r ${TMP_DIR4}/* $node:$tomcat_dir/webapps ssh $node unzip $tomcat_dir/webapps/*.zip -d $tomcat_dir/webapps/ ssh $node "cp -rf $tomcat_dir/webapps/config/* $tomcat_dir/webapps/ROOT/WEB-INF/classes/" ssh $node rm -rf $tomcat_dir/webapps/*.zip ssh $node "/app/tomcat4/bin/startup.sh" echo "${node} deploy success!" else echo "${node} TOMCAT4 DEPLOY ERROR!" cat TOMCAT4_DEPLOY_Failed? fi done else echo "DEPLOY ERROR" fi } rollback_fun(){ for node in $ROLLBACK_LIST;do ssh $node "rm -f /webroot/web-demo && ln -s /opt/webroot/$1 /webroot/web-demo" echo "${node} rollback success!" done } main(){ case $1 in deploy_group1) ghsmp_deploy_group1 $2 $3; ;; deploy_group2) ghsmp_deploy_group2 $2 $3; ;; *) echo $"Usage: $0 {deploy_group1|deploy_group2|ghsmp_rollback}" esac } main $1 $2 $3
2、Jenkins配置
建立一個Pipeline項目
參數化構建—>選擇主機組
參數化構建—>選擇發佈項目
參數化構建—>選擇發佈項目的目錄
使用Pipeline方式構建
開始測試發佈
測試結果以下
web
BlueOcean
使用 BlueOcean 這種方式來完成此處 CI/CD 的工做,BlueOcean 是 Jenkins 團隊從用戶體驗角度出發,專爲 Jenkins Pipeline 從新設計的一套 UI 界面,仍然兼容之前的 fressstyle 類型的 job,BlueOcean 具備如下的一些特性:
連續交付(CD)Pipeline 的複雜可視化,容許快速直觀的瞭解 Pipeline 的狀態
能夠經過 Pipeline 編輯器直觀的建立 Pipeline
須要干預或者出現問題時快速定位,BlueOcean 顯示了 Pipeline 須要注意的地方,便於異常處理和提升生產力
用於分支和拉取請求的本地集成能夠在 GitHub 或者 Bitbucket 中與其餘人進行代碼協做時最大限度提升開發人員的生產力。
tomcat