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

Pipeline script post handler - changed action always fired

    Details

    • Similar Issues:

      Description

      In a pipeline script I have the following post handler logic.

         post {                                               
             always {                                                          
                 junit 'results/*.xml' 
             }                             
             changed {                                                      
                 emailext body: '$DEFAULT_CONTENT', subject: '$DEFAULT_SUBJECT', to: 'email@domain.com'                                             
             }                                                            
         }   
      

      What used to happen is that emails would only be sent if the state changed. So if the build results were as follows

      • FAILED
      • UNSTABLE
      • UNSTABLE
      • UNSTABLE
      • FAILED
      • UNSTABLE
      • FAILED
      • UNSTABLE
      • UNSTABLE

      Then email would only be sent on the state change from UNSTABLE->FAILED or FAILED->UNSTABLE. Emails would not be sent when the state was UNSTABLE->UNSTABLE.

      However our Jenkins server was updated over Christmas and as a result I am seeing an email being sent regardless of the state remaining the same. I could work around this by using something similar to

      if (currentBuild.currentResult.toString() != currentBuild.getPreviousBuild.getResult().toString()) {
      // email
      }

       

      However the previous behaviour was what I was expecting to see. Changes in the pipeline plugins seem to be

      Plugin Previous version New version
      Pipeline: API 2.23 2.24
      Pipeline: Basic Steps 2.5 2.6
      Pipeline: Build Step 2.5.1 2.6
      Pipeline: Declarative 1.2.2 1.2.5
      Pipeline: Declarative Extension Points API 1.2.2 1.2.5
      Pipeline: Groovy 2.41 2.42
      Pipeline: Nodes and Processes 2.11 2.17
      Pipeline: Shared Groovy Libraries 2.8 2.9
      Pipeline: Stage Step 2.2 2.3
      Pipeline: Step API 2.13 2.14

        Attachments

          Issue Links

            Activity

            ismith Ian Smith created issue -
            abayer Andrew Bayer made changes -
            Field Original Value New Value
            Component/s pipeline-model-definition-plugin [ 21706 ]
            Component/s pipeline [ 21692 ]
            Hide
            abayer Andrew Bayer added a comment -

            Oh, that is weird. I'll see if I can reproduce it.

            Show
            abayer Andrew Bayer added a comment - Oh, that is weird . I'll see if I can reproduce it.
            Hide
            kgerstl Franz Kaefer added a comment -

            Andrew Bayer Maybe this example helps: manually aborting the job honors the previous result, aborting by timeout always executes the changed block.

            pipeline {
                agent any
            
                options {
                    timeout(time: 3, unit: 'SECONDS')
                }
            
                stages {
                    stage("time out") {
                        steps {
                            sleep(time: 5, unit: 'SECONDS')
                        }
                    }
                }
            
                post {
                    changed { echo "changed: " + currentBuild.getPreviousBuild().result + " -> " + currentBuild.result }
                    aborted { echo "aborted pipeline" }
                }
            }
            

             

            Show
            kgerstl Franz Kaefer added a comment - Andrew Bayer Maybe this example helps: manually aborting the job honors the previous result, aborting by timeout always executes the changed block. pipeline { agent any options { timeout(time: 3, unit: 'SECONDS' ) } stages { stage( "time out" ) { steps { sleep(time: 5, unit: 'SECONDS' ) } } } post { changed { echo "changed: " + currentBuild.getPreviousBuild().result + " -> " + currentBuild.result } aborted { echo "aborted pipeline" } } }  
            Hide
            lostinberlin Steve Boardwell added a comment -

            Andrew Bayer: I can confirm this is also happening in Jenkins 2.89.2

            I found the change block to be triggered for every build state except SUCCESS. On subsequent successes, the change block is not entered. 

            See also: https://stackoverflow.com/questions/48468500/jenkins-pipeline-post-condition-consistently-entering-the-changed-condition-unle

            Show
            lostinberlin Steve Boardwell added a comment - Andrew Bayer : I can confirm this is also happening in Jenkins 2.89.2 I found the change block to be triggered for every build state except SUCCESS. On subsequent successes, the change block is not entered.  See also: https://stackoverflow.com/questions/48468500/jenkins-pipeline-post-condition-consistently-entering-the-changed-condition-unle
            abayer Andrew Bayer made changes -
            Assignee Andrew Bayer [ abayer ]
            abayer Andrew Bayer made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            Hide
            abayer Andrew Bayer added a comment -

            Reproduced - it is specific to non-SUCCESS, and the existing test...well, it only checked SUCCESS. D'oh. Working on it now.

            Show
            abayer Andrew Bayer added a comment - Reproduced - it is specific to non-SUCCESS, and the existing test...well, it only checked SUCCESS. D'oh. Working on it now.
            abayer Andrew Bayer made changes -
            Status In Progress [ 3 ] In Review [ 10005 ]
            Hide
            abayer Andrew Bayer added a comment -

            Well, that was some dumb code on my part. I won't even try to explain how I messed up, since it was so dumb that I can't even tell what I was intending exactly, let alone what was really happening! But - https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/242 drastically simplifies things with existing tests still passing and adds a new test to verify the fix. So woo. Should be out in the next few days in Declarative 1.2.7.

            Show
            abayer Andrew Bayer added a comment - Well, that was some dumb code on my part. I won't even try to explain how I messed up, since it was so dumb that I can't even tell what I was intending exactly, let alone what was really happening! But - https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/242 drastically simplifies things with existing tests still passing and adds a new test to verify the fix. So woo. Should be out in the next few days in Declarative 1.2.7.
            abayer Andrew Bayer made changes -
            Remote Link This issue links to "PR #242 (Web Link)" [ 19974 ]
            Hide
            abayer Andrew Bayer added a comment -

            Fix will be in 1.2.7, releasing today or tomorrow.

            Show
            abayer Andrew Bayer added a comment - Fix will be in 1.2.7, releasing today or tomorrow.
            abayer Andrew Bayer made changes -
            Status In Review [ 10005 ] Resolved [ 5 ]
            Resolution Fixed [ 1 ]
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Changed.groovy
            pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BuildConditionResponderTest.java
            pipeline-model-definition/src/test/resources/postOnChangeUnstable.groovy
            http://jenkins-ci.org/commit/pipeline-model-definition-plugin/08be448a802e6b5bbcddf1e50ebc230011976bb3
            Log:
            [FIXED JENKINS-48752] Drastically simplify and fix changed calculation

            The previous was...complicated. In some contexts, we do need to check
            the execution result, not just the run result, but that led to me
            writing some crappy code that meant that changed always triggered for
            any status but SUCCESS. Sigh. So instead, specifically get the worst
            status from the run and the execution and use that for
            comparison. Suddenly everything's simpler. =)

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: pipeline-model-definition/src/main/groovy/org/jenkinsci/plugins/pipeline/modeldefinition/model/conditions/Changed.groovy pipeline-model-definition/src/test/java/org/jenkinsci/plugins/pipeline/modeldefinition/BuildConditionResponderTest.java pipeline-model-definition/src/test/resources/postOnChangeUnstable.groovy http://jenkins-ci.org/commit/pipeline-model-definition-plugin/08be448a802e6b5bbcddf1e50ebc230011976bb3 Log: [FIXED JENKINS-48752] Drastically simplify and fix changed calculation The previous was...complicated. In some contexts, we do need to check the execution result, not just the run result, but that led to me writing some crappy code that meant that changed always triggered for any status but SUCCESS. Sigh. So instead, specifically get the worst status from the run and the execution and use that for comparison. Suddenly everything's simpler. =)
            sarkrish Sarath Chandran Radhakrishnan made changes -
            Comment [ I am using Jenkins version: 2.135

            In case of repeated FAILURE results, I enter the changed block again and again. I couldn't verify when the issue was introduced again after the fix. ]
            Hide
            bitwiseman Liam Newman added a comment -

            Bulk closing resolved issues.

            Show
            bitwiseman Liam Newman added a comment - Bulk closing resolved issues.
            bitwiseman Liam Newman made changes -
            Status Resolved [ 5 ] Closed [ 6 ]

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                ismith Ian Smith
              • Votes:
                0 Vote for this issue
                Watchers:
                7 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: