jenkins pipeline样例
1. 前后端代码在不同的repo里,比较容易
2. 前后端代码在同一repo里,样板pipeline script
pipeline { agent any environment { gitRepo = "http://gitlab/project.git" NODE_BIN = "/usr/local/nodejs12/bin" } parameters { choice choices: ['', 'dev', 'master'], description: 'The parameter <repoBranch> lists all git branches', name: 'repoBranch' choice choices: ['', 'frontend', 'backend'], description: 'The parameter <funChoice> lists functions', name: 'funChoice' } triggers { GenericTrigger( causeString: 'Generic Trigger on $ref', genericVariables: [[defaultValue: '', key: 'ref', regexpFilter: '', value: '$.ref']], printContributedVariables: true, printPostContent: true , token: '267fb390274cee0a938dfc341e9294a3', tokenCredentialId: '') } tools { jdk 'jdk1.8' git 'git' maven 'maven3.5.4' } stages { stage('Git Branch') { steps { script { remoteTomcatPort = 8075 if ("$params.repoBranch" != "") { gitBranch = "$params.repoBranch" println ">>> Handle Trigger on <${gitBranch}>" } else { if ("$ref" != "") { gitBranch = "$ref" - "refs/heads/" println ">>> Webhook Trigger on <${gitBranch}>" } } println ">>> Port for remote tomcat: <$remoteTomcatPort>" } } } stage('CheckOut') { steps { script { println ">>> Checkout code from <$gitBranch>" checkout([$class: 'GitSCM', branches: [[name: "$gitBranch"]], extensions: [[$class: 'CleanBeforeCheckout']], userRemoteConfigs: [[credentialsId: 'fa9dc774-acca-491c-9413-7f7ad67926eb', url: "$env.gitRepo"]]]) sh 'git rev-parse --short HEAD' } } } stage('Backend Maven Build') { when { anyOf { changeset "project-service/**" expression { return params.funChoice != "frontend" } } } steps { script { println ">>> Maven build" sh """ pwd cd project-service mvn clean package -Dmaven.test.skip=true """ } } } stage('Frontend Npm Build') { when { anyOf { changeset "project-vue/**" expression { return params.funChoice != "backend" } } } steps { script { println ">>> Npm build" sh """ pwd cd project-vue #rm -rf node_modules #rm parkage-lock.json ${env.NODE_BIN}/npm cache clear --force ${env.NODE_BIN}/npm install --unsafe-perm=true --allow-root ${env.NODE_BIN}/npm run build """ } } } stage('Backend Deploy') { when { anyOf { changeset "project-service/**" expression { return params.funChoice != "frontend" } } } steps { script { def remote = [:] remote.name = "Dev Server" remote.host = "172.0.0.1" remote.allowAnyHosts = true withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-dev', keyFileVariable: 'identity', usernameVariable: 'userName')]) { remote.user = userName remote.identityFile = identity sshCommand remote: remote, command: "sh ~/script/project-service-build.sh $env.WORKSPACE tomcat-supervise $remoteTomcatPort" } } } } stage('Frontend Deploy') { when { anyOf { changeset "project-vue/**" expression { return params.funChoice != "backend" } } } steps { script { def remote = [:] remote.name = "Dev Server" remote.host = "172.0.0.1" remote.allowAnyHosts = true withCredentials([sshUserPrivateKey(credentialsId: 'jenkins-tj-dev', keyFileVariable: 'identity', usernameVariable: 'userName')]) { remote.user = userName remote.identityFile = identity sshCommand remote: remote, command: "sh ~/script/project-frontend-build.sh $env.WORKSPACE tomcat-supervise" } } } } } }
清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己
-- 共勉