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

Execution of post stage block is based on pipeline status, not stage result which docs says it should

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Blocker Blocker
    • pipeline
    • None

      The declarative pipeline documentation on "post" says the following: "These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage" 

      Currently, the post execution of a stage seems to refer to the whole pipeline status at that point and not the completion status of the stage as documented.

      Given the following pipeline:

      pipeline {
        agent any
        stages {
          stage('First') {
            steps { echo 'Hello' }
            post { success { echo 'First stage is success' } }
          }
          stage('Second') {
            steps { sh """ echo '<testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>' > test-results.xml """ }
            post {
              always { junit 'test-results.xml' }
              unstable { echo 'Second stage is unstable' }
            }
          }
          stage('Third') {
            steps { echo 'Hello' }
            post {
              success { echo 'Third stage is success' }
              unstable { echo 'Third stage is unstable! really???' }
            }
          }
        }
        post { unstable { echo 'Job is unstable' } }
      }
      
      

      I get the following output:

      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (First)
      [Pipeline] echo
      Hello
      Post stage
      [Pipeline] echo
      First stage is success
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Second)
      [Pipeline] sh
      + echo <testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>
      Post stage
      [Pipeline] junit
      Recording test results
      [Pipeline] echo
      Second stage is unstable
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Third)
      [Pipeline] echo
      Hello
      Post stage
      [Pipeline] echo
      Third stage is unstable! really???
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Declarative: Post Actions)
      [Pipeline] echo
      Job is unstable
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      

      Clearly the third stage post unstable block is executed even though the stage itself is a success.

      This is also contradicted by the blue ocean stage view:

          [JENKINS-57801] Execution of post stage block is based on pipeline status, not stage result which docs says it should

          Daniel Beland created issue -

          We're having the exact same issue in our pipeline.

          James Shorthouse added a comment - We're having the exact same issue in our pipeline.

          Marcus Philip added a comment - - edited

          I was about to report this myself.

          The text in https://www.jenkins.io/doc/book/pipeline/syntax/#post :

          These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage. (my emphasis)

          This is not the case. It is the pipeline status at the time of execution of the post block that decides which condition block is executed, not the status of the stage

           

          Here is a simple example that shows the problem:

          import groovy.transform.Field
          @Field List<String> failedStages = []
          pipeline {
              agent { label 'default' }
              stages {
                  stage('Stage One') {
                      steps {
                          unstable 'We are unstable'
                      }
                      post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } }
                  }
                  stage('Stage Two') {
                      steps {
                          echo 'Two is OK'
                      }
                      post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } }
                  }
                  stage('Stage Three') {
                      steps {
                          error 'Oh nos for threes!'
                      }
                      post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } }
                  }
              }
              post {
                  unsuccessful {
                      echo ">> Failed stages: ${failedStages}"
                  }
              }
          }
          

          Expected:

          1. All stages are executed.
          2. Stage One is UNSTABLE, Stage Two is SUCCESS, Stage Three is FAILURE.
          3. Pipeline is FAILURE.
          4. failedStages contains Stage One and Stage Three.

           

          Actual:

          • 1-3 is as expected
          • But 4) is not, failedStages contains: Stage One, Stage Two, Stage Three

           

          Although I know of no 'external' concept (i.e. env var) of the stage result, evidently it exists since the stages have the correct color in the GUI in my example pipeline*.*

           

          Marcus Philip added a comment - - edited I was about to report this myself. The text in https://www.jenkins.io/doc/book/pipeline/syntax/#post  : These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage . (my emphasis) This is not the case. It is the pipeline status at the time of execution of the post block that decides which condition block is executed, not the status of the  stage .    Here is a simple example that shows the problem: import groovy.transform.Field @Field List< String > failedStages = [] pipeline { agent { label ' default ' } stages { stage( 'Stage One' ) { steps { unstable 'We are unstable' } post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } } } stage( 'Stage Two' ) { steps { echo 'Two is OK' } post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } } } stage( 'Stage Three' ) { steps { error 'Oh nos for threes!' } post { unsuccessful { script { failedStages.add(env.STAGE_NAME) } } } } } post { unsuccessful { echo ">> Failed stages: ${failedStages}" } } } Expected: All stages are executed. Stage One is UNSTABLE, Stage Two is SUCCESS, Stage Three is FAILURE. Pipeline is FAILURE. failedStages contains Stage One and Stage Three.   Actual: 1-3 is as expected But 4) is not, failedStages contains: Stage One, Stage Two, Stage Three   Although I know of no 'external' concept (i.e. env var) of the stage result, evidently it exists since the stages have the correct color in the GUI in my example pipeline*.*  
          Marcus Philip made changes -
          Summary Original: Execution of the wrong post stage block New: Execution post stage block is based on pipeline status, not stage result
          Marcus Philip made changes -
          Summary Original: Execution post stage block is based on pipeline status, not stage result New: Execution of post stage block is based on pipeline status, not stage result
          Marcus Philip made changes -
          Description Original: The declarative pipeline documentation on "post" says the following: "These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage"

           

          Currently, the post execution of a stage seems to refer to the whole pipeline status at that point and not the completion status of the stage as documented.

           

          Given the following pipeline:
          {code:java}
          pipeline {
            agent any
            stages {
              stage('First') {
                steps { echo 'Hello' }
                post { success { echo 'First stage is success' } }
              }
              stage('Second') {
                steps { sh """ echo '<testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>' > test-results.xml """ }
                post {
                  always { junit 'test-results.xml' }
                  unstable { echo 'Second stage is unstable' }
                }
              }
              stage('Third') {
                steps { echo 'Hello' }
                post {
                  success { echo 'Third stage is success' }
                  unstable { echo 'Third stage is unstable! really???' }
                }
              }
            }
            post { unstable { echo 'Job is unstable' } }
          }

          {code}
          I get the following output:
          {noformat}
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          First stage is success
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] sh
          + echo <testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>
          Post stage
          [Pipeline] junit
          Recording test results
          [Pipeline] echo
          Second stage is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Third)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          Third stage is unstable! really???
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Declarative: Post Actions)
          [Pipeline] echo
          Job is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          {noformat}
          Clearly the third stage post unstable block is executed even though the stage itself is a success.

          This is also contradicted by the blue ocean stage view:

           

          !image-2019-05-31-14-26-08-781.png!

           

           
          New: The declarative pipeline documentation on "post" says the following: "These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage" 

          Currently, the post execution of a stage seems to refer to the whole pipeline status at that point and not the completion status of the stage as documented.

          Given the following pipeline:
          {code:java}
          pipeline {
            agent any
            stages {
              stage('First') {
                steps { echo 'Hello' }
                post { success { echo 'First stage is success' } }
              }
              stage('Second') {
                steps { sh """ echo '<testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>' > test-results.xml """ }
                post {
                  always { junit 'test-results.xml' }
                  unstable { echo 'Second stage is unstable' }
                }
              }
              stage('Third') {
                steps { echo 'Hello' }
                post {
                  success { echo 'Third stage is success' }
                  unstable { echo 'Third stage is unstable! really???' }
                }
              }
            }
            post { unstable { echo 'Job is unstable' } }
          }

          {code}
          I get the following output:
          {noformat}
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          First stage is success
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] sh
          + echo <testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>
          Post stage
          [Pipeline] junit
          Recording test results
          [Pipeline] echo
          Second stage is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Third)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          Third stage is unstable! really???
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Declarative: Post Actions)
          [Pipeline] echo
          Job is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          {noformat}
          Clearly the third stage post unstable block is executed even though the stage itself is a success.

          This is also contradicted by the blue ocean stage view:

           

          !image-2019-05-31-14-26-08-781.png!

           

           
          Marcus Philip made changes -
          Description Original: The declarative pipeline documentation on "post" says the following: "These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage" 

          Currently, the post execution of a stage seems to refer to the whole pipeline status at that point and not the completion status of the stage as documented.

          Given the following pipeline:
          {code:java}
          pipeline {
            agent any
            stages {
              stage('First') {
                steps { echo 'Hello' }
                post { success { echo 'First stage is success' } }
              }
              stage('Second') {
                steps { sh """ echo '<testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>' > test-results.xml """ }
                post {
                  always { junit 'test-results.xml' }
                  unstable { echo 'Second stage is unstable' }
                }
              }
              stage('Third') {
                steps { echo 'Hello' }
                post {
                  success { echo 'Third stage is success' }
                  unstable { echo 'Third stage is unstable! really???' }
                }
              }
            }
            post { unstable { echo 'Job is unstable' } }
          }

          {code}
          I get the following output:
          {noformat}
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          First stage is success
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] sh
          + echo <testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>
          Post stage
          [Pipeline] junit
          Recording test results
          [Pipeline] echo
          Second stage is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Third)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          Third stage is unstable! really???
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Declarative: Post Actions)
          [Pipeline] echo
          Job is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          {noformat}
          Clearly the third stage post unstable block is executed even though the stage itself is a success.

          This is also contradicted by the blue ocean stage view:

           

          !image-2019-05-31-14-26-08-781.png!

           

           
          New: The declarative pipeline documentation on "post" says the following: "These condition blocks allow the execution of steps inside each condition depending on the completion status of the Pipeline or stage" 

          Currently, the post execution of a stage seems to refer to the whole pipeline status at that point and not the completion status of the stage as documented.

          Given the following pipeline:
          {code:java}
          pipeline {
            agent any
            stages {
              stage('First') {
                steps { echo 'Hello' }
                post { success { echo 'First stage is success' } }
              }
              stage('Second') {
                steps { sh """ echo '<testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>' > test-results.xml """ }
                post {
                  always { junit 'test-results.xml' }
                  unstable { echo 'Second stage is unstable' }
                }
              }
              stage('Third') {
                steps { echo 'Hello' }
                post {
                  success { echo 'Third stage is success' }
                  unstable { echo 'Third stage is unstable! really???' }
                }
              }
            }
            post { unstable { echo 'Job is unstable' } }
          }

          {code}
          I get the following output:
          {noformat}
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          First stage is success
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] sh
          + echo <testsuite errors="1" failures="0" name="test" skipped="0" tests="1" time="1"><testcase classname="" file="file.txt" name="name" time="0.000"><error message="error"></error></testcase></testsuite>
          Post stage
          [Pipeline] junit
          Recording test results
          [Pipeline] echo
          Second stage is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Third)
          [Pipeline] echo
          Hello
          Post stage
          [Pipeline] echo
          Third stage is unstable! really???
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Declarative: Post Actions)
          [Pipeline] echo
          Job is unstable
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          {noformat}
          Clearly the third stage post unstable block is executed even though the stage itself is a success.

          This is also contradicted by the blue ocean stage view:

          !image-2019-05-31-14-26-08-781.png!
          Marcus Philip made changes -
          Link New: This issue is duplicated by JENKINS-61405 [ JENKINS-61405 ]
          Marcus Philip made changes -
          Summary Original: Execution of post stage block is based on pipeline status, not stage result New: Execution of post stage block is based on pipeline status, not stage result which docs says it should

          Marcus Philip added a comment -

          In JENKINS-37792 the stage post block functionality was introduced. Judging from the lack of tests of this in https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/30 I'm guessing it never worked.

          Marcus Philip added a comment - In JENKINS-37792 the stage post block functionality was introduced. Judging from the lack of tests of this in https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/30  I'm guessing it never worked.
          Marcus Philip made changes -
          Link New: This issue is caused by JENKINS-37792 [ JENKINS-37792 ]

            Unassigned Unassigned
            dcendents Daniel Beland
            Votes:
            10 Vote for this issue
            Watchers:
            14 Start watching this issue

              Created:
              Updated: