朱汉自动化运维专题(十):项目案例之Pipeline分布式流水线发布JAVA项目

自动化运维专题(十):项目案例之Pipeline分布式流水线发布JAVA项目

Jenkins自动化运维专题


六,项目案例二:流水线自动化发布JAVA项目

PYCPdfView_l8Pdj7rUBB.png-14.2kB

  1. #所有服务器进行如下操作
  2. [root@localhost ~]# cat /etc/redhat-release
  3. CentOS Linux release 7.5.1804 (Core)
  4. [root@localhost ~]# uname -r
  5. 3.10.0-862.el7.x86_64
  6. [root@localhost ~]# systemctl stop firewalld
  7. [root@localhost ~]# systemctl disable firewalld
  8. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
  9. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  10. [root@localhost ~]# systemctl stop NetworkManager
  11. [root@localhost ~]# systemctl disable NetworkManager
  12. Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
  13. Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
  14. Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.

6.1 Tomcat服务器环境部署

  1. #在tomcat服务器进行如下操作
  2. #部署jdk
  3. [root@localhost ~]# ls
  4. anaconda-ks.cfg apache-tomcat-8.5.31.tar.gz
  5. apache-maven-3.5.0-bin.tar.gz jdk-8u171-linux-x64.tar.gz
  6. [root@localhost ~]# tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local/
  7. [root@localhost ~]# cd /usr/local/
  8. [root@localhost local]# mv jdk1.8.0_171 jdk
  9. [root@localhost local]# ln -s /usr/local/jdk/bin/java /usr/local/bin/
  10. [root@localhost local]# java -version
  11. java version "1.8.0_171"
  12. Java(TM) SE Runtime Environment (build 1.8.0_171-b11)
  13. Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)
  14. #部署java变量
  15. [root@localhost local]# sed -i.org '$a export JAVA_HOME=/usr/local/jdk/' /etc/profile
  16. [root@localhost local]# sed -i.org '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
  17. [root@localhost local]# sed -i.org '$a export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
  18. [root@localhost local]# tail -3 /etc/profile
  19. export JAVA_HOME=/usr/local/jdk/
  20. export PATH=$PATH:$JAVA_HOME/bin
  21. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
  22. [root@localhost local]# source /etc/profile
  23. #部署maven
  24. [root@localhost ~]# ls
  25. anaconda-ks.cfg apache-tomcat-8.5.31.tar.gz
  26. apache-maven-3.5.0-bin.tar.gz jdk-8u171-linux-x64.tar.gz
  27. [root@localhost ~]# tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/
  28. [root@localhost ~]# cd /usr/local/
  29. [root@localhost local]# mv apache-maven-3.5.0 maven
  30. [root@localhost local]# ln -s /usr/local/maven/bin/mvn /usr/local/bin/
  31. [root@localhost local]# mvn -version
  32. Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
  33. Maven home: /usr/local/maven
  34. Java version: 1.8.0_171, vendor: Oracle Corporation
  35. Java home: /usr/local/jdk/jre
  36. Default locale: zh_CN, platform encoding: UTF-8
  37. OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"
  38. #部署maven环境变量
  39. [root@localhost local]# vim /etc/profile
  40. [root@localhost local]# tail -2 /etc/profile
  41. export MAVEN_HOME=/usr/local/maven
  42. export PATH=$PATH:${MAVEN_HOME}
  43. [root@localhost local]# source /etc/profile
  44. #解压tomcat
  45. [root@localhost ~]# ls
  46. anaconda-ks.cfg apache-tomcat-8.5.31.tar.gz
  47. apache-maven-3.5.0-bin.tar.gz jdk-8u171-linux-x64.tar.gz
  48. [root@localhost ~]# tar xf apache-tomcat-8.5.31.tar.gz -C /usr/local/
  49. [root@localhost ~]# cd /usr/local/
  50. [root@localhost local]# mv apache-tomcat-8.5.31 tomcat
  51. [root@localhost local]# cd tomcat/
  52. [root@localhost tomcat]# ls
  53. bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work
  54. [root@localhost tomcat]# rm -rf webapps/*
  55. [root@localhost tomcat]# ls webapps/
  56. #部署tomcat环境变量
  57. [root@localhost tomcat]# vim /etc/profile
  58. [root@localhost tomcat]# tail -1 /etc/profile
  59. export TOMCAT_HOME=/usr/local/tomcat
  60. [root@localhost tomcat]# source /etc/profile
  61. #自动部署需要解压maven构建后的war包,因此需要安装unzip
  62. [root@localhost tomcat]# yum -y install unzip
  63. [root@localhost tomcat]# which unzip
  64. /usr/bin/unzip
  65. #自动部署需要git远程仓库代码,因此需要安装git
  66. [root@localhost tomcat]# yum -y install git

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

