![0bfddac1fa061e7468d2da1870ed3c97.png](https://i-blog.csdnimg.cn/blog_migrate/a352edfd84d31125d4c70fb0f509e3c1.jpeg)
环境:(软件部署过程省略...)
192.168.2.50 jenkins
192.168.2.33 jenkins-slave tomcat
192.168.2.51 git-server
1、创建git 仓库 ,放入源代码
192.168.2.51 上:
[root@git ~]# su - gitLast login: Fri Jun 12 03:59:22 EDT 2020 on pts/0[git@git ~]$ lsrepos[git@git ~]$ cd repos/[git@git repos]$ lsapp.git blog.git jenkinsfile.git[git@git repos]$ mkdir test1.git[git@git repos]$ cd test1.git/[git@git test1.git]$ git --bare initInitialized empty Git repository in /home/git/repos/test1.git/[git@git test1.git]$ lsbranches config description HEAD hooks info objects refs
192.168.2.50 客户端 上传代码到master 分支上
[root@jenkins ~]# git clone git@192.168.2.51:/home/git/repos/test1.gitCloning into 'test1'...warning: You appear to have cloned an empty repository.[root@jenkins ~]# git clone https://gitee.com/jflyfox/jfinal_cms.gitCloning into 'jfinal_cms'...remote: Enumerating objects: 6435, done.remote: Counting objects: 100% (6435/6435), done.remote: Compressing objects: 100% (3221/3221), done.remote: Total 6435 (delta 2868), reused 6320 (delta 2790), pack-reused 0Receiving objects: 100% (6435/6435), 39.62 MiB | 10.26 MiB/s, done.Resolving deltas: 100% (2868/2868), done.[root@jenkins ~]# lsapache-maven-3.5.3-bin.tar.gz app blog jdk-8u201-linux-x64.tar.gz jenkinsfile jfinal_cms test1[root@jenkins ~]# cd jfinal_cms/[root@jenkins jfinal_cms]# lsApache v2 License.license ChangeLog.md doc pom.xml README.md sql src[root@jenkins jfinal_cms]# mv ./* ../test1/[root@jenkins jfinal_cms]# ls ../test1Apache v2 License.license ChangeLog.md doc pom.xml README.md sql src[root@jenkins jfinal_cms]#cd ../test1[root@jenkins test1]# git add .[root@jenkins test1]# git commit -m "test1"[root@jenkins test1]# git push origin masterCounting objects: 2527, done.Delta compression using up to 2 threads.Compressing objects: 100% (2415/2415), done.Writing objects: 100% (2527/2527), 22.14 MiB | 9.08 MiB/s, done.Total 2527 (delta 375), reused 0 (delta 0)To git@192.168.2.51:/home/git/repos/test1.git* [new branch] master -> master
2、编写pipeline 流水线
[root@jenkins item-b]#cd /root/jenkinsfile/item-b
[root@jenkins item-b]# vim Jenkinsfile-java
node ("slave-192.168.2.33") {//指定节点运行stage('git checkout') {checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'git@192.168.2.51:/home/git/repos/test1.git']]])}//指定git 地址;拉取代码,使用jenkins拉下来一个repo在jenkins的workspace中stage('maven build') {sh '''export JAVA_HOME=/usr/local/jdk1.8/usr/local/maven3.5/bin/mvn clean package -Dmaven.test.skip=true'''}//maven构建代码stage('deploy') {sh '''JENKINS_NODE_COOKIE=dontkillmeexport JAVA_HOME=/usr/local/jdk1.8TOMCAT_NAME=tomcatTOMCAT_HOME=/usr/local/$TOMCAT_NAMEWWWROOT=$TOMCAT_HOME/webapps/ROOTif [ -d $WWWROOT ]; thenmv $WWWROOT /data/backup/${TOMCAT_NAME}-$(date +"%F_%T")fiunzip ${WORKSPACE}/target/*.war -d $WWWROOTPID=$(ps -ef |grep $TOMCAT_NAME |egrep -v "grep|$$" |awk '{print $2}')[ -n "$PID" ] && kill -9 $PID$TOMCAT_HOME/bin/startup.sh'''// JENKINS_NODE_COOKIE=dontkillme避免jenkins自动关闭衍生的子进程;}stage('test') {sh "/usr/bin/echo 'hello word'"}}
上传pipeline 到git仓库中
[root@jenkins item-b]# git add .[root@jenkins item-b]# git commit -m "test"[master e8aa5c7] test1 file changed, 2 insertions(+), 2 deletions(-)[root@jenkins item-b]# git push origin masterCounting objects: 7, done.Delta compression using up to 2 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (4/4), 372 bytes | 0 bytes/s, done.Total 4 (delta 1), reused 0 (delta 0)To git@192.168.2.51:/home/git/repos/jenkinsfile.gita6391b0..e8aa5c7 master -> master
3、创建任务
![f0c60ce82506dbd204081460cdfafccc.png](https://i-blog.csdnimg.cn/blog_migrate/ea73305d23cf16e2c9f06e1b38bfc5f8.jpeg)
![5d197785af7176a44871d8bebecaf60f.png](https://i-blog.csdnimg.cn/blog_migrate/176c79cd06e0ec9fb72717aa564263a6.jpeg)
添加一个参数化构建过程,选择字符参数
![fd20dc661fcf9b93c20704178beb99db.png](https://i-blog.csdnimg.cn/blog_migrate/f75e3b19bf02cd9f777ca51d8b635947.jpeg)
指定git 仓库中,pepeline 的位置
写明 脚本路径
保存
![84fe5a54bcc8274937471f87b574e109.png](https://i-blog.csdnimg.cn/blog_migrate/c5d0fc74f9d64769b1c1581b081768a1.jpeg)
4、构建任务
![cdfbbe1b2788eb710cd11ffaf8f3ad33.png](https://i-blog.csdnimg.cn/blog_migrate/0970bbabd171b7d6427f1211d6cc4c48.jpeg)
![0e1b85f87322f7fc64ae8a9e1f4f075e.png](https://i-blog.csdnimg.cn/blog_migrate/14bae1a8366b95fa0add4fd791d7ac05.jpeg)
![a3f427c7bb3642b0c62063435cdfd9ad.png](https://i-blog.csdnimg.cn/blog_migrate/549205a1c99929eeb260afcd10220fb1.jpeg)
构建成功,我们来测试下
5、测试
![cc488c1ee600f532ae890383ec23d945.png](https://i-blog.csdnimg.cn/blog_migrate/600bf8e3936f66185a1e70213f4a0892.jpeg)