jenkins_pipeline实例_java
1 pipeline { 2 agent any 3 environment { 4 5 project_name = 'PhoenixTree' #PhoneixTree 自定义的项目名 6 } 7 stages { 8 stage ('拉取代码、打包') { 9 steps { 10 11 #这里git拉代码,在pipeline表单下面有个 pipeline 语法的连接,自己生成 12 git credentialsId: 'git_key', url: 'ssh/https_gitdir' 13 14 #这里使用sh后可以直接执行linux命令 15 sh ''' 16 CODE_DIR=/var/jenkins_home/workspace/Deploy-PhoenixTree-Testing/ 17 mvn clean install -Dmaven.test.skip=true 18 cd ${CODE_DIR}/phoenix-tree-api/target 19 mv phoenix-tree-api-0.0.1.war phoenix-tree-api-${BUILD_NUMBER}.war 20 ''' 21 } 22 } 23 24 stage ('项目项目部署'){ 25 steps { 26 sshPublisher( 27 publishers: [ 28 29 #这里需要现在jenkins中配置服务器之后才能连接,后面和正常手动上线过程类似 30 sshPublisherDesc(configName: 'ALIYUN_IP', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''source /etc/profile 31 cd ~/release/ 32 let "delete_count=`ls -l project.war | wc -l` -5" 33 34 if [ ${delete_count} -gt 0 ]; then 35 ls -tR project*.war | tail -n ${delete_count} | xargs rm -f 36 fi 37 38 rm project.war 39 cp project-${BUILD_NUMBER}.war project.war 40 cd /usr/local/tomcat-project 41 42 STATE=$(ps aux | grep tomcat-project| grep -v grep | wc -l) 43 if [ $STATE -gt 0 ]; then 44 ps aux | grep tomcat-project| grep -v grep | awk \'{print $2}\' | xargs kill -9 45 echo "tomcat kill success!" 46 else 47 echo "tomcat $BASE_DIR pid not exsit" 48 fi 49 sleep 3 50 cp ~/release/project.war /usr/local/tomcat-project/webapps/ 51 ./bin/startup.sh 52 chown -R test.testwork/Catalina/localhost/project/ 53 54 #下面这行命令(execTimeout)也是在jenkins中pipeline插件生成 55 ps -ef|grep tomcat''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'release', remoteDirectorySDF: false, removePrefix: 'phoenix-tree-api/target/', sourceFiles: "phoenix-tree-api/target/phoenix-tree-api-${BUILD_NUMBER}.war")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false) 56 ] 57 ) 58 } 59 } 60 } 61 62 #下面的部分是pipeline发版bearychat报警的 63 64 post { 65 unstable { 66 bearychatSend color: 'red', message: "${env.JOB_NAME},构建不稳定" 67 } 68 failure { 69 bearychatSend color: 'red', message: "${env.JOB_NAME},构建失败" 70 } 71 } 72 }