在我的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
}
}现在,我在一个特性分支上进行开发,例如只为客户2开发feature/ISSUE-123,所以作业customer-1/ISSUE-123和customer-3/ISSUE-123并不存在。我怎么能告诉詹金斯在这种情况下不要失败?
发布于 2019-10-22 09:28:36
考虑提取一个名为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.
}
}
}
}
} https://stackoverflow.com/questions/58500857
复制相似问题