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

Pipeline: Send notification "Back to normal" state

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      I understand how to send slack notifications in a Pipeline script on success, or on failure. This is useful for build jobs.

      For automated tests, a notification on each job is not so useful. Instead, I'd rather be notified when a job fails, or when a job succeeds and the previous job failed. Pre-Pipeline, this was referred to as "Back to normal" I think.

      I'd like a way to know that a previous build failed. Something like this:

      [... test completed successfully]
      if (previousBuild.success() == false) {
         [send slack message: "Test back to normal]
      }
      

        Attachments

          Activity

          Hide
          0x89 Martin Sander added a comment -
          def previousResult = currentBuild.previousBuild?.result
          if (previousResult && previousResult != currentBuild.result) {
              // do it
          }
          
          Show
          0x89 Martin Sander added a comment - def previousResult = currentBuild.previousBuild?.result if (previousResult && previousResult != currentBuild.result) { // do it }
          Hide
          jviki Jan Viktorin added a comment -

          Hello,

          this code:

          pipeline {
          ...
          }
          post {
                  success {
                      def previousResult = currentBuild.previousBuild?.result
                      if (previousResult && previousResult != currentBuild.result) {
                          emailext attachLog: true, to: "${env.MAINTAINERS}", subject: "Fixed: ${env.JOB_NAME} #${currentBuild.number}",
                              body: "See ${env.BUILD_URL} for details."
                      }
                  }
          }
          

          produces:

          org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
          WorkflowScript: 44: Expected a step @ line 44, column 13.
                         def previousResult = currentBuild.previousBuild?.result
                         ^
          
          WorkflowScript: 45: Expected a step @ line 45, column 13.
                         if (previousResult && previousResult != currentBuild.result) {

           

          Show
          jviki Jan Viktorin added a comment - Hello, this code: pipeline { ... } post {         success {             def previousResult = currentBuild.previousBuild?.result             if (previousResult && previousResult != currentBuild.result) {                 emailext attachLog: true , to: "${env.MAINTAINERS}" , subject: "Fixed: ${env.JOB_NAME} #${currentBuild.number}" ,                     body: "See ${env.BUILD_URL} for details."             }         } } produces: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 44: Expected a step @ line 44, column 13. def previousResult = currentBuild.previousBuild?.result ^ WorkflowScript: 45: Expected a step @ line 45, column 13. if (previousResult && previousResult != currentBuild.result) {  
          Hide
          0x89 Martin Sander added a comment - - edited

          Jan Viktorin:

          The attached code snippet is for the Pipeline Plugin, see e.g. https://jenkins.io/solutions/pipeline/.

          Your snippet seems to be using the "declarative pipeline" from the Pipeline Model Definition Plugin, which is not the same. The latter is supposed to be easier to use, but less powerful.

          Maybe you are looking for https://jenkins.io/blog/2017/02/15/declarative-notifications/.

          Show
          0x89 Martin Sander added a comment - - edited Jan Viktorin : The attached code snippet is for the Pipeline Plugin , see e.g. https://jenkins.io/solutions/pipeline/ . Your snippet seems to be using the "declarative pipeline" from the Pipeline Model Definition Plugin , which is not the same. The latter is supposed to be easier to use, but less powerful. Maybe you are looking for https://jenkins.io/blog/2017/02/15/declarative-notifications/ .
          Hide
          jefflikesjenkins Jeff Jenkins added a comment - - edited

          So the "Pipeline Plugin" link in your post times out. In jenkins by default, when I select "New Item" -> "Pipeline", then using the syntax above does not work. Not sure if the pipeline syntax changes for the default pipeline project type depending on what you have installed, however I found that the syntax above does not work for me either, and in my perspective, it is not setup properly.

          Here is the proper layout that allows my pipeline to function properly for build back to normal:

          pipeline {

             // pipeline stages/steps
             post {
               success {
                 script

          { // wrap the if statement in a script tag.          def previousResult = currentBuild.previousBuild?.result          if (previousResult && previousResult != currentBuild.result)            \{            // do it          }

                 }
               }

          }

          Show
          jefflikesjenkins Jeff Jenkins added a comment - - edited So the "Pipeline Plugin" link in your post times out. In jenkins by default, when I select "New Item" -> "Pipeline", then using the syntax above does not work. Not sure if the pipeline syntax changes for the default pipeline project type depending on what you have installed, however I found that the syntax above does not work for me either, and in my perspective, it is not setup properly. Here is the proper layout that allows my pipeline to function properly for build back to normal: pipeline {    // pipeline stages/steps    post {      success {        script { // wrap the if statement in a script tag.          def previousResult = currentBuild.previousBuild?.result          if (previousResult && previousResult != currentBuild.result)            \{            // do it          }        }      } }

            People

            Assignee:
            kmadel Kurt Madel
            Reporter:
            crummy malcolm crum
            Votes:
            7 Vote for this issue
            Watchers:
            14 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: