Reputation: 937
In my Jenkins I have a Groovy pipeline script which triggers multiple jobs afterward:
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
}
}
Now, I develop on a feature-branch e.g. feature/ISSUE-123
just for customer 2, so the jobs customer-1/ISSUE-123
and customer-3/ISSUE-123
do not exist. How can I tell Jenkins not to fail in this case?
Upvotes: 1
Views: 266
Reputation: 42184
Consider extracting a new method called safeTriggerJob
that wraps the build
step with the try-catch block that catches exception thus let the pipeline continue running.
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}"
}
}
Output:
[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
Alternatively, instead of extracting a dedicated method you could add try-catch directly inside steps
block, but in this case, you would need to wrap it with script
, something like:
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.
}
}
}
}
}
Upvotes: 1