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

Execute alternative step(s) if stage is filtered by a when condition

XMLWordPrintable

      Currently if a when condition filters a stage, there's no way to execute a step in response to that, it just treats it like it never existed. It would be useful to allow something like post.notBuilt (or a new post condition) to execute for a filtered stage (stage.when only - would not make sense in a pipeline.when condition).

      My use case is a pipeline that runs a series of verification stages against a Gerrit change (like a pull request), using the Gerrit Code Review plugin to set the status of one or more "Checks". Not all stages have to run on every change though, so I'd rather be more intelligent about skipping stages that don't apply for a change, and still report that feedback in the change UI.

      pipeline {
          agent any
          stages {
              // other stages
              stage("Style") {
                  when {
                      expression { false }
                      // For example, only if source files changed:
                      changeset '**/src/*/java/**'
                  }
                  steps {
                      // run checkstyle, lint, etc
                  }
                  post {
                      notBuilt {
                          // Set PR stage results to "Not Relevant" to show that it was skipped
                          gerritCheck checks: ["jenkins:style": "NOT_RELEVANT"]
                          // This does not execute because the stage was skipped.
                      }
                      successful {
                          gerritCheck checks: ["jenkins:style": "SUCCESS"]
                      }
                      unsuccessful {
                          gerritCheck checks: ["jenkins:style": "FAILED"]
                      }
                  }
              }
          }
      }
      

      I understand the post.notBuilt block has a different meaning – specifically it executes only if the stage executes and the stage/build result is NOT_BUILT. And that is different from a stage being skipped altogether.

      The only other way I can see to achieve this would be another stage that runs with inverted conditions (results in duplication of the when conditions), or by setting an environment variable when the target stage runs, that is then checked in another stage after it (if not set, it didn't run, so mark it NOT_RELEVANT). These solutions all come across as hacks and overly verbose, when all I really need to do is execute some step when the stage would be filtered.

      Another option could be something like a new when.otherwise block that executes a step in place of the stage, if the conditions failed:

                  when {
                      expression { false }
      
                      otherwise {
                          gerritCheck checks: ["jenkins:style": "NOT_RELEVANT"]
                      }
                  }
      

            Unassigned Unassigned
            jhansche Joe Hansche
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: