Reputation: 11
I have a Jenkins pipeline with multiple stages. These stages are executed on separate worker nodes. Currently when I abort a pipeline the subsequent stages are all aborted and the build status is set to aborted.
I would like to develop the pipeline such that I can abort a stage without aborting all subsequent stages.
Below is a sample build log of a pipeline with two stages: Stage A and Stage B. As you can see when I aborted Stage A Jenkins skips Stage B. I would like Jenkins to execute Stage B when Stage A is aborted
[Pipeline] {
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] echo
Started stage A
[Pipeline] sleep
Sleeping for 20 sec
Aborted by anonymous
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
Stage "B" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
Upvotes: 1
Views: 2361
Reputation: 3824
Your question isn't very clear. My understanding is that you wish to structure you pipeline such that if Stage A is aborted it will still run Stage B. To do this you need to use the catchError step. Sample code is included below
The catchError step behaves differently than a try/catch block so please read the linked documentation
pipeline {
agent any
stages {
stage("A") {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'ABORTED') {
echo "Started stage A"
sleep(time: 20, unit: "SECONDS")
}
}
}
stage("B") {
steps {
echo "Started stage B"
sleep(time: 20, unit: "SECONDS")
}
}
}
}
The following build log illustrates me aborting the pipeline in Stage A, but Jenkins continues onward past catchError and executes Stage B.
Running on Jenkins in /var/jenkins_home/workspace/Testing/sleep-abort-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (A)
[Pipeline] catchError
[Pipeline] {
[Pipeline] echo
Started stage A
[Pipeline] sleep
Sleeping for 20 sec
Aborted by Chris Maggiulli
[Pipeline] }
[Pipeline] // catchError
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (B)
[Pipeline] echo
Started stage B
[Pipeline] sleep
Sleeping for 20 sec
Click here to forcibly terminate running steps
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
Upvotes: 1