1 原流水线
#!groovy
@Library('jenkinslib') _
def tools = new org.devops.tools()
def build = new org.devops.build()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
pipeline {
agent {
node {
label "built-in"
}
}
environment {
String GsrcUrl="${env.srcUrl}"
String GbranchName="${env.branchName}"
String GbuildType="${env.buildType}"
String GbuildShell="${env.buildShell}"
}
stages {
stage('CheckOut') {
steps {
script {
if ("$runOpts" == "GitlabPush"){
GbranchName = branch - 'refs/heads/'
currentBuild.description = "Trigger by user ${userName} \n branch: ${GbranchName}"
gitlab.ChangeCommitStatus(projectId,commitSha,"running")
}
tools.PrintMes("获取代码",'green')
checkout scmGit(branches: [[name: "${GbranchName}"]],
extensions: [cleanBeforeCheckout()],
userRemoteConfigs: [[credentialsId: '8c8a0774-7d4c-48a9-8f52-dd1a04f71320',
url: "${GsrcUrl}"]])
}
}
}
stage("Build") {
steps {
script {
tools.PrintMes("执行打包",'green')
build.Build(GbuildType,GbuildShell)
}
}
}
}
post {
always {
script {
println("always")
}
}
success {
script {
println("success")
gitlab.ChangeCommitStatus(projectId,commitSha,"success")
toemail.Email("流水线成功了",userEmail)
}
}
failure {
script {
println("failure")
gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
toemail.Email("流水线失败了",userEmail)
}
}
aborted {
script {
println("aborted")
gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
toemail.Email("流水线被取消了",userEmail)
}
}
}
}
2 流水线优化
2.1 概述
之前设置了一堆触发器参数
使用$可以获取输出json
2.2 参数优化
可以将branch、userName、projectId、commitSha这四个变量从触发器post参数里面删掉,从allData里面提取出来
def WebHookData = readJSON text : allData
String branch=WebHookData.ref
String userName=WebHookData.user_username
String projectId=WebHookData.project.id
String commitSha=WebHookData.checkout_sha
放在pipeline上面,千万不要放在pipeline里面