Answers: <p>A pretty simple solution is to declare a global variable on top of your pipeline, and check it's value in your last conditional stage:</p>
  
  <pre class="lang-java prettyprint-override"><code>def flag = false;
  
  pipeline {
      stages {
          stage('1 - Always') {
              steps {
                  sh './always.sh'
              }
          }
  
          stage('2 - Maybe') {
              when { condition }
  
              steps {
                  sh './maybe.sh'
                  script { flag = true }
              }
          }
  
          stage('3 - If Maybe was executed') {
              when { expression { flag == true } }
  
              steps {
                  sh './conditional.sh'
              }
          }
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-15T09:33:28.090Z
Answers: 2019-11-15T09:59:44.287Z
Answers: 2019-11-15T09:59:44.287Z
Answers: Hedi Nasr
Body: <p>I have a Jenkins declarative pipeline with several stages. Some of these stages need to be executed <em>based on whether a previous stage has been executed</em>. In pseudocode:</p>
  
  <pre><code>pipeline{
    stages{
      stage('1: Always do this thing') {
        steps { 
          sh 'do_thing.sh'
        } 
      }
      stage('2: Maybe do this thing') {
        when{ condition }
        steps {
          sh 'sometimes_do_this_thing.sh'
        }
      }
      stage('3: Do this thing if 2 was executed'){
        when { pipeline.stages.2.was_executed }
        steps {
          sh 'conditional_path.sh'
        }
      }
    }
  }
  
  </code></pre>
  
  <p>I accept that the best way to do this might be in the scripted pipeline, but I have declarative pipelines. Including some scripted bits in them is also acceptable, if kept to a minimum.</p>
  
  <p>So: <strong>What is the most elegant way to execute a stage in a pipeline conditional on the execution status of the previous one</strong>?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-11-15T09:27:33.670Z
Comments: Could you explain what is going wrong? What I see on a daily basis is that if one uses stage that they are executed sequentially and the next stage will be started if the former has been completed, unless if parallel is enabled.
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-11-15T09:50:44.007Z
Comments: The issue is only that I can't predict whether the condition will be satisfied in Stage 2. I don't want to set up conditions based on the commit or message, but on the state of stages in the pipeline. Sequential execution is fine, but some stages should be skipped in some cases.
Comments: Bruce Becker
Tags: <jenkins><jenkins-pipeline><groovy>
Title: Jenkins Declarative pipeline: Execute stage conditional on execution of previous stage
UserDisplayName: Bruce Becker
ThreadText: # Jenkins Declarative pipeline: Execute stage conditional on execution of previous stage
  
  (Asked by: username_0 on 2019-11-15)
  
  I have a Jenkins declarative pipeline with several stages. Some of these stages need to be executed *based on whether a previous stage has been executed*. In pseudocode:
  
  ```
  pipeline{
    stages{
      stage('1: Always do this thing') {
        steps { 
          sh 'do_thing.sh'
        } 
      }
      stage('2: Maybe do this thing') {
        when{ condition }
        steps {
          sh 'sometimes_do_this_thing.sh'
        }
      }
      stage('3: Do this thing if 2 was executed'){
        when { pipeline.stages.2.was_executed }
        steps {
          sh 'conditional_path.sh'
        }
      }
    }
  }
  
  ```
  
  I accept that the best way to do this might be in the scripted pipeline, but I have declarative pipelines. Including some scripted bits in them is also acceptable, if kept to a minimum.
  
  So: **What is the most elegant way to execute a stage in a pipeline conditional on the execution status of the previous one**?
  
  **Tags:** jenkins, jenkins-pipeline, groovy
  
  > **Comments:**
  > **username_1**: Could you explain what is going wrong? What I see on a daily basis is that if one uses stage that they are executed sequentially and the next stage will be started if the former has been completed, unless if parallel is enabled.  
  > **username_0**: The issue is only that I can't predict whether the condition will be satisfied in Stage 2. I don't want to set up conditions based on the commit or message, but on the state of stages in the pipeline. Sequential execution is fine, but some stages should be skipped in some cases.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-11-15. Score: 7 | Accepted answer
  
  A pretty simple solution is to declare a global variable on top of your pipeline, and check it's value in your last conditional stage:
  
  ```java
  def flag = false;
  
  pipeline {
      stages {
          stage('1 - Always') {
              steps {
                  sh './always.sh'
              }
          }
  
          stage('2 - Maybe') {
              when { condition }
  
              steps {
                  sh './maybe.sh'
                  script { flag = true }
              }
          }
  
          stage('3 - If Maybe was executed') {
              when { expression { flag == true } }
  
              steps {
                  sh './conditional.sh'
              }
          }
      }
  }
  
  ```
  
  
  ---
  
  