I want to define multiple stages in Jenkins declarative pipeline syntax which can continue past any one of them failing. I cannot find any existing questions which are true duplicates, because they all assume or allow scripted syntax.
pipeline {
agent any
stages {
stage('stage 1') {
steps {
echo "I need to run every time"
}
}
stage('stage 2') {
steps {
echo "I need to run every time, even if stage 1 fails"
}
}
stage('stage 3') {
steps {
echo "Bonus points if the solution is robust enough to allow me to continue *or* be halted based on previous stage status"
}
}
}
}
To clarify, I'm not looking for how to do accomplish this in scripted syntax. I'm trying to understand if this kind of flow control is actually supported and formalized in declarative syntax. To that end, I'll try to define exactly what I'm looking for:
post step
shenanigans. I want true multiple stages, not one stage with a post always
step that contains all my other logicThis is now possible:
pipeline {
agent any
stages {
stage('1') {
steps {
sh 'exit 0'
}
}
stage('2') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh "exit 1"
}
}
}
stage('3') {
steps {
sh 'exit 0'
}
}
}
}
In the example above, all stages will execute, the pipeline will be successful, but stage 2 will show as failed:
As you might have guessed, you can freely choose the buildResult
and stageResult
, in case you want it to be unstable or anything else. You can even fail the build and continue the execution of the pipeline.
Just make sure your Jenkins is up to date, since this is a fairly new feature.
EDIT: This is the question that this answer was originally written for. It is also the correct answer for a few other questions, which is why I posted this answer there as well. This is the right solution for multiple similar problems. I've tailored my other answers to their specific questions to make that clear. I only copied the answer to save myself some time. That doesn't mean it's not a good correct answer.