在Jenkins中,我有一个Groovy管道脚本,该脚本随后会触发多个作业:
stage('Build other pipelines') {
steps {
build job: "customer-1/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
build job: "customer-2/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
build job: "customer-3/${URLEncoder.encode(BRANCH_NAME, "UTF-8")}", propagate: true, wait: false
}
}
现在,我在一个功能分支上进行开发,例如feature/ISSUE-123
仅针对客户2,因此作业customer-1/ISSUE-123
和customer-3/ISSUE-123
不存在。在这种情况下,我如何告诉詹金斯不要失败?
答案 0 :(得分:1)
考虑提取一个名为safeTriggerJob
的新方法,该方法将build
步骤与try-catch块一起包装,该块捕获异常,从而使管道继续运行。
pipeline {
agent any
stages {
stage("Test") {
steps {
safeTriggerJob job: "job2", propagate: true, wait: false
}
}
}
}
void safeTriggerJob(Map params) {
try {
build(params)
} catch (Exception e) {
echo "WARNING: ${e.message}"
}
}
输出:
[Pipeline] Start of Pipeline (hide)
[Pipeline] node
Running on Jenkins in /home/wololock/.jenkins/workspace/sandbox-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] build
[Pipeline] echo
WARNING: No item named job2 found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
或者,您可以直接在steps
块内添加try-catch,而不是提取专用方法,但是在这种情况下,您需要使用script
将其包装,例如:
pipeline {
agent any
stages {
stage("Test") {
steps {
script {
try {
build job: "job2", propagate: true, wait: false
} catch (Exception e) {
echo "WARNING: ${e.message}"
}
// The next build inside its own try-catch here, etc.
}
}
}
}
}