问题
I'm trying to build a job from my Jenkins pipeline like this:
build job:"${jobName}", propagate:false, wait:false
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.
回答1:
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")
}
来源:https://stackoverflow.com/questions/52054417/how-to-build-a-jenkins-job-from-pipeline-ignoring-if-the-job-doesnt-exist