PYCPdfView_0T7pNWF7zU.png-138.5kB

PYCPdfView_eT2D658oLI.png-69.1kB

PYCPdfView_w4dYlqaCwa.png-68.7kB

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

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

  1. #在Git服务器创建用于存放solo源代码的仓库
  2. [git@localhost ~]$ hostname -I
  3. 192.168.200.189
  4. [git@localhost ~]$ cd /home/git/repos/
  5. [git@localhost repos]$ ls
  6. app.git jenkinsfile wordpress
  7. [git@localhost repos]$ mkdir solo.git
  8. [git@localhost repos]$ chown git.git solo.git
  9. [git@localhost repos]$ cd solo.git/
  10. [git@localhost solo.git]$ git --bare init
  11. 初始化空的 Git 仓库于 /home/git/repos/solo.git/
  12. [git@localhost solo.git]$ cd ..
  13. [git@localhost repos]$ chown -R git.git solo.git
  14. #在jenkins服务器上进行如下操作
  15. #解压solo.tar.gz源代码包
  16. [root@localhost ~]# tar xf solo.tar.gz
  17. mk[root@localhost ~]# mkdir -p /mycode/
  18. [root@localhost ~]# cd /mycode/
  19. [root@localhost mycode]# ls
  20. app
  21. [root@localhost mycode]# rm -rf *
  22. [root@localhost mycode]# git clone git@192.168.200.189:/home/git/repos/solo.git
  23. 正克隆到 'solo'...
  24. warning: 您似乎克隆了一个空版本库。
  25. [root@localhost mycode]# ls
  26. solo
  27. [root@localhost solo]# mv /root/solo .
  28. [root@localhost solo]# ls
  29. solo
  30. [root@localhost solo]# mv solo/* .
  31. [root@localhost solo]# rm -rf solo/
  32. [root@localhost solo]# ls
  33. CHANGE_LOGS.html Dockerfile LICENSE pom.xml README_zh_CN.md
  34. docker-compose.yml gulpfile.js package.json README.md src
  35. #为了最后的测试,我们需要修改源代码的一个配置文件
  36. [root@localhost solo]# ls
  37. CHANGE_LOGS.html Dockerfile LICENSE pom.xml README_zh_CN.md
  38. docker-compose.yml gulpfile.js package.json README.md src
  39. [root@localhost solo]# cd src/main/resources/
  40. [root@localhost resources]# ll latke.properties
  41. -rw-r--r--. 1 root root 1241 10月 18 2018 latke.properties
  42. [root@localhost resources]# sed -n '29p;31p' latke.properties
  43. serverHost=localhost
  44. serverPort=8080
  45. [root@localhost resources]# vim latke.properties
  46. [root@localhost resources]# sed -n '29p;31p' latke.properties
  47. serverHost=192.168.200.200
  48. serverPort=8080
  49. #将solo源代码提交到git仓库
  50. [root@localhost solo]# cd /mycode/solo/
  51. [root@localhost solo]# git add *
  52. [root@localhost solo]# git commit -m "x"
  53. [root@localhost solo]# git push -u origin master
  54. Counting objects: 2099, done.
  55. Compressing objects: 100% (2044/2044), done.
  56. Writing objects: 100% (2099/2099), 27.44 MiB | 19.06 MiB/s, done.
  57. Total 2099 (delta 367), reused 0 (delta 0)
  58. To git@192.168.200.189:/home/git/repos/solo.git
  59. * [new branch] master -> master
  60. 分支 master 设置为跟踪来自 origin 的远程分支 master。
  61. #将代码克隆到本地进行克隆认证
  62. [root@localhost solo]# cd /test/
  63. [root@localhost test]# ls
  64. jenkinsfile
  65. [root@localhost test]# rm -rf *
  66. [root@localhost test]# git clone git@192.168.200.189:/home/git/repos/solo.git
  67. 正克隆到 'solo'...
  68. remote: 对象计数中: 2099, 完成.
  69. remote: 压缩对象中: 100% (1677/1677), 完成.
  70. remote: Total 2099 (delta 367), reused 2099 (delta 367)
  71. 接收对象中: 100% (2099/2099), 27.44 MiB | 0 bytes/s, done.
  72. 处理 delta 中: 100% (367/367), done.
  73. [root@localhost test]# cd solo/
  74. [root@localhost solo]# ls
  75. CHANGE_LOGS.html Dockerfile LICENSE pom.xml README_zh_CN.md
  76. docker-compose.yml gulpfile.js package.json README.md src

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

  1. #在jenkins服务器上进行如下操作
  2. [root@localhost solo]# mkdir -p /jenkinsfile
  3. [root@localhost solo]# cd /jenkinsfile/
  4. [root@localhost jenkinsfile]# git clone git@192.168.200.189:/home/git/repos/jenkinsfile
  5. 正克隆到 'jenkinsfile'...
  6. remote: 对象计数中: 16, 完成.
  7. remote: 压缩对象中: 100% (11/11), 完成.
  8. remote: Total 16 (delta 2), reused 0 (delta 0)
  9. 接收对象中: 100% (16/16), done.
  10. 处理 delta 中: 100% (2/2), done.
  11. [root@localhost jenkinsfile]# ls
  12. jenkinsfile
  13. [root@localhost jenkinsfile]# cd jenkinsfile/
  14. [root@localhost jenkinsfile]# ls
  15. itemA
  16. [root@localhost jenkinsfile]# mkdir itemB
  17. [root@localhost jenkinsfile]# ls
  18. itemA itemB
  19. [root@localhost jenkinsfile]# vim itemB/jenkinsfile-java-solo
  20. [root@localhost jenkinsfile]# cat itemB/jenkinsfile-java-solo
  21. node ("B-JAVA") {
  22. stage('git checkout') {
  23. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [],
  24. userRemoteConfigs: [[credentialsId: 'ea8eec2d-4809-4035-8fd1-244598f7eba4',
  25. url: 'git@192.168.200.189:/home/git/repos/solo.git']]])
  26. }
  27. stage('maven build') {
  28. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  29. }
  30. stage('deploy') {
  31. sh '''WWWROOT=/usr/local/tomcat/webapps/ROOT
  32. [ -d /data/backup ] || mkdir -p /data/backup
  33. if [ -d $WWWROOT ];then
  34. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  35. fi
  36. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  37. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \'{print $2}\')
  38. [ -n "$PID" ] && kill -9 $PID
  39. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  40. }
  41. stage('test') {
  42. sh 'echo susses'
  43. }
  44. }

PYCPdfView_S3ilZs1hpE.png-68kB

PYCPdfView_JUTQf6MNNe.png-61.1kB

PYCPdfView_rOMxU50rOu.png-62.4kB

  1. #推送本地仓库到远程git
  2. [root@localhost jenkinsfile]# git add *
  3. [root@localhost jenkinsfile]# git commit -m "test"
  4. [master 9ac23dd] test
  5. 1 file changed, 24 insertions(+)
  6. create mode 100644 itemB/jenkinsfile-java-solo
  7. [root@localhost jenkinsfile]# git push -u origin master
  8. Counting objects: 5, done.
  9. Compressing objects: 100% (3/3), done.
  10. Writing objects: 100% (4/4), 860 bytes | 0 bytes/s, done.
  11. Total 4 (delta 0), reused 0 (delta 0)
  12. To git@192.168.200.189:/home/git/repos/jenkinsfile
  13. 081e971..9ac23dd master -> master
  14. 分支 master 设置为跟踪来自 origin 的远程分支 master
  15. #进行流水线脚本克隆测试
  16. [root@localhost jenkinsfile]# cd /test/
  17. [root@localhost test]# ls
  18. solo
  19. [root@localhost test]# rm -rf *
  20. [root@localhost test]# git clone git@192.168.200.189:/home/git/repos/jenkinsfile
  21. 正克隆到 'jenkinsfile'...
  22. remote: 对象计数中: 20, 完成.
  23. remote: 压缩对象中: 100% (14/14), 完成.
  24. remote: Total 20 (delta 2), reused 0 (delta 0)
  25. 接收对象中: 100% (20/20), done.
  26. 处理 delta 中: 100% (2/2), done.
  27. [root@localhost test]# ls
  28. jenkinsfile
  29. [root@localhost test]# cd jenkinsfile/
  30. [root@localhost jenkinsfile]# ls
  31. itemA itemB
  32. [root@localhost jenkinsfile]# ls itemB
  33. jenkinsfile-java-solo

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

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

PYCPdfView_djlmGd0NfY.png-34.4kB

6.5.2 配置slave从节点

PYCPdfView_7DgKwmgTs7.png-80.4kB

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

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

  • JENKINS_NODE_COOKIE=dontkillme
  1. node ("B-JAVA") {
  2. stage('git checkout') {
  3. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [],
  4. userRemoteConfigs: [[credentialsId: 'ea8eec2d-4809-4035-8fd1-244598f7eba4',
  5. url: 'git@192.168.200.189:/home/git/repos/solo.git']]])
  6. }
  7. stage('maven build') {
  8. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  9. }
  10. stage('deploy') {
  11. sh '''WWWROOT=/usr/local/tomcat/webapps/ROOT
  12. JENKINS_NODE_COOKIE=dontkillme
  13. [ -d /data/backup ] || mkdir -p /data/backup
  14. if [ -d $WWWROOT ];then
  15. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  16. fi
  17. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  18. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \'{print $2}\')
  19. [ -n "$PID" ] && kill -9 $PID
  20. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  21. }
  22. stage('test') {
  23. sh 'echo susses'
  24. }
  25. }
  26. #提交远程git仓库
  27. [root@localhost itemB]# git add *
  28. [root@localhost itemB]# git commit -m "xxx"
  29. [root@localhost itemB]# git push -u origin master

