jenkins 使用pipline/Jenkinsfile发布项目
一、jenkins发布项目的各种方式原理都是差不多一样,pipline只是将普通的页面操作改成了类似代码的方式
二、具体执行步骤:
1.在运行jar的远程服务器,创建一个目录 /app/demo_pipline 用于接收jenkins服务器发过来的jar包
2.在运行jar的远程服务器的登录用户的家目录,创建一个目录 pipline,并且在该目录下,存放pre steps和post steps相关的shell 脚本:pipline_clean.sh 和 pipline_start.sh
pipline_clean.sh 如下:
#!/bin/bash #删除旧的jar包 echo "开始执行了">ak.txt rm -rf /app/demo_pipline/demo_mybatis*.jar #判断就的java进程是否存在,存在则kill掉 isServerExist=`ps -ef|grep java |grep -v "grep"| grep demo_mybatis | wc -l` if [ "$isServerExist" = "0" ]; then echo "my_server not found" # exit; elif [ "$isServerExist" = "1" ]; then pid=`ps -ef|grep java |grep -v "grep"| grep demo_mybatis | awk '{print $2}'` kill $pid fi echo "结束啦啦">bbk.txt sleep 1s
pipline_start.sh 如下:
#!/bin/bash echo "开始发包">start.txt nohup java -jar /app/demo_pipline/demo_mybatis-*-SNAPSHOT.jar & echo "发包成功">finish.txt sleep 1s
2.jenkins的配置:
a.先看build的功能,通过git拉取代码并且打成jar包
b.配置pre steps步骤:
c.配置post steps
最终的pipline脚本:
pipeline { agent any tools { // Install the Maven version configured as "M3" and add it to the path. maven "maven" } stages { stage('Build') { steps { // Get some code from a GitHub repository git credentialsId: 'gitee_user_password', url: 'https://gitee.com/yangxioahui/demo_mybatis.git' // Run Maven on a Unix agent. sh "mvn -Dmaven.test.failure.ignore=true clean package" // To run Maven on a Windows agent, use // bat "mvn -Dmaven.test.failure.ignore=true clean package" } } stage('pre steps') { steps { sshPublisher(publishers: [sshPublisherDesc(configName: 'vagrant_10', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'sh pipline/pipline_clean.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } stage('post steps') { steps { sshPublisher(publishers: [sshPublisherDesc(configName: 'vagrant_10', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'nohup sh pipline/pipline_start.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/demo_pipline', remoteDirectorySDF: false, removePrefix: 'target/', sourceFiles: 'target/demo_mybatis-*-SNAPSHOT.jar', usePty: true)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) } } } }
上面的pipline脚本就是Jenkinsfile
启动结果:
三、使用Jenkinsfile来发布项目,将上面的pipline脚本写成Jenkinsfile
1. 将Jenkinsfile放到java项目,并且上传到git
2. 下载blue ocean插件
之后启动: