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:
Required
- No try/catch. I don't want to drop down into scripted mode, or "wrap" my declarative pipeline in another shared library or scripted block.
- No
post step
shenanigans. I want true multiple stages, not one stage with apost always
step that contains all my other logic
Optional
- The failing stage should be recognized as failed; I don't want a failed stage showing up as green because it was "skipped" or "continued".
- A build with any failed stage should be marked as red (or yellow, or anything that is not green).
I may be missing something, but the idea of declarative, opinionated pipeline is to provide coverage of most simple use cases. The moment you need something the opinionated hasn't covered, you HAVE to resort to scripted pipeline, this is only referring to the "requirement" of "declarative pipeline": not going to happen now.
As to your other "requirements", they make very little sense, since the whole idea is to wrap low-level ugliness into shared libraries providing users with constructs like:
Naturally, you would have to find or implement such
mylib
class and thefailable_stages
would get a closure, and wrap it in various plumbing/boilerplate code pieces.Hope this is helpful.
I think it depends on how dependent the jobs are on each other. Derived from your example I would assume that
So the correpsonding pipeline could be
stage 1 and stage 2 will always run, stage 3 reacts on their combined success/failure.
Additional thought: This concept only works 'at the end' of your pipeline. In case you need it somewhere in the middle AND the build has to continue, you could move it into an own job and use the
build job
plugin.