In the Stage View and Blue Ocean there is a nice feature in the pipeline visualisation to show skipped stages:
To show all stages at every build even if not executed is a good practice and brings transparency
into pipelines with conditional steps or rather stages. Add the when condition to a stage of your pipeline:
For those who want to benefit from the liberty and compactness of the scripted (imperative) pipeline the situation is not pleasant at all. So let’s wait until some day this feature is implemented for the scripted pipeline.
Wait a minute! Let’s look at the facts and use our phantasy and maybe have some guesses.
- If the when condition restults to false the steps are not executed.
- We can do that be an if clause in scripted pipeline.
- So, there is only missing how this can be told to the Stage View and Blue Ocean.
- Well, most likely it is only one flag set in a stage when it is skipped.
- Claim that we can set this flag also from imperative pipeline.
- If we can do that we are able to wrap this thing in a library function.
Ok, let’s find out, how this assumed flag can be set…
After digging into the source code of the jenkins plugins I found that here:
Restricted for internal use only? I mean in Groovy as far as I know it there is no such thing as privacy. So we can write a test pipeline like that:
That works! The Stage View looks ok, Blue Ocean visualisation also.
So, finally wrap that thing into a nice library function, include the library on Jenkins at master level, enable autoload:
Now you can use when in your scripted pipeline out of the box: