How to send "back to normal" notifications in Jenkins' declarative pipeline?

I'm trying to convert an existing Jenkins pipeline to a new declarative pipeline and I was wondering how to properly handle mail notifications?

I am currently using this code:

node {
   try {

      ...

      currentBuild.result = 'SUCCESS'
   } catch (any) {
       currentBuild.result = 'FAILURE'
       throw any
   } finally {
       step([$class: 'Mailer',
           notifyEveryUnstableBuild: true,
           recipients: "baptiste.wicht@gmail.com",
           sendToIndividuals: true])
   }
}

      

This works well, but I can't see how to use the new declarative syntax for this. I think something can be done using post () and various notifications, but I am not sure exactly how to do it. I've tried this:

post {
    always {
        step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "baptiste.wicht@gmail.com",
            sendToIndividuals: true])
    }
}

      

But the problem is that it doesn’t send "Back to normal" mail.

How can I use the Mailer plugin in Jenkins declarative pipeline to send Back to Normal messages?

Should I use try / catch again in all declarative syntax?

+4


source to share


4 answers


The problem is that in the post section of the declarator, currentBuild.result is not set to SUCCESS. Bug fixed and ABORTED. So the behavior here seems inconsistent at the moment.

I improved my answer from How to get the same Mailer behavior for Jenkins pipeline to better deal with this case:



pipeline {
  agent any
  stages {
      stage('test') {
        steps {
            echo 'some steps'        
            // error("Throw exception for testing purpose..")
        }
      }
  }
  post {
      always {
          script {
              if (currentBuild.result == null) {
                  currentBuild.result = 'SUCCESS'    
              }
          }    
          step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "test@test.com",
            sendToIndividuals: true])
      }
  }
}

      

+4


source


Send mail on build failure. When the build is successful, you will check if the previous build was successful. If it is not, you will send a mail to inform the user that it is working again.



    post {

        failure {
            mail to: 'user@mail.com',
            subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
            body: "Build failed: ${env.BUILD_URL}"
        }

        success {
            if (currentBuild.previousBuild != null && currentBuild.previousBuild.result != 'SUCCESS') {
                mail to: 'user@mail.com',
                subject: "Pipeline Success: ${currentBuild.fullDisplayName}",
                body: "Build is back to normal (success): ${env.BUILD_URL}"     
            }           
        }
    }

      

+7


source


you can check the previous build like for all stars:

pipeline {
  agent { label 'docker' }
  stages {
    stage ('build') {
      steps {
        sh 'ls'
      }
    }
  }
  post {
    always {
      script {
        if (currentBuild.result == null || currentBuild.result == 'SUCCESS') {
          if (currentBuild.previousBuild != null && currentBuild.previousBuild.result != 'SUCCESS') {
            echo 'send your back to normal email here, maybe something like this, your mileage may vary'
            emailext (
              subject: "Back to normal: ${currentBuild.fullDisplayName}",
              body: "Project is back to <blink>normal</blink>",
              mimeType: 'text/html',
              recipientProviders: [[$class: 'RequesterRecipientProvider'], [$class: 'DevelopersRecipientProvider']]
            )
          }
        }
      }
    }
  }
}

      

+5


source


This can now be done much easier using fixed

postcondition ( Documentation ).

Here's a quick example I wrote in my sandbox pipeline project.

pipeline{
    agent {
        label 'Build'
    }
    stages{
        stage('Build'){
            steps{
                script{
                    echo "Building..."
                }
            }
        }
    }
    post{
        success {
            echo "Success"
        }
        failure {
            echo "Failure"
        }
        fixed {
            echo "Back to normal"
        }
    }
}

      

0


source







All Articles