Jenkins CICD代码构建发布流程

一、发布环境

发布脚本

#!/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] [email protected]" >>${SHELL_LOG}

writelog(){
    LOGINFO=$1
    echo "${CDATE} ${CTIME} : ${SEHLL_NAME} : ${LOGINFO}" >> ${SHELL_LOG}
}
ghsmp_deploy_group1 (){
      #echo " ${DATE_N} ${USER_N} execute $0 [INFO] [email protected]" >>${SHELL_LOG}
      DEPLOY=$1
      tomcat_dir=$2
      if [ $DEPLOY = "deploy1" ]; then
         scp  -r [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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 [email protected]:/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

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

时间: 2024-10-30 08:15:22

Jenkins CICD代码构建发布流程的相关文章

使用Jenkins Pipeline自动化构建发布Java项目

简介 Pipeline,简而言之,就是一套运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. Pipeline是Jenkins2.X的最核心的特性,帮助Jenkins实现从CI到CD与DevOps的转变. 一,创建pipeline项目 二,清除部分历史构建 三,参数化构建 这里使用三个参数,分别对应是否拉取代码,项目名称,以及发版选项 四,编写pipeline脚本 选择pipeline script编写pipeline

基于Gitlab+Jenkins的代码自动化发布

这里所讲的自动化发布是指代码从提交到仓库,到发布到目标服务器的整个过程. 主要涉及到两个工具Gitlab,Jenkins,要完成自动化还需要rsync,qqbot,log,ant.shell脚本,python等. Gitlab:我们主要用它来做代码的仓库 Jenkins:用来执行任务的持续集成,构建等.一.大体的自动化思路: 开发人员push代码到gitlab,触发webhook,调用jenkins job. jenkins job 执行拉取代码,编译,调用loadblance,下架部分服务器更

Solo博客系统--Jenkins/docker自动化构建发布系统

部署 git服务器 yum install git useradd git passwd git 创建仓库 su - git mkdir solo.git git --bare init ##初始化仓库 docker cat >> /etc/docker/daemon.json << EOF { "insecure-registries":["reg.hequan.xyz:5000"] } EOF Jenkins服务器 wget https:

jenkins pipeline 复杂的发布流程

1.参数化构建界面 2.交付流水线界面 3.脚本详解 #!groovy pipeline { //在任何可用的代理上执行Pipeline agent any //参数化变量,目前只支持[booleanParam, choice, credentials, file, text, password, run, string]这几种参数类型,其他高级参数化类型还需等待社区支持. parameters { //git代码路径[参数值对外隐藏] string(name:'repoUrl', defaul

持续集成学习7 jenkins自动化代码构建

一.整体功能 1.触发上下游构建 2.我们在触发一个job的时候顺便丢一些参数过去,这些参数有可能是我这次编译过程中产生的一些地址,版本号或动态的一些东西丢到下游作为下游的构建参数 3.如何查看上下游构建视图 原文地址:https://www.cnblogs.com/Presley-lpc/p/11806092.html

运维技巧:简单4步完成自动化构建发布

各位看官,这不是一个揭发单身有为青年因同事们天天秀恩爱而受到一万点暴击伤害的故事.这里指的狗粮,不是真正的"狗粮"--当然,也不是你们认为的狗粮. 事实上,现在很多涉足产品开发的互联网公司,都会提到"吃狗粮"这一概念(出自"Eating yourown dog food -- 吃你自家的狗粮"),它的意思是公司内部员工使用自己生产的产品进行日常工作.这么做有什么好处呢,比方说一家公司做美颜APP的,结果他们自己员工却用某图秀秀P图,这产品对外怎么

gitlab+jenkins+hook代码自动构建发布上线

Gitlab+Jenkins+Hook 1.gitlab和jenkins的安装见: http://www.cnblogs.com/cuishuai/p/7544663.html http://www.cnblogs.com/cuishuai/p/7544775.html 2.gitlab配置 1)创建一个project,并创建一个monkey的分支. 2)对分支进行设置: 点击project->settings->integrations: 1. 2. 3 Webhook,点击test,返回如

一步一步用jenkins,ansible,supervisor打造一个web构建发布系统

新blog地址:http://hengyunabc.github.io/deploy-system-build-with-jenkins-ansible-supervisor/ 一步一步用jenkins,ansible,supervisor打造一个web构建发布系统. 本来应该还有gitlab这一环节的,但是感觉加上,内容会增加很多.所以直接用github上的spring-mvc-showcase项目来做演示. https://github.com/spring-projects/spring-

Jenkins环境搭建部署实现代码自动化发布

一.jenkins代码自动部署 1.配置免密钥通信 实现自动化部署首先要解决的是免密码传输,配置jenkins至测试服务器之间免密钥ssh登录 测试免密钥ssh登录 在测试服务器上编写一个测试脚本,检测是否可以执行成功,正式环境可以写一个自动化部署的脚本 2.jenkins新建部署代码项目 在构建这里选择执行shell命令 点击立即构建 控制台输出日志:成功 这样就实现了使用jenkins代码的自动化部署 实际情况中我们通常使用版本控制系统管理代码,svn 或者 git 二.gitlab利用we