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

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      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:

            Assignee:
            Unassigned
            Reporter:
            Daniel Beland
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Archived: