码迷,mamicode.com
首页 > 其他好文 > 详细

Jenkins CICD代码构建发布流程

时间:2018-09-30 12:47:53      阅读:653      评论:0      收藏:0      [点我收藏+]

标签:ges   pre   node   name   $@   技术分享   success   发布脚本   col   

一、发布环境
技术分享图片
发布脚本

#!/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

Jenkins CICD代码构建发布流程

标签:ges   pre   node   name   $@   技术分享   success   发布脚本   col   

原文地址:http://blog.51cto.com/10880347/2287850

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!