Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better visualization of skipped stages in declarative pipeline

I'm looking into moving our scripted pipelines to declarative pipelines.

I'm using the when key word to skip stages

stage('test') {       
     // Only do anything if we are on the master branch
     when { branch 'master' }
     //...
}

This works, however the skipped stage is shown as green. I would prefer if it was shown as gray in the pipeline overview. Is there a way to achieve this?

like image 510
Roman Pickl Avatar asked Mar 23 '17 09:03

Roman Pickl


People also ask

Which is better scripted or declarative pipeline?

The Jenkins scripted pipeline model is recommended for those who have numerous specific requirements for their continuous delivery pipeline. You may also consider leveraging a “best of both worlds” approach, by using declarative pipelines with script() step to run a created scripted pipeline.

How do you skip a declarative pipeline stage?

You can skip stages in declarative pipelines using when , so the following should work. stages { stage('Deploy') { when { equals expected: true, actual: Deploy } steps { // ... } } } If it should be totally invisible in the review pipeline, then use scripted pipelines and wrap the stage with an if statement.

How do you skip stages in Jenkins scripted pipeline?

So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping. If you're using a version of Jenkins older than mid 2019, you must uncheck Use Groovy Sandbox checkbox, since the Utils method was not yet whitelisted for external use.

What is difference between declarative pipeline and script based pipeline in Jenkins?

Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.


2 Answers

If the stage is appearing green for you then it's likely still actually running. A skipped stage should look like this in the Jenkins classic stage view. Consider the following code sample, which has three stages, the middle stage being skipped conditionally with the when directive.

pipeline {
    agent any
    stages {
        stage('Always run 1') {
            steps { echo "hello world" }
        }
        stage('Conditionally run') {
            when {
                expression { return false }
            }
            steps { echo "doesn't get printed" }
        }
        stage("Always run 2") {
            steps { echo "hello world again" }
        }
    }
}

This should produce the following line in your build log

Stage "Conditionally run" skipped due to when conditional

Another answerer of this question mentioned Blue Ocean, which definitely presents a beautiful presentation of the stage view. Here is an image of how a skipped stage looks in the Blue Ocean stage view. Note that Blue Ocean is a UI and your job's underlying pipeline code will be the same regardless of which UI you choose to use.

like image 187
Zach Goodman Avatar answered Oct 16 '22 09:10

Zach Goodman


As you mentioned in your comment I suggest you to use Jenkins Blue Ocean when working with pipelines.

It provides a more modern and user friendly view for your pipeline projects. Even the pipeline itself is display in a much more convenient way.

like image 1
saw303 Avatar answered Oct 16 '22 10:10

saw303