该博文实现效果:结合maven+gitlab,可以使用Jenkins对不同环境(测试及线上环境)的tomcat服务器实现版本的迭代更新及版本回滚操作,部署完成后,只需点击几下,即可实现。

一、环境准备

系统 IP 主机名 运行服务
Centos7.3 192.168.171.131 Jenkins Jenkins+gitlab+Maven
Centos7.3 192.168.171.134 Tomcat1 Tomcat
Centos7.3 192.168.171.135 Tomcat2 Tomcat

Jenkins、gitlab服务部署可参考:部署Jenkins+Gitlab实现持续集成
Tomcat1用于测试环境,Tomcat2用于生产环境,部署可参考:Tomcat 的安装与优化
在进行真正的配置前,优先确保可以访问到以下几个页面:
1、gitlab

二、部署及配置
1、Jenkins服务器上安装JDK环境

[root@jenkins ~]# rpm -qa | grep jdk
copy-jdk-configs-1.2-1.el7.noarch
java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
#卸载下面的两个包
[root@jenkins ~]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
[root@jenkins ~]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
#部署我提供的jdk包
[root@jenkins ~]# tar zxf jdk-8u211-linux-x64.tar.gz -C /usr/local/
[root@jenkins ~]# vim /etc/profile
export  JAVA_HOME=/usr/local/jdk1.8.0_211
export  JRE_HOME=/usr/local/jdk1.8.0_211/jre
export  CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export  PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@jenkins ~]# . /etc/profile
[root@jenkins ~]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

2、部署Maven服务

[root@jenkins ~]# tar zxf apache-maven-3.6.1-bin.tar.gz 
[root@jenkins ~]# mv apache-maven-3.6.1/ /usr/local/maven
[root@jenkins ~]# vim /etc/profile
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:$MAVEN_HOME/bin
[root@jenkins ~]# . /etc/profile
[root@jenkins ~]# mvn -v               #执行命令,只要出现下面内容就部署成功了(因为这是二进制的方式部署)
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T03:00:29+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_211/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.el7.x86_64", arch: "amd64", family: "unix"

3、指定阿里云仓库

[root@jenkins ~]# vim /usr/local/maven/conf/settings.xml
#在158行下写入以下内容
<mirror>
      <id>aliyun</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven</name>
      <url>http://www.zww55.com/980/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>       #添加在该行上面

4、解决Jenkins无法启动问题
至此,maven服务已经部署成功了,但是,这里还同时部署着Jenkins,Jenkins的启动是依赖之前的Java环境的,部署maven服务又重新配置了Java环境,所以,Jenkins当前无法正常启动,现在需要对Java命令做软连接

[root@jenkins ~]# ln -sf /usr/local/jdk1.8.0_211/bin/java /usr/bin/
[root@jenkins ~]# /etc/init.d/jenkins restart         #重启Jenkins,确定可以启动成功
Restarting jenkins (via systemctl):                        [  OK  ]

5、maven构建测试代码并上传至gitlab

[root@jenkins ~]# mvn archetype:generate -DgroupId=cn.test.testweb -DartifactId=testweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[root@jenkins ~]# cd testweb/
[root@jenkins testweb]# vim src/main/webapp/index.jsp 
<html>
<body>
<h2>Hello World! test1 web</h2>  #更改此行,以便区分不同版本
</body>
</html>
#删除本地gitlab库之前的文件,并提交到gitlab库
[root@jenkins test1]# rm -rf *
[root@jenkins test1]# git commit -m "del"
[root@jenkins test1]# git push origin master
#将maven构建的项目复制到此目录下,并提交到gitlab远端库
[root@jenkins test1]# cd ~/testweb/
[root@jenkins testweb]# cp -r pom.xml src/ ~/test1/
[root@jenkins testweb]# cd ~/test1/
[root@jenkins test1]# git add *
[root@jenkins test1]# git commit -m "test1 web"
[root@jenkins test1]# git push origin master 

6、配置Jenkins免密登录tomcat服务器

#由于需要Jenkins向Tomcat服务器复制代码及添加目录,所以需要配置免密登录
#默认在配置gitlab时,已经生成了当前用户的秘钥对,所以无需重新生成
#将公钥分别发送至两台tomcat服务器,自行输入“yes”确认,及tomcat的root密码
[root@jenkins test1]# ssh-copy-id root@192.168.171.135
[root@jenkins test1]# ssh-copy-id root@192.168.171.134

7、修改Jenkins的运行用户
同样,也是为了方便Jenkins服务在运行过程中,拥有一些创建目录或写入文件的权限,我这里为了方便,直接改为root用户运行Jenkins,生产环境中,需要好好考虑权限问题

[root@jenkins test1]# sed -i 's/JENKINS_USER="jenkins"/JENKINS_USER="root"/g' /etc/sysconfig/jenkins
[root@jenkins test1]# /etc/init.d/jenkins restart 

8、配置Jenkins的web界面安装插件

按照下面的提示,依次安装插件提取码:08n5 (可以自行使用在线安装插件的方式,自行研究),共7个,除了要先安装jquery插件再安装jquery-ui插件外,其他插件的上传顺序可随意,注意:一次只能上传一个插件


以下脚本可以直接复制下面内容(注意,需要根据实际情况修改下面定义host的IP字段,建议仔细阅读脚本,以便查看这个脚本的作用)
这个脚本是实现版本的迭代升级及回滚的关键!!!
这个脚本是实现版本的迭代升级及回滚的关键!!!
这个脚本是实现版本的迭代升级及回滚的关键!!!

##Deploy tomcat 

war_bak="/data/war/bak"
tomcat_deploy="/usr/local/tomcat/webapps"
#WAR_PATH="${WORKSPACE}/${MODULE_NAME}/target/*.war"
WAR_PATH="${WORKSPACE}/target/*.war"
#以下是定义测试服务器及线上服务器
test_host="192.168.171.134"    #测试服务器,IP可以写多个,但是要注意格式
stag_host="192.168.171.135"    #线上服务器,同样,IP可以写多个
port="8080"

echo "构建环境:${deploy} 项目名称:${JOB_NAME} 构建时间:`date +%F` 本次上线版本:${GIT_COMMIT}" >> /var/log/${JOB_NAME}.log

### status deploy or rollback

##判断git是否为空,如果是为空进行提示
if [ "${git}" = "" ];then

echo "请输入git版本 #############"
exit 1

else

## 判断发布 or 回滚
if [ "${Status}" = "Deploy" ];then

### 判断是否为测试环境
    if [ "${deploy}" = "test" ];then
            ### 构建主机
               for i in ${test_host}
                 do
               ssh ${i} "mkdir -p ${war_bak}/${JOB_NAME}/${git}"
                 scp ${WAR_PATH} ${i}:${war_bak}/${JOB_NAME}/${git}/ROOT.war
                 ssh ${i} rm -rf ${tomcat_deploy}/*
                 ssh ${i} cp ${war_bak}/${JOB_NAME}/${git}/ROOT.war ${tomcat_deploy}
                 ssh ${i} /etc/init.d/tomcat  restart
          ### 判断tomcat是否正常      
                     for http in `seq 1 5`
                   do
                        tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
                    if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
                        echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
                              sleep 5
                      else
                        echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
                              break
                      fi
                  done

                      if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
                          if [[ "${tomcat_status}" = "" ]];then
                      echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
                      exit 1
                          fi
                        echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
                    fi
              done
                echo -e "\033[5;34m 本次构建${test_host}主机,本次环境 ${deploy} \033[0m"
## 判断为预发布环境
    elif [ "${deploy}" = "stag" ];then
               for i in "${stag_host}"
                 do
               ssh ${i} mkdir -p ${war_bak}/${JOB_NAME}/${git}
                 scp ${WAR_PATH} ${i}:${war_bak}/${JOB_NAME}/${git}/ROOT.war
                 ssh ${i} rm -rf ${tomcat_deploy}/*
                 ssh ${i} cp ${war_bak}/${JOB_NAME}/${git}/ROOT.war ${tomcat_deploy}
                 ssh ${i} /etc/init.d/tomcat restart
          ### 判断tomcat是否正常      
                     for http in `seq 1 5`
                   do
                        tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
                    if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
                        echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
                              sleep 5
                      else
                        echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
                              break
                      fi
                  done

                      if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
                          if [[ "${tomcat_status}" = "" ]];then
                      echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
                      exit 1
                        fi
                        echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
                    fi
        done
               echo -e "\033[5;34m 本次构建${test_host}主机,本次环境 ${deploy} \033[0m"
   fi

### 回滚操作
elif [[ "${Status}" = "RollBack" ]];then

  ### 判断回滚环境及主机
            if [ "${deploy}" = "test" ];then
              for i in ${test_host}
              do
                 ssh ${i}  "[ -d ${war_bak}/${JOB_NAME}/${git} ]"
                      if [ $? -ne '0' ];then
                            echo -e "\033[5;34m  git commit 回滚目录不存在,环境${deploy} 错误主机${i} \033[0m"
                            exit 3
                        else
                            echo -e "\033[5;34m  准备回滚操作  本次回滚环境${deploy} 回滚主机${i} \033[0m"
                            sleep 3
                      fi
                 ssh ${i}  "mkdir -p ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "cp -r ${tomcat_deploy}/* ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "rm -rf ${tomcat_deploy}/*"
                 ssh ${i} "cp -r ${war_bak}/${JOB_NAME}/${git}/*.war ${tomcat_deploy}/"
                 ssh ${i} /etc/init.d/tomcat  restart
                   ### 判断tomcat是否正常      
              for http in `seq 1 5`
              do
                 tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
                 if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
                        echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
                        sleep 5
                 else
                        echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
                        break
                 fi
              done

                  if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
                    if [[ "${tomcat_status}" = "" ]];then
                      echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
                      exit 1
                    fi
                      echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
                 fi
              done

            elif [ "${deploy}" = "stag" ];then
              for i in ${stag_host}
              do
                 ssh ${i}  "[ -d ${war_bak}/${JOB_NAME}/${git} ]"
                      if [ $? -ne '0' ];then
                            echo -e "\033[5;34m  git commit 回滚目录不存在,环境${deploy} 错误主机${i} \033[0m"
                            exit 3
                        else
                            echo -e "\033[5;34m  准备回滚操作  本次回滚环境${deploy} 回滚主机${i} \033[0m"
                            sleep 3
                      fi
                 ssh ${i}  "mkdir -p ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "cp -r ${tomcat_deploy}/* ${war_bak}/${JOB_NAME}/${git}_${Status}_rollback/"
                 ssh ${i}  "rm -rf ${tomcat_deploy}/*"
                 ssh ${i} "cp -r ${war_bak}/${JOB_NAME}/${git}/*.war ${tomcat_deploy}/"
                 ssh ${i} /etc/init.d/tomcat  restart
                   ### 判断tomcat是否正常      
              for http in `seq 1 5`
              do
                 tomcat_status=`curl -I ${i}:${port} -s|awk -F "[ ]" '{print $2}' |sed -n '1p'`
                 if  [[ "$tomcat_status" -ne 200 ]] || [[ "$tomcat_status" = "" ]];then
                        echo -e "\033[5;34m 请稍等,服务启动中........ \033[0m"
                        sleep 5
                 else
                        echo -e "\033[5;34m 构建 ${i}环境发布正常,返回值[${tomcat_status}] \033[0m"
                        break
                 fi
              done

                  if [[ "${tomcat_status}" -ne 200 ]] || [[ "${tomcat_status}" = "" ]];then
                    if [[ "${tomcat_status}" = "" ]];then
                      echo -e "\033[5;34m 构建 ${i}服务启动异常 \033[0m"
                      exit 1
                    fi
                      echo -e "\033[5;34m 构建 ${i}环境发布异常,返回值[${tomcat_status}] \033[0m"
                 fi
              done
            ### 判断测试环境fi结束
            fi

fi

#### fi是判断是否有git地址的结束
fi


11、编写tomcat服务的启动脚本,11选5走势图:两台服务器都需要进行以下配置
如果不编写tomcat的启动脚本,则在上面的shell脚本需要更改很多地方,因为上面脚本指定的tomcat启动脚本是/etc/init.d/tomcat
tomcat1配置如下

[root@tomcat1 ~]# vim /etc/init.d/tomcat
#!/bin/bash
# description: Tomcat is a Java Servlet Container
TOMCAT_HOME=/usr/local/tomcat
start () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
      source   /etc/profile
      /bin/bash $TOMCAT_HOME/bin/startup.sh
else
    echo "$0 is  running"
fi
}
stop () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
        echo "$0 is not running"
else
        echo "shutting down $0"
        kill -9 "$TOMCAT_PID" && echo "PID $TOMCAT_PID killed."
fi
}
status () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
        echo "$0 is not running"
else
        echo "$0 is running PID is $TOMCAT_PID"
fi
}
case $1 in
start)
start
#tail -f $TOMCAT_HOME/logs/catalina.out
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
#tail -f $TOMCAT_HOME/logs/catalina.out
;;
*)
echo "Usage:$0  {start|stop|status|restart}."
;;
esac
[root@tomcat1 ~]# chmod +x /etc/init.d/tomcat             # 赋予执行权限
[root@tomcat1 ~]# /etc/init.d/tomcat restart      # 测试脚本是否可以使用 

注:tomcat2配置相同
注:tomcat2配置相同
注:tomcat2配置相同
12、将gitlab的代码部署到tomcat1测试服务器上
首先需要在gitlab的web界面找到该测试代码的commit ID号,然后复制,如下:

14、对tomcat服务进行页面升级
1)准备测试代码:

#执行mvn命令,生成代码
[root@jenkins /]# mvn archetype:generate -DgroupId=cn.test.testweb -DartifactId=testweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
[root@jenkins /]# vim testweb/src/main/webapp/index.jsp 
<html>
<body>
<h2>Hello World! test2 web</h2>             # 修改为test2
</body>
</html>
[root@jenkins /]# cd testweb/
[root@jenkins testweb]# ls
pom.xml  src
[root@jenkins testweb]# cp -r pom.xml src/ ~/test1/
[root@jenkins testweb]# cd ~/test1/
[root@jenkins test1]# git add *
[root@jenkins test1]# git commit -m "test2 web"
[root@jenkins test1]# git push origin master         # 将代码推送至gitlab

查看gitlab上推送来代码的commit ID号: