如何通过管道忽略作业是否存在来构建Jenkins作业? [英] How to build a Jenkins job from pipeline ignoring if the job doesn't exist?
问题描述
我正试图从我的Jenkins管道中建立工作,像这样:
I'm trying to build a job from my Jenkins pipeline like this:
build job:"${jobName}", propagate:false, wait:false
这里$ {jobName}是一个多分支管道作业,因此有时在我的工作流程中可能不存在.
Here ${jobName} is a multi branch pipeline job and as such may sometimes not exist in my workflow.
如果作业不存在,此步骤会将我的构建标记为失败. 如果工作不存在,是否有办法简单地忽略并继续前进?
This step marks my build as failed if the job doesn't exist. Is there a way to simply ignore and move on if the job doesn't exist?
我试图像这样检查给定的工作是否存在:
I tried to check whether the given job exist or not like this:
if(jenkins.model.Jenkins.instance.getItem("${jobName}") != null) {
println("Preparing to build the ${jobName}...")
build job:"${jobName}", propagate:false, wait:false
} else {
println("Not building the job ${jobName} as it doesn't exist")
}
但是,有时这会失败(由于脚本安全?). 有更好的方法吗? 我需要做的就是仅在存在的情况下建立工作.
However, this at times fails (due to script security?). Is there a better way of doing this? All I need is to build a job only if it exists.
推荐答案
如果使用脚本化管道,则可以添加try-catch块:
If you use a scripted pipeline, you can add a try-catch block:
try {
println("Preparing to build the ${jobName}...")
build job:"${jobName}", propagate:false, wait:false
} catch (NullPointerException e) {
println("Not building the job ${jobName} as it doesn't exist")
}
这篇关于如何通过管道忽略作业是否存在来构建Jenkins作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!