Jenkins流水线项目发布
CI/CD代码发布流程
本次环境为:
主机名 | IP | 已安装的服务 |
localhost | 192.168.44.128 | tomcat jenkins |
node1 | 192.168.44.129 | tomcat |
//准备工作 [root@localhost ~]# systemctl disable --now firewalld [root@localhost ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config [root@localhost ~]# setenforce 0 [root@localhost ~]# yum -y install maven git [root@localhost ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.44.129 node1 [root@localhost ~]# ssh-keygen -t rsa //全部按回车 [root@localhost ~]# ssh-copy-id root@192.168.44.129 [root@node1 ~]# systemctl disable --now firewalld [root@node1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config [root@node1 ~]# setenforce 0
首先把源替换成国内源
再安装Pipeline、Publish Over SSH、Publish Over SSH、SSH Build Agents、Git、GitHub、GitLab、Gitee插件(如果出现错误多装几次)
新建任务
pipeline { agent any stages { stage('pull code') { steps { #拉取代码 git 'http://github.com/lizhenliang/tomcat-java-demo.git' } } stage('package') { steps { #打包代码 sh """ mvn clean mvn package """ } } stage('deploy') { steps { #scp命令传输包到部署机上并重启tomcat环境 sh """ scp target/ly-simple-tomcat-0.0.1-SNAPSHOT.war root@192.168.44.129:/usr/local/apache-tomcat-9.0.46/webapps ssh root@192.168.44.129 "bash /usr/local/apache-tomcat-9.0.46/bin/shutdown.sh && bash /usr/local/apache-tomcat-9.0.46/bin/startup.sh" """ } } } }
基于参数构建流水线流程
pipeline { agent any stages { stage('pull code') { steps { git "${GIT_URL}" #调用仓库地址变量 } } stage('package') { steps { sh """ mvn clean mvn package """ } } stage('deploy') { steps { sh """ #调用maven包名,部署主机,tomcat安装目录 scp target/${MAVEN_PACK}.war root@${DEPLOY_HOST}:${TOMCAT_DIR}webapps ssh root@192.168.44.129 "bash ${TOMCAT_DIR}bin/shutdown.sh && bash ${TOMCAT_DIR}bin/startup.sh" #远程命令控制部署机重启tomcat服务 """ } } } }
验证