项目案例之Pipeline流水线发布JAVA项目(三)

链接:https://pan.baidu.com/s/1NZZbocZuNwtQS0eGkkglXQ
提取码:z7gj
复制这段内容后打开百度网盘手机App,操作更方便哦

6. 项目案例二:流水线自动化发布JAVA项目

主机名 IP地址 备注
Git 192.168.200.61 Git服务器
Jenkins 192.168.200.62 Jenkins服务器
Web01 192.168.200.65 Tomcat服务器
#所有服务器进行如下操作
[root@Git ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@Git ~]# uname -r
3.10.0-862.3.3.el7.x86_64
[root@Git ~]# systemctl stop firewalld
[root@Git ~]# systemctl disable firewalld
[root@Git ~]# systemctl stop NetworkManager
[root@Git ~]# systemctl disable NetworkManager

6.1 Tomcat服务器环境部署

#在tomcat服务器进行如下操作
#部署jdk
[root@Web02 ~]# ls
anaconda-ks.cfg  apache-maven-3.5.0-bin.tar.gz  apache-tomcat-8.5.33.tar.gz  jdk-8u171-linux-x64.tar.gz
[root@Web02 ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/
[root@Web02 ~]# cd /usr/local/
[root@Web02 local]# mv jdk1.8.0_171 jdk
[root@Web02 local]# ln -s /usr/local/jdk/bin/java /usr/local/bin/
[root@Web02 local]# java -version
java version "1.8.0_171"
Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
#部署java环境变量
[root@Web02 local]# vim /etc/profile
[root@Web02 local]# tail -4 /etc/profile
#部署java环境变量
export JAVA_HOME=/usr/local/jdk/
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
[root@Web02 local]# source /etc/profile
#部署maven
[root@Web02 ~]# tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local
[root@Web02 ~]# cd /usr/local/
[root@Web02 local]# mv apache-maven-3.5.0 maven
[root@Web02 local]# ln -s /usr/local/maven/bin/mvn /usr/local/bin/
[root@Web02 local]# mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_171, vendor: Oracle Corporation
Java home: /usr/local/jdk/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.14.4.el7.x86_64", arch: "amd64", family: "unix"

#部署maven环境变量
[root@Web02 local]# vim /etc/profile
[root@Web02 local]# tail -3 /etc/profile
#部署maven环境变量
export MAVEN_HOME=/usr/local/maven
export PATH=$PATH:${MAVEN_HOME}
[root@Web02 local]# source /etc/profile
#解压tomcat
[root@Web02 ~]# tar xf apache-tomcat-8.5.33.tar.gz -C /usr/local/
[root@Web02 ~]# cd /usr/local/
[root@Web02 local]# mv apache-tomcat-8.5.33 tomcat
[root@Web02 local]# cd tomcat/
[root@Web02 tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@Web02 tomcat]# rm -rf webapps/*
[root@Web02 tomcat]# ls webapps/
[root@Web02 tomcat]# 
#部署tomcat环境变量
[root@Web02 tomcat]# vim /etc/profile
[root@Web02 tomcat]# tail -2 /etc/profile
#部署tomcat环境变量
export TOMCAT_HOME=/usr/local/tomcat
[root@Web02 tomcat]# source /etc/profile

#自动部署需要解压maven构建后的war包,因此需要安装unzip
[root@Web02 tomcat]# yum -y install unzip
[root@Web02 tomcat]# rpm -qa unzip
unzip-6.0-19.el7.x86_64

#自动部署需要git远程仓库代码,因此需要安装git
[root@Web02 tomcat]# yum -y install git
[root@Web02 tomcat]# rpm -qa git
git-1.8.3.1-14.el7_5.x86_64

6.2 构建一个B-JAVA流水线项目

image.png-83kB

image.png-38.2kB

image.png-23.5kB

image.png-60.9kB

6.3 下载solo项目源代码并上传Git远程仓库

solo源代码下载地址 https://github.com/b3log/solo

#在Git服务器上创建用于存放solo源代码的仓库
[root@Git ~]# hostname -I
192.168.200.61 
[root@Git ~]# cd /home/git/repos/
[root@Git repos]# mkdir solo.git
[root@Git repos]# cd solo.git/
[root@Git solo.git]# git --bare init
初始化空的 Git 版本库于 /home/git/repos/solo.git/
[root@Git solo.git]# cd ..
[root@Git repos]# chown -R git.git solo.git
#在jenkins服务器上进行如下操作
#解压solo.tar.gz源代码包
[root@Jenkins ~]# tar xf solo.tar.gz
[root@Jenkins ~]# mkdir -p /mycode
[root@Jenkins ~]# cd /mycode/
[root@Jenkins mycode]# git clone git@192.168.200.61:/home/git/repos/solo.git
正克隆到 \'solo\'...
warning: 您似乎克隆了一个空版本库。
[root@Jenkins mycode]# ls
solo
[root@Jenkins mycode]# cd solo/
[root@Jenkins solo]# ls
[root@Jenkins solo]# mv /root/solo/* .
[root@Jenkins solo]# ls
CHANGE_LOGS.html    Dockerfile   LICENSE       pom.xml    README_zh_CN.md
docker-compose.yml  gulpfile.js  package.json  README.md  src
[root@Jenkins solo]# rm -rf /root/solo
#为了最后的测试,我们需要修改源代码的一个配置文件
[root@Jenkins solo]# cd src/main/resources/
[root@Jenkins resources]# ll -d latke.properties 
-rw-r--r-- 1 root root 1241 10月 25 14:29 latke.properties
[root@Jenkins resources]# vim latke.properties 
[root@Jenkins resources]# cat -n latke.properties | sed -n \'29p;31p\'
    29	serverHost=192.168.200.65
    31	serverPort=8080
    

#将solo源代码提交到Git仓库
[root@Jenkins resources]# cd /mycode/solo/
[root@Jenkins solo]# git add *
[root@Jenkins solo]# git commit -m "第一次提交"
[root@Jenkins solo]# git push -u origin master
[root@Jenkins solo]# git push -u origin master
Counting objects: 2099, done.
Compressing objects: 100% (2044/2044), done.
Writing objects: 100% (2099/2099), 27.44 MiB | 9.27 MiB/s, done.
Total 2099 (delta 367), reused 0 (delta 0)
To git@192.168.200.61:/home/git/repos/solo.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
#将代码克隆到本地进行克隆验证
[root@Jenkins solo]# mkdir -p /test
[root@Jenkins solo]# cd /test/
[root@Jenkins test]# git clone git@192.168.200.61:/home/git/repos/solo.git
正克隆到 \'solo\'...
remote: Counting objects: 2099, done.
remote: Compressing objects: 100% (1677/1677), done.
remote: Total 2099 (delta 367), reused 2099 (delta 367)
接收对象中: 100% (2099/2099), 27.44 MiB | 22.41 MiB/s, done.
处理 delta 中: 100% (367/367), done.
[root@Jenkins test]# ls
solo
[root@Jenkins test]# cd solo/
[root@Jenkins solo]# ls
CHANGE_LOGS.html    Dockerfile   LICENSE       pom.xml    README_zh_CN.md
docker-compose.yml  gulpfile.js  package.json  README.md  src

6.4 创建一个流水线脚本并提交Git远程仓库

#在jenkins服务器上进行如下操作
[root@Jenkins ~]# mkdir -p /jenkinsfile
[root@Jenkins ~]# cd /jenkinsfile/
[root@Jenkins jenkinsfile]# git clone git@192.168.200.61:/home/git/repos/jenkinsfile
正克隆到 \'jenkinsfile\'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 20 (delta 3), reused 0 (delta 0)
接收对象中: 100% (20/20), done.
处理 delta 中: 100% (3/3), done.

[root@Jenkins jenkinsfile]# ls
jenkinsfile
[root@Jenkins jenkinsfile]# cd jenkinsfile/
[root@Jenkins jenkinsfile]# ls
itemA

image.png-55.4kB

image.png-16kB

image.png-28.8kB

image.png-10.1kB

image.png-43.5kB

[root@Jenkins jenkinsfile]# mkdir itemB
[root@Jenkins jenkinsfile]# cd itemB/
[root@Jenkins jenkinsfile]# vim itemB/jenkinsfile-java-solo
[root@Jenkins jenkinsfile]# cat itemB/jenkinsfile-java-solo
node ("PHP-slave2-192.168.200.65") {
   stage(\'git checkout\') {
      checkout([$class: \'GitSCM\', branches: [[name: \'${branch}\']], 
      doGenerateSubmoduleConfigurations: false, extensions: [],submoduleCfg: [], 
      userRemoteConfigs: [[credentialsId: \'f58165e3-e5a7-408a-a57d-a4b643b14308\', 
      url: \'git@192.168.200.61:/home/git/repos/solo.git\']]])
   }
   stage(\'maven build\') {
      sh \'/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true\'
   }
   stage(\'deploy\') {
      sh \'\'\'WWWROOT=/usr/local/tomcat/webapps/ROOT
      [ -d /data/backup ] || mkdir -p /data/backup
      if [ -d $WWWROOT ];then
         mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
      fi
      unzip ${WORKSPACE}/target/*.war -d $WWWROOT
      PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \\'{print $2}\\')
      [ -n "$PID" ] && kill -9 $PID
      /bin/bash /usr/local/tomcat/bin/startup.sh\'\'\'
   }
   stage(\'test\') {
       sh \'echo susses\'
   }
}
#推送本地仓库到远程Git
[root@Jenkins jenkinsfile]# git add *
[root@Jenkins jenkinsfile]# git commit -m "java流水线脚本"
[master 89a206e] java流水线脚本
 1 file changed, 25 insertions(+)
 create mode 100644 itemB/jenkinsfile-java-solo
[root@Jenkins jenkinsfile]# git push -u origin master
Counting objects: 6, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 913 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@192.168.200.61:/home/git/repos/jenkinsfile
   6106cee..89a206e  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
#进行流水线脚本克隆测试
[root@Jenkins jenkinsfile]# cd /test/
[root@Jenkins test]# ls
solo
[root@Jenkins test]# rm -rf *
[root@Jenkins test]# git clone git@192.168.200.61:/home/git/repos/jenkinsfile
正克隆到 \'jenkinsfile\'...
remote: Counting objects: 24, done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 24 (delta 4), reused 0 (delta 0)
接收对象中: 100% (24/24), done.
处理 delta 中: 100% (4/4), done.
[root@Jenkins test]# ls
jenkinsfile
[root@Jenkins test]# cd jenkinsfile/
[root@Jenkins jenkinsfile]# ls
itemA  itemB
[root@Jenkins jenkinsfile]# ls itemB/jenkinsfile
itemB/jenkinsfile

6.5 设置分布式构建的slave管理节点

6.5.1 添加用于ssh连接的账户和密码

image.png-25.3kB

6.5.2 配置slave从节点

image.png-27.5kB

image.png-57.4kB

image.png-13kB

6.6 进行流水线的单Slave从节点分布式构建测试

如果不出意外,构建会失败在最后一步(tomcat进程未启动)。
jenkins没办法启动tomcat的socket进程
原因在于Pipeline流水线在启动一个脚本程序的时候,这个脚本程序会运行在后台。
但是当pipeline结束以后,jenkins会自动kill掉所有和pipeline有关的派生子进程
因此,我们需要在流水线脚本里加一个变量参数,不让pipeline杀掉才可以
JENKINS_NODE_COOKIE=dontkillme

#修改流水线脚本,添加参数,并提交远程仓库
[root@Jenkins jenkinsfile]# cat itemB/jenkinsfile-java-solo 
node ("PHP-slave2-192.168.200.65") {
   stage(\'git checkout\') {
      checkout([$class: \'GitSCM\', branches: [[name: \'${branch}\']], 
      doGenerateSubmoduleConfigurations: false, extensions: [],submoduleCfg: [], 
      userRemoteConfigs: [[credentialsId: \'f58165e3-e5a7-408a-a57d-a4b643b14308\', 
      url: \'git@192.168.200.61:/home/git/repos/solo.git\']]])
   }
   stage(\'maven build\') {
      sh \'/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true\'
   }
   stage(\'deploy\') {
      sh \'\'\'
      JENKINS_NODE_COOKIE=dontkillme    #添加此行
      WWWROOT=/usr/local/tomcat/webapps/ROOT
      [ -d /data/backup ] || mkdir -p /data/backup
      if [ -d $WWWROOT ];then
         mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
      fi
      unzip ${WORKSPACE}/target/*.war -d $WWWROOT
      PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \\'{print $2}\\')
      [ -n "$PID" ] && kill -9 $PID
      /bin/bash /usr/local/tomcat/bin/startup.sh\'\'\'
   }
   stage(\'test\') {
       sh \'echo susses\'
   }
}


#提交远程Git仓库
[root@Jenkins jenkinsfile]# git add *
[root@Jenkins jenkinsfile]# git commit -m "第二次提交java流水线脚本"
[master 1677c59] 第二次提交java流水线脚本
 1 file changed, 3 insertions(+), 1 deletion(-)
[root@Jenkins jenkinsfile]# git push -u origin master
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 457 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To git@192.168.200.61:/home/git/repos/jenkinsfile
   89a206e..1677c59  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

进行分布式构建测试
image.png-23.6kB

image.png-56.2kB

image.png-32.3kB

6.7 进行流水线的单Slave从节点SSH远程分布式构建测试

克隆一台tomcat服务器,进行SSH远程构建

主机名 IP地址 备注
Git 192.168.200.192 Git服务器
Jenkins 192.168.200.193 Jenkins服务器
Web01 192.168.200.196 Tomcat服务器
Web02 192.168.200.197 TomcatB服务器

6.7.1 安装SSH Pipeline Steps插件

image.png-97.5kB

image.png-82.5kB

6.7.2 修改流水线脚本,并提交Git仓库

#修改流水线脚本,并提交Git仓库
[root@Jenkins jenkinsfile]# cat itemB/jenkinsfile-java-solo 
node ("PHP-slave2-192.168.200.196") {
   //def mvnHome
   def remote = [:]
   stage(\'git checkout\') {
    sh \'hostname -I\'
        checkout([$class: \'GitSCM\', branches: [[name: \'${branch}\']], 
        doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: \'d0721eb3-07e1-49f2-bb30-2fae94220fad\', 
        url: \'git@192.168.200.192:/home/git/repos/solo.git\']]])
   }
   stage(\'maven build\') {
        sh \'/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true\'
   }
   stage(\'Remote SSH 200.192\') {
        remote.name = \'test\'
        remote.host = \'192.168.200.192\'
        remote.user = \'root\'
        remote.password = \'666666\'
        remote.allowAnyHosts = true
        writeFile file: \'192.sh\',text:\'echo "`hostname -I`"\'
        sshScript remote: remote,script: "192.sh"
   }
   stage(\'Remote SSH 200.197\') {
        remote.name = \'test\'
        remote.host = \'192.168.200.197\'
        remote.user = \'root\'
        remote.password = \'666666\'
        remote.allowAnyHosts = true
    writeFile file: \'197.sh\',text:\'\'\'
echo "`hostname -I`"
\'\'\'
    sshScript remote: remote,script: "197.sh"
   }
}

6.7.3 进行分布式构建测试

image.png-105.6kB

image.png-43.4kB

image.png-76.1kB

image.png-88.4kB
6.8 进行流水线的多Slave从节点分布式构建测试

添加Slave从节点,并进行多节点构建

image.png-48.9kB

#修改流水线脚本,并提交Git仓库
[root@Jenkins jenkinsfile]# cat itemB/jenkinsfile-java-solo
node ("PHP-slave2-192.168.200.196") {
   //def mvnHome
   stage(\'git checkout\') {
        sh \'hostname -I\'
        checkout([$class: \'GitSCM\', branches: [[name: \'${branch}\']], 
        doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: \'d0721eb3-07e1-49f2-bb30-2fae94220fad\', 
        url: \'git@192.168.200.192:/home/git/repos/solo.git\']]])
   }
   stage(\'maven build\') {
        sh \'/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true\'
   }
   stage(\'deploy\') {
        sh \'\'\'
        JENKINS_NODE_COOKIE=dontkillme
        WWWROOT=/usr/local/tomcat/webapps/ROOT
        [ -d /data/backup ] || mkdir -p /data/backup
        if [ -d $WWWROOT ];then
            mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
        fi
        unzip ${WORKSPACE}/target/*.war -d $WWWROOT
        PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \\'{print $2}\\')
        [ -n "$PID" ] && kill -9 $PID
        /bin/bash /usr/local/tomcat/bin/startup.sh\'\'\'
   }
}
node ("PHP-slave3-192.168.200.197") {
   //def mvnHome
   stage(\'git checkout\') {
        sh \'hostname -I\'
        checkout([$class: \'GitSCM\', branches: [[name: \'${branch}\']], 
        doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], 
        userRemoteConfigs: [[credentialsId: \'d0721eb3-07e1-49f2-bb30-2fae94220fad\', 
        url: \'git@192.168.200.192:/home/git/repos/solo.git\']]])
   }
   stage(\'maven build\') {
        sh \'/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true\'
   }
   stage(\'deploy\') {
        sh \'\'\'
	JENKINS_NODE_COOKIE=dontkillme
	WWWROOT=/usr/local/tomcat/webapps/ROOT
        [ -d /data/backup ] || mkdir -p /data/backup
        if [ -d $WWWROOT ];then
            mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
        fi
        unzip ${WORKSPACE}/target/*.war -d $WWWROOT
        PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \\'{print $2}\\')
        [ -n "$PID" ] && kill -9 $PID
        /bin/bash /usr/local/tomcat/bin/startup.sh\'\'\'
   }
}

image.png-58.3kB

7.生产环境网站项目发布思路

image.png-181.2kB

版权声明:本文为ywb123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ywb123/p/11222997.html