jenkins pipline实战案例

pipline部署示例
node {
  stage("clone代码"){
    sh 'rm -rf /var/lib/jenkins/workspace/pipeline-test/*'
    git branch: 'develop', credentialsId: 'd7e3bd0b-fd88-4ffa-b330-2258e93319ed', url: 'git@10.0.0.100:magedu/app1.git'
  }
  stage("代码打包"){
    sh 'cd /var/lib/jenkins/workspace/pipeline-test/ && tar czvf /data/code.tar.gz *'
  }
 stage("代码复制"){
    sh 'scp /data/code.tar.gz 10.0.0.104:/data/tomcat/tomcat_appdir/'
    sh 'scp /data/code.tar.gz 10.0.0.105:/data/tomcat/tomcat_appdir/'
  }
  stage("停止tomcat服务"){
     sh 'ssh 10.0.0.104 "systemctl stop tomcat"'
     sh 'ssh 10.0.0.105 "systemctl stop tomcat"'
  }

  stage("代码部署"){
     sh 'ssh 10.0.0.104 "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
     sh 'ssh 10.0.0.105 "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
 }
  stage("启动tomcat服务"){
    sh 'ssh 10.0.0.104 "systemctl start tomcat"'
    sh 'ssh 10.0.0.105 "systemctl start tomcat"'
  } 
}
执行并验证pipline job

在gitlab提交代码,执行pipline job并验证代码是否最终部署到了web服务器。

jenkins pipline实战案例插图

指定node节点 运行job
#jenkins的slave的节点需要安装git命令
[root@jenkins-slave1-ubuntu ~]#apt -y install git
[root@jenkins-slave2-ubuntu ~]#apt -y install git

#jenkins的slave的节点需要打通与web server免密钥登录
[root@jenkins-slave1-ubuntu ~]#ssh-keygen
[root@jenkins-slave1-ubuntu ~]#ssh-copy-id 10.0.0.104
[root@jenkins-slave1-ubuntu ~]#ssh-copy-id 10.0.0.105

[root@jenkins-slave2-ubuntu ~]#ssh-keygen
[root@jenkins-slave2-ubuntu ~]#ssh-copy-id 10.0.0.104
[root@jenkins-slave2-ubuntu ~]#ssh-copy-id 10.0.0.105

jenkins pipeline代码

node("jenkins-slave1"){
  stage("clone代码"){
    sh 'rm -rf /var/lib/jenkins/workspace/pipeline-test/*'
    git branch: 'develop', credentialsId: 'd7e3bd0b-fd88-4ffa-b330-2258e93319ed', url: 'git@10.0.0.100:magedu/app1.git'
  }
  stage("代码打包"){
    sh 'cd /var/lib/jenkins/workspace/pipeline-test/ && tar czvf /data/code.tar.gz *'
  }
 stage("代码复制"){
    sh 'scp /data/code.tar.gz 10.0.0.104:/data/tomcat/tomcat_appdir/'
    sh 'scp /data/code.tar.gz 10.0.0.105:/data/tomcat/tomcat_appdir/'
  }
  stage("停止tomcat服务"){
     sh 'ssh 10.0.0.104 "systemctl stop tomcat"'
     sh 'ssh 10.0.0.105 "systemctl stop tomcat"'
  }
  stage("代码部署"){
     sh 'ssh 10.0.0.104 "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
     sh 'ssh 10.0.0.105 "rm -rf /data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"'
 }
  stage("启动tomcat服务"){
    sh 'ssh 10.0.0.104 "systemctl start tomcat"'
    sh 'ssh 10.0.0.105 "systemctl start tomcat"'
  } 
}
验证slave执行构建

jenkins pipline实战案例插图(1)

查看流水线步骤

jenkins pipline实战案例插图(2)

验证web 服务器代码版本

Gitlab重新提交代码并测试代码部署

[root@centos8 app1]#vim index.html 
[root@centos8 app1]#cat index.html 
 <h1>magedu/app1 v16 </h1>
[root@centos8 app1]#git add .;git commit -m v16;git push
Username for 'http://10.0.0.100': wang
Password for 'http://wang@10.0.0.100': 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 259 bytes | 259.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for develop, visit:
remote:   http://10.0.0.100/magedu/app1/merge_requests/new?merge_request%5Bsource_branch%5D=develop
remote: 
To http://10.0.0.100/magedu/app1.git
   2bdda2f..c5ca637  develop -> develop
[root@centos8 app1]#

在后端服务器验证结果

[root@tomcat1 ~]#cat /data/tomcat/tomcat_webapps/myapp/index.html 
<h1>magedu/app1 v16 </h1>
[root@tomcat1 ~]#systemctl status tomcat
● tomcat.service - Tomcat
   Loaded: loaded (/lib/systemd/system/tomcat.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-10 14:30:34 CST; 57s ago
  Process: 7034 ExecStop=/apps/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 7196 ExecStart=/apps/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 7204 (java)
    Tasks: 41 (limit: 1084)
   CGroup: /system.slice/tomcat.service
           └─7204 /apps/jdk/jre/bin/java -Djava.util.logging.config.file=/apps/tomcat/conf/logging.properties -Djava.util.logging.mana

Mar 10 14:30:34 tomcat1 systemd[1]: Starting Tomcat...
Mar 10 14:30:34 tomcat1 startup.sh[7196]: Tomcat started.
Mar 10 14:30:34 tomcat1 systemd[1]: Started Tomcat.

[root@tomcat2 ~]#cat /data/tomcat/tomcat_webapps/myapp/index.html
<h1>magedu/app1 v16 </h1>
[root@tomcat2 ~]#systemctl status tomcat
● tomcat.service - Tomcat
   Loaded: loaded (/lib/systemd/system/tomcat.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-03-10 14:30:35 CST; 1min 29s ago
  Process: 5717 ExecStop=/apps/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)
  Process: 5879 ExecStart=/apps/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 5887 (java)
    Tasks: 41 (limit: 1084)
   CGroup: /system.slice/tomcat.service
           └─5887 /usr/local/jdk/jre/bin/java -Djava.util.logging.config.file=/apps/tomcat/conf/logging.properties -Djava.util.logging

Mar 10 14:30:35 tomcat2 systemd[1]: Starting Tomcat...
Mar 10 14:30:35 tomcat2 startup.sh[5879]: Tomcat started.
Mar 10 14:30:35 tomcat2 systemd[1]: Started Tomcat.

本文链接:http://www.yunweipai.com/35846.html

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/52613.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论