Quality Management, Testmanagement, Testautomation, Continuous Integration and Delivery, Jenkins, Consulting, Training, Auditing
Skipped Stages in Jenkins Scripted Pipeline | Comquent GmbH, Continuous Quality in Software

Skipped Stages in Jenkins Scripted Pipeline

By Thursday, der 8. February 2018 No Comments

Our Jenkins Pipeline training course is just updated on 2020! – PRESS HERE

In the Stage View and Blue Ocean there is a nice feature in the pipeline visualisation to show skipped stages:

Skipped Stages in Jenkins Scripted Pipeline

Skipped Stages in Jenkins Scripted Pipeline

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:

stage('My Conditional Stage') {
    when {
        branch 'master'
    steps {
        echo 'Do that only on master branch'

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:

static void markStageSkippedForConditional(String stageName) {
    markStageWithTag(stageName, getStageStatusMetadata().tagName, getStageStatusMetadata().skippedForConditional)

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:

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

// .....
stage('I am skipped') {

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:

Skipped Stages in Jenkins Scripted Pipeline

Now you can use when in your scripted pipeline out of the box:

stage('My Conditional Stage') {
    when (BRANCH_NAME != 'master') {
        echo 'Only on master branch.'