进行分布式构建测试

PYCPdfView_ZevJc0GDle.png-64.6kB

PYCPdfView_0EjwlAYqAO.png-22.5kB

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

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

PYCPdfView_Wa4lxG5z7L.png-28.1kB

6.7.1 安装SSH Pipeline Steps插件

PYCPdfView_5VcRUqbf4Y.png-105kB

PYCPdfView_4FEMpcmDdr.png-95.5kB

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

  1. #修改流水线脚本,并提交Git仓库
  2. node ("B-JAVA") {
  3. //def mvnHome
  4. def remote = [:]
  5. stage('git checkout') {
  6. sh 'hostname -I'
  7. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [],
  8. userRemoteConfigs: [[credentialsId: 'ea8eec2d-4809-4035-8fd1-244598f7eba4',
  9. url: 'git@192.168.200.189:/home/git/repos/solo.git']]])
  10. }
  11. stage('maven build') {
  12. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  13. }
  14. stage('Remote SSH 200.189') {
  15. remote.name = 'test'
  16. remote.host = '192.168.200.189'
  17. remote.user = 'git'
  18. remote.password = '123123'
  19. remote.allowAnyHosts = true
  20. writeFile file: '189.sh', text: 'echo "`hostname -I`"'
  21. sshScript remote: remote, script: "189.sh"
  22. }
  23. stage('Remote SSH 192.168.200.201') {
  24. remote.name = 'test'
  25. remote.host = '192.168.200.201'
  26. remote.user = 'root'
  27. remote.password = '635509'
  28. remote.allowAnyHosts = true
  29. writeFile file: '201.sh', text: 'echo "`hostname -I`"'
  30. sshScript remote: remote, script: "201.sh"
  31. }
  32. }

6.7.3 进行分布式构建测试

PYCPdfView_hJKzIafugF.png-69.2kB

PYCPdfView_N9fsUb0OS7.png-118.4kB

PYCPdfView_yULzpDfGh7.png-106.7kB

6.8 进行流水线的多slave从节点分布式构建测试

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

PYCPdfView_XA775HJqBu.png-57.8kB

  1. #修改流水线脚本,并提交Git仓库
  2. node ("B-JAVA") {
  3. //def mvnHome
  4. stage('Git checkout') {
  5. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [],
  6. userRemoteConfigs: [[credentialsId: 'ea8eec2d-4809-4035-8fd1-244598f7eba4',
  7. url: 'git@192.168.200.189:/home/git/repos/solo.git']]])
  8. }
  9. stage('Maven Build') {
  10. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  11. }
  12. stage('Deploy') {
  13. sh '''
  14. JENKINS_NODE_COOKIE=dontkillme
  15. WWWROOT=/usr/local/tomcat/webapps/ROOT
  16. [ -d /data/backup ] || mkdir -p /data/backup
  17. if [ -d $WWWROOT ];then
  18. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  19. fi
  20. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  21. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \'{print $2}\')
  22. [ -n "$PID" ] && kill -9 $PID
  23. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  24. }
  25. stage('Test') {
  26. echo "success"
  27. }
  28. }
  29. node ("B-JAVA2") {
  30. //def mvnHome
  31. stage('git checkout') {
  32. checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [],
  33. userRemoteConfigs: [[credentialsId: 'ea8eec2d-4809-4035-8fd1-244598f7eba4',
  34. url: 'git@192.168.200.189:/home/git/repos/solo.git']]])
  35. }
  36. stage('Maven Build') {
  37. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  38. }
  39. stage('Deploy') {
  40. sh '''
  41. JENKINS_NODE_COOKIE=dontkillme
  42. WWWROOT=/usr/local/tomcat/webapps/ROOT
  43. [ -d /data/backup ] || mkdir -p /data/backup
  44. if [ -d $WWWROOT ];then
  45. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  46. fi
  47. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  48. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk \'{print $2}\')
  49. [ -n "$PID" ] && kill -9 $PID
  50. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  51. }
  52. stage('Test') {
  53. echo "success"
  54. }
  55. }

PYCPdfView_12ww7QoF2a.png-45.7kB

posted @ 2021-10-07 23:37  有小熊陪着你看月亮  阅读(494)  评论(0)    收藏  举报