Pipeline流水线JAVA项目发布

所有的全局变量

 
  1. export JAVA_HOME=/usr/local/jdk
  2. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
  3. export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
  4. export TOMCAT_HOME=/usr/local/tomcat
  5. export MAVEN_HOME=/usr/local/maven
  6. export PATH=$PATH:${MAVEN_HOME}
  7. export TOMCAT_HOME=/usr/local/tomcat
 

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

主机名IP地址备注
Git 192.168.200.70 Git服务器
Jenkins 192.168.200.91 Jenkins服务器
Tomcat 192.168.200.104 Tomcat服务器
 

所有服务器进行如下操作

cat /etc/redhat-release

uname -r

systemctl stop firewalld

systemctl disable firewalld

systemctl stop NetworkManager

systemctl disable NetworkManager

image_1cvng71ns5anv3619101bj7hr9p.png-21.1kB

 

Tomcat服务器环境部署

 

tomcat所有包

链接:https://pan.baidu.com/s/1bFIsmqk0AdFGeB2-fbb7cQ 
提取码:uvyw

 

部署jdk

ls

tar xf jdk-8u171-linux-x64.tar.gz -C /usr/local

cd /usr/local

mv jdk1.8.0_171 jdk

ln -s /usr/local/jdk/bin/java /usr/local/bin

java -version

image_1cvngsdkqg9l1lef1kaf2ph1d8h9.png-44.1kB

 

部署java环境变量

 
  1. sed -i.ori '$a export JAVA_HOME=/usr/local/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile

tail -3 /etc/profile

source /etc/profile

image_1cvnh3nh7jq013e81cdsd1n1ak7m.png-41.5kB

 

部署maven

ls

tar xf apache-maven-3.5.0-bin.tar.gz -C /usr/local/

cd /usr/local

mv apache-maven-3.5.0 maven

ln -s /usr/local/maven/bin/mvn /usr/local/bin

mvn -version

image_1cvnhauki393enq1do8b0nrhs13.png-60.7kB

 

部署maven环境变量

vim /etc/profile

 
  1. export MAVEN_HOME=/usr/local/maven
  2. export PATH=$PATH:${MAVEN_HOME}

tail -2 /etc/profile

source /etc/profile

image_1cvnhfn9318g019qn930ldoboi1g.png-21.2kB

 

部署tomcat

ls

tar xf apache-tomcat-8.5.31.tar.gz -C /usr/local/

cd /usr/local

mv apache-tomcat-8.0.27 tomcat

cd tomcat

rm -rf webapps/*

ls webapps/

image_1cvnhk7kv1389q1k995c4h13k61t.png-35.5kB

 

部署tomcat环境变量

echo 'export TOMCAT_HOME=/usr/local/tomcat' >> /etc/profile

tail -1 /etc/profile

source /etc/profile

image_1cvnhoakq1b1e3lbfmomq71cuc2a.png-21.7kB

 

自动部署需要解压maven构建后的war包,因此需要安装unzip

yum -y install unzip

which unzip

image_1cvnhul9p1pol587ckkvue14mr37.png-70.9kB

 

自动部署需要git远程仓库代码,因此需要安装git

yum -y install git

which git

image_1cvnhv67c177t8nn1eqiea8103p3k.png-130.2kB

 

构建一个B-JAVA流水线项目的提前准备工作

 

在Git服务器上创建用于存放solo源代码的仓库

hostname -I

cd /home/git/repos

mkdir solo.git

cd solo.git

git --bare init

cd ..

chown -R git.git solo.git

image_1cvnjl79lgpf1r9l1gtec3k1ftd5r.png-60.6kB

 

在jenkins服务器上进行解压solo.tar.gz

链接:https://pan.baidu.com/s/1Fui3TNCwEsmv4TeUZ0fiWA 
提取码:tjo6 --->solo源代码

ls

tar xf solo.tar.gz

mkdir -p /mycode

cd /mycode

git clone git@192.168.200.70:/home/git/repos/solo.git

ls

cd solo

mv /root/solo/* .

ls

image_1cvnjnrup1cu21inj1lut1b9pfv068.png-63.6kB

 

为了最后的测试,我们需要修改源代码的一个配置文件

ls

cd src/main/resources

cat -n latke.properties | sed -n '29p;31p'

vim latke.properties +29

cat -n latke.properties | sed -n '29p;31p'

 
  1. 29 serverHost=192.168.200.104 --->tomcat服务器IP地址
  2. 31 serverPort=8080 --->监听端口

image_1cvnk1jm51k6g6ch1mu0efmq196l.png-45.1kB

 

将solo源代码提交到git仓库

cd /mycode/solo

git add *

git commit -m "第一次提交"

git push -u origin master

image_1cvnke1j5vhf175ts2ql1o1pb672.png-171kB

 

将代码克隆到本地进行克隆验证

cd /test

git clone git@192.168.200.70:/home/git/repos/solo.git

cd solo

ls

image_1cvnkf9mm1gm0173f1hq51ros1oq77f.png-60.5kB

 

在jenkins服务器上创建一个流水线脚本并提交git远程仓库(构建B-JAVA的时候生成过脚本)

mkdir /jenkinsfile

cd /jenkinsfile

git clone git@192.168.200.70:/home/git/repos/jenkinsfile

ls

cd jenkinsfile

mkdir itemB

vim itemB/jenkinsfile-java-solo

cat itemB/jenkinsfile-java-solo

 
  1. node ("JAVA-slave1-192.168.200.104"){
  2. //def mvnHome
  3. stage('checkout') { // for display purposes
  4. checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
  5. doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
  6. userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
  7. url: 'git@192.168.200.70:/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 '''WWWROOT=/usr/local/tomcat/webapps/ROOT
  14. [ -d /data/backup ] || mkdir -p /data/backup
  15. if [ -d $WWWROOT ];then
  16. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  17. fi
  18. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  19. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
  20. [ -n "$PID" ] && kill -9 $PID
  21. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  22. }
  23. stage('test') {
  24. sh 'echo susses'
  25. }
  26. }

image_1cvnsq61j1jbt11u1vpc1t3vl9jgj.png-115.7kB

 

在jenkins网页上创建tomcat服务器的节点信息

http://192.168.200.91:8080

image_1cvnle6k51uke1t91hcdslh1p367s.png-203.7kB


image_1cvnleq7bjs51r9e1mgi1o7r1k2589.png-104.7kB


image_1cvnlfsb61eum15onqce17o2gik8m.png-92.5kB


image_1cvnloe62g31lv8v5a13n01hkb93.png-136.7kB


image_1cvnlpp7s1kl9ema1e60e8q1m009g.png-90.9kB

 

构建一个B-JAVA流水线项目(有上文生成的脚本信息)

http://192.168.200.91:8080

image_1cvni4mar17ue11t77tpn6f1igp41.png-173kB


image_1cvni9b7v13ieuvvrp5i3g9924u.png-102.8kB


image_1cvnmu5nif1e13hu8pi1sktvch9t.png-117.7kB


image_1cvnn6qt2jh1gt13p21jia1pcaa.png-169kB

 

推送本地仓库到远程git

git add *

git commit -m "java流水线脚本"

git push -u origin master

image_1cvnpbhf01dk9f5d14ke1ndk1qgmc1.png-50.4kB

 

在B-JAVA立即构建并测试

image_1cvnpcurj103l1elul7qm9qiicce.png-81.1kB


image_1cvnstauula7qid67qd5bqioh0.png-141.6kB

 

在tomcat服务器上查看tomcat服务并在浏览器查看测试结果

ss -antup | grep 8080

http://192.168.200.104:8080

image_1cvnt10gk1qbt1qpd1ofvn5j18nhd.png-16.9kB


image_1cvnt2ah31mev1jlf1p911ubng4ohq.png-62.3kB

 

如果进行流水线的单Slave从节点分布式构建测试(出现tomcat服务未启动情况)

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

 

在jenkins服务器上重新克隆远程仓库代码

cd /jenkinsfile

rm -rf *

git clone git@192.168.200.70:/home/git/repos/jenkinsfile

ls

cd /jenkinsfile

ls

image_1cvnqcmrrqdbadf1o245fon5dl.png-54.2kB

 

修改jenkins服务器上的流水线脚本

cd /jenkinsfile/jenkinsfile/

vim itemB/jenkinsfile-java-solo

cat itemB/jenkinsfile-java-solo

 
  1. node ("JAVA-slave1-192.168.200.104"){
  2. //def mvnHome
  3. stage('checkout') { // for display purposes
  4. checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
  5. doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
  6. userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
  7. url: 'git@192.168.200.70:/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. sh 'echo susses'
  27. }
  28. }

image_1cvnt8bhv18h01bci1su311fn18svi7.png-87.5kB

 

推送本地仓库到远程git

git add *

git commit -m "java-流水线脚本"

git push -u origin master

image_1cvnt9914i1kue1amb1lp11a3ik.png-48.2kB

 

在B-JAVA立即构建并测试

image_1cvnrkhgala017691ci0gl17l5g6.png-44.1kB


image_1cvnu25b51a35ksqdk010g01qntj1.png-153.2kB

 

在tomcat服务器上查看进程并浏览器上查看测试结果

ss -antup | grep 8080 --->不添加内段话,流水线脚本默认杀掉tomcat服务

http://192.168.200.104:8080

image_1cvnu2j821lpn1vefakljuq1tr2je.png-26.2kB


image_1cvnu30bm1rlt12ig1ljrdqlfs8jr.png-62.7kB

 

让ssh支持流水线,需要安装插件SSH Pipeline Steps

image_1cvnvdo551a271ear1uui6d8ernk8.png-136.6kB


image_1cvnve3cvsmt1jhvfoparr1a54kl.png-138.2kB


image_1cvnvkqbprpggtrmnh31gtd5lv.png-152.6kB

 

流水线ssh方式的脚本详解

 
  1. def remote = [:] #定义变量不需要改
  2. remote.name = 'test' #远程主机的名字
  3. remote.host = 'test.domain.com' #远程主机的IP
  4. remote.user = 'root' #远程主机的用户
  5. remote.password = 'password' #远程主机的密码
  6. remote.allowAnyHosts = true #照着超就可以
  7. stage('Remote SSH') { #阶段名称
  8. writeFile file: 'abc.sh', text: 'ls -lrt' #把后面的命令,让如abc.sh脚本里
  9. sshScript remote: remote, script: "abc.sh" #把脚本分发自定义的主机
  10. }
 

在浏览器进入jenkins主用户来添加新节点信息(有几点节点就添加几个)

image_1cvnvk36c2qc1e2616ss1vh7jvvli.png-110.6kB

 

流水线ssh方式的脚本内容(一个节点给多台发送SSH脚本)

 
  1. node ("JAVA-slave1-192.168.200.104"){
  2. //def mvnHome
  3. def remote = [:]
  4. stage('checkout') { // for display purposes
  5. checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
  6. doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
  7. userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
  8. url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
  9. }
  10. stage('maven build') {
  11. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  12. }
  13. stage('Remote ssh 200.104') {
  14. remote.name = 'test'
  15. remote.user = '192.168.200.104'
  16. remote.user = 'root'
  17. remote.password = 'linyaonie'
  18. remote.allowAnyHosts = true
  19. writeFile file: '104.sh',text:'''
  20. echo "`hostname -I`"
  21. '''
  22. sshScript remote: remote,script: "104.sh"
  23. }
  24. stage('Remote ssh 200.xxx') {
  25. remote.name = 'test'
  26. remote.user = '192.168.200.xxx'
  27. remote.user = 'root'
  28. remote.password = 'linyaonie'
  29. remote.allowAnyHosts = true
  30. writeFile file: 'xxx.sh',text:'''
  31. echo "`hostname -I`"
  32. '''
  33. sshScript remote: remote,script: "xxx.sh"
  34. }
  35. }
 

重新把新修过的内容添加到远程仓库并构建测试在查看logs日志的结果

git add *

git commit -m "测试SSH"

git push -u origin master

 

流水线项目发布多节点需要注意的事项

 

多节点流水线脚本内容(多个节点)

 
  1. node ("JAVA-slave1-192.168.200.104"){
  2. //def mvnHome
  3. stage('checkout') { // for display purposes
  4. checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
  5. doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
  6. userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
  7. url: 'git@192.168.200.70:/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. sh 'echo susses'
  27. }
  28. }
  29. node ("JAVA-slave2-192.168.200.xxx"){
  30. //def mvnHome
  31. stage('checkout') { // for display purposes
  32. checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
  33. doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
  34. userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
  35. url: 'git@192.168.200.70:/home/git/repos/solo.git']]])
  36. }
  37. stage('maven build') {
  38. sh '/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'
  39. }
  40. stage('deploy') {
  41. sh '''
  42. JENKINS_NODE_COOKIE=dontkillme
  43. WWWROOT=/usr/local/tomcat/webapps/ROOT
  44. [ -d /data/backup ] || mkdir -p /data/backup
  45. if [ -d $WWWROOT ];then
  46. mv $WWWROOT /data/backup/tomcat-$(date +%F_%T)
  47. fi
  48. unzip ${WORKSPACE}/target/*.war -d $WWWROOT
  49. PID=$(ps -ef | grep tomcat | egrep -v "grep|$$" | awk '{print $2}')
  50. [ -n "$PID" ] && kill -9 $PID
  51. /bin/bash /usr/local/tomcat/bin/startup.sh'''
  52. }
  53. stage('test') {
  54. sh 'echo susses'
  55. }
  56. }
 

假如是克隆的机器需要把hosts文件里的映射修改一下

 

在浏览器进入jenkins主用户来添加新节点信息(有几点节点就添加几个)

 

重新把新修过的内容添加到远程仓库并构建测试在查看logs日志的结果

git add *

git commit -m "测试SSH"

git push -u origin master

 

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

image_1cvnvomh919tkjg5m5nfgp1795nc.png-678.4kB

 

Pipeline流水线云架构参考图

image_1cvnvvj1l14k31t9u1gr0tlo126f9.png-166.6kB

posted @ 2019-07-24 15:33  L1n  阅读(3479)  评论(1编辑  收藏  举报