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

Stage post conditions doomed by build result becoming unstable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • None
    • Jenkins ver. 2.176.3
      Pipeline: Declarative 1.3.9

      In a pipeline with multiple sequential stages that typically have either an unstable or a success result, stage post conditions can't be used to take any meaningful action, because one unstable stage dooms the subsequent stages to fire the unsuccessful post condition instead of the success post condition.

      Pipeline:

       

      pipeline {
          agent any
          stages {
              stage('First') {
                  steps {
                      echo 'Before step with error'
                      catchError(message: 'Step had error.', stageResult: 'UNSTABLE') {
                          sh 'nonexistentcommand'
                      }
                      echo 'After step with error'
                  }
                  post {
                      success {
                          echo 'First stage post success'
                      }
                      unsuccessful {
                          echo 'First stage post unsuccessful'
                      }
                  }
              }
              stage('Second') {
                  steps {
                      echo 'Stage after unstable stage'
                  }
                  post {
                      success {
                          echo 'Second stage post success'
                      }
                      unsuccessful {
                          echo 'Second stage post unsuccessful'
                      }
                  }
              }
          }
      }

      Console Output:

       

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] Start of Pipeline
      [Pipeline] node
      Running on Jenkins in ...
      [Pipeline] {
      [Pipeline] stage
      [Pipeline] { (First)
      [Pipeline] echo
      Before step with error
      [Pipeline] catchError
      [Pipeline] {
      [Pipeline] sh
      + nonexistentcommand
      .../script.sh: line 1: nonexistentcommand: command not found
      [Pipeline] }
      ERROR: Step had error.
      ERROR: script returned exit code 127
      [Pipeline] // catchError
      [Pipeline] echo
      After step with error
      Post stage
      [Pipeline] echo
      First stage post unsuccessful
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] stage
      [Pipeline] { (Second)
      [Pipeline] echo
      Stage after unstable stage
      Post stage
      [Pipeline] echo
      Second stage post unsuccessful
      [Pipeline] }
      [Pipeline] // stage
      [Pipeline] }
      [Pipeline] // node
      [Pipeline] End of Pipeline
      Finished: FAILURE
      

      Resolved issue https://issues.jenkins-ci.org/browse/JENKINS-52114 references making the post conditions "more stage specific", but from this test, the conditions don't seem stage-specific at all.  It's really unclear what the current behavior for the stage post conditions is supposed to be. 

      Unresolved issue https://issues.jenkins-ci.org/browse/JENKINS-59469 is a little bit similar, in that it asks for a variable with the stage result that can be used in the post section.  That would address our overall use case here too (which is sending per-stage notifications).  But just having post conditions that referred to the stage would be helpful.

      This behavior I think is also described in the commentary on JENKINS-39203https://issues.jenkins-ci.org/browse/JENKINS-39203?focusedCommentId=350599&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-350599

      If there are any workarounds for the current behavior, I'd love to give them a try.

          [JENKINS-61405] Stage post conditions doomed by build result becoming unstable

          Jim D created issue -
          Jim D made changes -
          Description Original: In a pipeline with multiple sequential stages that typically have either an unstable or a success result, stage post conditions can't be used to take any meaningful action, because one unstable stage dooms the subsequent stages to fire the unsuccessful post condition instead of the success post condition.

          Pipeline:

           
          {noformat}
          pipeline {
              agent any
              stages {
                  stage('First') {
                      steps {
                          echo 'Before step with error'
                          catchError(message: 'Step had error.', stageResult: 'UNSTABLE') {
                              sh 'nonexistentcommand'
                          }
                          echo 'After step with error'
                      }
                      post {
                          success {
                              echo 'First stage post success'
                          }
                          unsuccessful {
                              echo 'First stage post unsuccessful'
                          }
                      }
                  }
                  stage('Second') {
                      steps {
                          echo 'Stage after unstable stage'
                      }
                      post {
                          success {
                              echo 'Second stage post success'
                          }
                          unsuccessful {
                              echo 'Second stage post unsuccessful'
                          }
                      }
                  }
              }
          }{noformat}
          Console Output:

           
          {noformat}
          Running in Durability level: MAX_SURVIVABILITY
          [Pipeline] Start of Pipeline
          [Pipeline] node
          Running on Jenkins in ...
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Before step with error
          [Pipeline] catchError
          [Pipeline] {
          [Pipeline] sh
          + nonexistentcommand
          .../script.sh: line 1: nonexistentcommand: command not found
          [Pipeline] }
          ERROR: Step had error.
          ERROR: script returned exit code 127
          [Pipeline] // catchError
          [Pipeline] echo
          After step with error
          Post stage
          [Pipeline] echo
          First stage post unsuccessful
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] echo
          Stage after unstable stage
          Post stage
          [Pipeline] echo
          Second stage post unsuccessful
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: FAILURE
          {noformat}
          Resolved issue https://issues.jenkins-ci.org/browse/JENKINS-52114 references making the post conditions "more stage specific", but from this test, the conditions don't seem stage-specific at all.  It's really unclear what the current behavior for the stage post conditions is supposed to be. 

          Unresolved issue https://issues.jenkins-ci.org/browse/JENKINS-59469 is a little bit similar, in that it asks for a variable with the stage result that can be used in the post section.  That would address our overall use case here too (which is sending per-stage notifications).  But just having post conditions that referred to the stage would be helpful.

          If there are any workarounds for the current behavior, I'd love to give them a try.
          New: In a pipeline with multiple sequential stages that typically have either an unstable or a success result, stage post conditions can't be used to take any meaningful action, because one unstable stage dooms the subsequent stages to fire the unsuccessful post condition instead of the success post condition.

          Pipeline:

           
          {noformat}
          pipeline {
              agent any
              stages {
                  stage('First') {
                      steps {
                          echo 'Before step with error'
                          catchError(message: 'Step had error.', stageResult: 'UNSTABLE') {
                              sh 'nonexistentcommand'
                          }
                          echo 'After step with error'
                      }
                      post {
                          success {
                              echo 'First stage post success'
                          }
                          unsuccessful {
                              echo 'First stage post unsuccessful'
                          }
                      }
                  }
                  stage('Second') {
                      steps {
                          echo 'Stage after unstable stage'
                      }
                      post {
                          success {
                              echo 'Second stage post success'
                          }
                          unsuccessful {
                              echo 'Second stage post unsuccessful'
                          }
                      }
                  }
              }
          }{noformat}
          Console Output:

           
          {noformat}
          Running in Durability level: MAX_SURVIVABILITY
          [Pipeline] Start of Pipeline
          [Pipeline] node
          Running on Jenkins in ...
          [Pipeline] {
          [Pipeline] stage
          [Pipeline] { (First)
          [Pipeline] echo
          Before step with error
          [Pipeline] catchError
          [Pipeline] {
          [Pipeline] sh
          + nonexistentcommand
          .../script.sh: line 1: nonexistentcommand: command not found
          [Pipeline] }
          ERROR: Step had error.
          ERROR: script returned exit code 127
          [Pipeline] // catchError
          [Pipeline] echo
          After step with error
          Post stage
          [Pipeline] echo
          First stage post unsuccessful
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] stage
          [Pipeline] { (Second)
          [Pipeline] echo
          Stage after unstable stage
          Post stage
          [Pipeline] echo
          Second stage post unsuccessful
          [Pipeline] }
          [Pipeline] // stage
          [Pipeline] }
          [Pipeline] // node
          [Pipeline] End of Pipeline
          Finished: FAILURE
          {noformat}
          Resolved issue https://issues.jenkins-ci.org/browse/JENKINS-52114 references making the post conditions "more stage specific", but from this test, the conditions don't seem stage-specific at all.  It's really unclear what the current behavior for the stage post conditions is supposed to be. 

          Unresolved issue https://issues.jenkins-ci.org/browse/JENKINS-59469 is a little bit similar, in that it asks for a variable with the stage result that can be used in the post section.  That would address our overall use case here too (which is sending per-stage notifications).  But just having post conditions that referred to the stage would be helpful.

          This behavior I think is also described in the commentary on JENKINS-39203https://issues.jenkins-ci.org/browse/JENKINS-39203?focusedCommentId=350599&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-350599

          If there are any workarounds for the current behavior, I'd love to give them a try.
          Andrew Bayer made changes -
          Assignee Original: Andrew Bayer [ abayer ]
          Marcus Philip made changes -
          Link New: This issue duplicates JENKINS-57801 [ JENKINS-57801 ]

            Unassigned Unassigned
            jkd Jim D
            Votes:
            6 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: