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

catchError should set result to STABLE if otherwise null

    XMLWordPrintable

Details

    Description

      Mailer doesn't send "back to normal" email, when pipeline gets green after failed build.
      The "failed build" email is send properly.

      The issue occurs only if plugin is used inside pipeline script.

      Example to reproduce:

      node {
          catchError {
            stage "random fail"
            Random random = new Random();
            int randomNumber = random.nextInt(10 - 1) + 1;
            echo "${randomNumber}"
            if (randomNumber > 5) {
              echo "in loop"
              currentBuild.result = 'FAILURE'
              error "failed"
            }
          }
          echo "after finally"
          step([$class: 'Mailer', recipients: 'tomasz.pietrek@sap.com'])    
      }
      

      The same issue occurs if code is wrapped with try, catch, finally instead of catchError

      Attachments

        Activity

          I had the same issue.
          I found some topics on google that explained tha this is because the currentBuild.result is not yet set and null.

          Your Failure works because you set it yourself.

          In my side, my workaround is to wrap the whole pipeline in a try / catch / finally.

          If the pipeline fall in the catch, that means that an exception is thrown so i mark the build as failed.

          In the finally, if currentBuil.result is null, I assume that it should be SUCCESS, so I set it myself.
          Then, again in the finally block, I call the Mailer step.

          It works for both, failures an back to stable notifications.

          It us a workaround.
          I'm not a big fan of bypassing the pipeline final result management.
          I don't know if something can be done to not have to do it.

          buendon Nicolas Thomas added a comment - I had the same issue. I found some topics on google that explained tha this is because the currentBuild.result is not yet set and null. Your Failure works because you set it yourself. In my side, my workaround is to wrap the whole pipeline in a try / catch / finally. If the pipeline fall in the catch, that means that an exception is thrown so i mark the build as failed. In the finally, if currentBuil.result is null, I assume that it should be SUCCESS, so I set it myself. Then, again in the finally block, I call the Mailer step. It works for both, failures an back to stable notifications. It us a workaround. I'm not a big fan of bypassing the pipeline final result management. I don't know if something can be done to not have to do it.
          jglick Jesse Glick added a comment -

          Currently catchError sets no result when the block succeeds. Probably it could set it to STABLE if not already set. CoreStepTest.mailer tests some related scenarios but I guess not this one.

          Anyway you should probably be using Declarative Pipeline, which handles this sort of thing without custom scripting.

          jglick Jesse Glick added a comment - Currently catchError sets no result when the block succeeds. Probably it could set it to STABLE if not already set. CoreStepTest.mailer tests some related scenarios but I guess not this one. Anyway you should probably be using Declarative Pipeline, which handles this sort of thing without custom scripting.

          jglick, when you talk about Declarative Pipeline, are you talking about what I found here: https://jenkins.io/blog/2016/09/19/blueocean-beta-declarative-pipeline-pipeline-editor/ ?

          buendon Nicolas Thomas added a comment - jglick , when you talk about Declarative Pipeline , are you talking about what I found here: https://jenkins.io/blog/2016/09/19/blueocean-beta-declarative-pipeline-pipeline-editor/ ?
          jglick Jesse Glick added a comment -

          Yes and wiki.

          jglick Jesse Glick added a comment - Yes and wiki .
          abayer Andrew Bayer added a comment -

          For compatibility reasons, we aren't going to change this, but you can use currentBuild.currentResult to get a non-null result at any time - it's result.SUCCESS by default.

          abayer Andrew Bayer added a comment - For compatibility reasons, we aren't going to change this, but you can use currentBuild.currentResult to get a non-null result at any time - it's result.SUCCESS by default.

          People

            Unassigned Unassigned
            tomaszpietrek Tomasz Pietrek
            Votes:
            5 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: