Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-47286

Support skipping stages in scripted pipelines for nice visualization in blue ocean and classic UI stage view

      Created as response to comments of https://issues.jenkins-ci.org/browse/JENKINS-37781 as the visualization of skipped stages is very, very nice for declarative pipelines in Blue Ocean.

      1. Allow to skip stages in scripted pipelines leading to equally nice visualization in Blue Ocean: The current approaches mentioned by mkobit in https://issues.jenkins-ci.org/browse/JENKINS-37781?focusedCommentId=294965&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-294965 and jamesdumay in https://issues.jenkins-ci.org/browse/JENKINS-37781?focusedCommentId=294966&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-294966 lead to either misleading or just less obvious visualization...
      2. Improve visualization of stage view: instead of showing skipped stages (declarative pipelines) as always being green and allegedly executed, make them e.g. gray.

          [JENKINS-47286] Support skipping stages in scripted pipelines for nice visualization in blue ocean and classic UI stage view

          Chris Maes added a comment -

          This issue is not solved. The skipping of stages in blue ocean view is well supported in Pipeline syntax, but not in scripted pipelines.

          The workaround proposed in https://github.com/comquent/imperative-when works correctly, but since I am using https://github.com/jenkinsci/JenkinsPipelineUnit it seems quite impossible for me to get my unit-tests working.

          Chris Maes added a comment - This issue is not solved. The skipping of stages in blue ocean view is well supported in Pipeline syntax, but not in scripted pipelines. The workaround proposed in https://github.com/comquent/imperative-when  works correctly, but since I am using https://github.com/jenkinsci/JenkinsPipelineUnit  it seems quite impossible for me to get my unit-tests working.

          This morning I was positively surprised that according to BlueOcean change log (https://plugins.jenkins.io/blueocean) this issue was solved:

          Change log
          1.11.0 (Feb 13, 2019)

          ...
          JENKINS-47286 Support skipping stages in scripted pipelines for nice visualization in blue ocean and classic UI stage view
          ...

          ... "only" to find out that it is still in status reopened, i.e. unresolved and that "only" one aspect was solved: namely the missing translation (https://github.com/jenkinsci/blueocean-plugin/commit/b0bf45c64afbf511971c7e79ff2dd317f1bddfc8) – of course I am happy/grateful about that too.

          However, this is a bit misleading

          Reinhold Füreder added a comment - This morning I was positively surprised that according to BlueOcean change log ( https://plugins.jenkins.io/blueocean) this issue was solved: Change log 1.11.0 (Feb 13, 2019) ... JENKINS-47286 Support skipping stages in scripted pipelines for nice visualization in blue ocean and classic UI stage view ... ... "only" to find out that it is still in status reopened, i.e. unresolved and that "only" one aspect was solved: namely the missing translation ( https://github.com/jenkinsci/blueocean-plugin/commit/b0bf45c64afbf511971c7e79ff2dd317f1bddfc8 ) – of course I am happy/grateful about that too. However, this is a bit misleading

          Cameron Taggart added a comment - - edited

          How do we use the workaround at https://github.com/comquent/imperative-when ? It does not appear to be a plugin that we can load. I'm a bit confused on how to use it. Any tips are appreciated. It would be great if this came out of the box. I'm migrating a declarative Jenkinsfile and got stuck on this.

          Cameron Taggart added a comment - - edited How do we use the workaround at https://github.com/comquent/imperative-when ? It does not appear to be a plugin that we can load. I'm a bit confused on how to use it. Any tips are appreciated. It would be great if this came out of the box. I'm migrating a declarative Jenkinsfile and got stuck on this.

          Adding it as a Global Shared Library is documented here. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries
          It is working for me.

          Cameron Taggart added a comment - Adding it as a Global Shared Library is documented here. https://jenkins.io/doc/book/pipeline/shared-libraries/#global-shared-libraries It is working for me.

          Mihai Dinu added a comment - - edited

          There's a nice post here: **https://comquent.de/en/skipped-stages-in-jenkins-scripted-pipeline/ that describes how to use the https://github.com/comquent/imperative-when workaround. 

          I have a related question though: is it possible to show the stages that follow a failed stage as skipped (or failed)? My use case is something like this

          node {
              try {
                  stage('Will fail') {
                      sh 'exit 1'
                  }
                  stage('Some operations') {
                      echo 'This stage should run when previous stage is successful'
                      echo 'Should be skipped or failed when previous stage fails'
                  }
              } 
              catch (err) {
                  echo "Print the error from the first stage $err"
              }
              finally {
                  stage('Post build') {
                      echo 'Always do some cleanup ops'
                  }
              }
          }
          
          
          

           The problem that I'm facing is that any stage from my pipeline can fail and I want all my downstream stages to be skipped, except the post build one. When a stage fails, it is caught in the catch block and so all downstream stages are ignored, ruining the pipeline stage view from the job UI. 

          Any possible solutions for this?

          Mihai Dinu added a comment - - edited There's a nice post here: ** https://comquent.de/en/skipped-stages-in-jenkins-scripted-pipeline/  that describes how to use the https://github.com/comquent/imperative-when  workaround.  I have a related question though: is it possible to show the stages that follow a failed stage as skipped (or failed)? My use case is something like this node { try { stage( 'Will fail' ) { sh 'exit 1' } stage( 'Some operations' ) { echo 'This stage should run when previous stage is successful' echo 'Should be skipped or failed when previous stage fails' } } catch (err) { echo "Print the error from the first stage $err" } finally { stage( 'Post build' ) { echo 'Always do some cleanup ops' } } }  The problem that I'm facing is that any stage from my pipeline can fail and I want all my downstream stages to be skipped, except the post build one. When a stage fails, it is caught in the catch block and so all downstream stages are ignored, ruining the pipeline stage view from the job UI.  Any possible solutions for this?

          While I am not sure if this is the correct/perfect/recommended place for this question, it admittedly fits to this issue and I can actually give you a little hint

          My approach for this is to have a skipableStage step in my shared Jenkins pipeline library that wraps the default/standard stage step, so the usage is almost like normally:

          ...
          acmeSharedLibrary.skipableStage("stage name") {
             ... // normal stage implementation
          }
          ...
          

          ... and the steps implementation in pseudo code is:

          stage(stageName) {
            if (currentBuild.result) {
              echo "Skipping stage '${stageName}' due to build result '${currentBuild.result}'..."
              Result result = Result.fromString(currentBuild.result)
              markStageSkippedForResult(stageName, result) // via the aformentioned workaround
            } else {
              body() // which is the closure block of this "skippableStage"
            }
          }
          

          Reinhold Füreder added a comment - While I am not sure if this is the correct/perfect/recommended place for this question, it admittedly fits to this issue and I can actually give you a little hint My approach for this is to have a skipableStage step in my shared Jenkins pipeline library that wraps the default/standard stage step, so the usage is almost like normally: ... acmeSharedLibrary.skipableStage( "stage name" ) { ... // normal stage implementation } ... ... and the steps implementation in pseudo code is: stage(stageName) { if (currentBuild.result) { echo "Skipping stage '${stageName}' due to build result '${currentBuild.result}' ..." Result result = Result.fromString(currentBuild.result) markStageSkippedForResult(stageName, result) // via the aformentioned workaround } else { body() // which is the closure block of this "skippableStage" } }

          Michael Neale added a comment -

          (can't leave this unassigned for some reason). 

          Michael Neale added a comment - (can't leave this unassigned for some reason). 

          michaelneale I am a bit surprised by being the new assignee of this issue, as I am not sure what you are expecting from me. Considering the admittedly rather long history of this issue and its distractions/deviations I think I can only try to do a little update:

          Reinhold Füreder added a comment - michaelneale I am a bit surprised by being the new assignee of this issue, as I am not sure what you are expecting from me. Considering the admittedly rather long history of this issue and its distractions/deviations I think I can only try to do a little update: the issue's title/goal is still completely the same and correct and IMHO the "only" (or main) thing certainly missing is a stable/public API for org.jenkinsci.plugins.pipeline.modeldefinition.Utils.markStageSkippedForConditional(STAGE_NAME) (as this is annotated with @Restricted(NoExternalUse.class) ) In the course of doing that other questions from https://issues.jenkins-ci.org/browse/JENKINS-47286?focusedCommentId=334032&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-334032 might be answered as well... (I think item #2 "mouse hover" was actually fixed; see https://issues.jenkins-ci.org/browse/JENKINS-47286?focusedCommentId=360479&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-360479 )

          Reinhold Füreder added a comment - - edited

          Assigning this back to michaelneale with the update/summary comment above...

          Reinhold Füreder added a comment - - edited Assigning this back to michaelneale with the update/summary comment above...

          Michael Neale added a comment -

          can't seem to unassign it. 

           

          Michael Neale added a comment - can't seem to unassign it.   

            Unassigned Unassigned
            reinholdfuereder Reinhold Füreder
            Votes:
            10 Vote for this issue
            Watchers:
            42 Start watching this issue

              Created:
              Updated:
              Resolved: