Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get same Mailer behaviour for Jenkins pipeline

Tags:

I started using Jenkins declarative pipelines. Now I want to have the same email notification behavior as defined in the Usage of the Mailer plugin:

  1. Every failed build triggers a new e-mail.
  2. A successful build after a failed (or unstable) build triggers a new e-mail, indicating that a crisis is over.
  3. An unstable build after a successful build triggers a new e-mail, indicating that there's a regression.
  4. Unless configured, every unstable build triggers a new e-mail, indicating that regression is still there.

I read about Notifications in Pipelines, but it does not notify based on above rules. Plus it does not contain part of the console output in the message body in case of a build failure.

Does anybody know how to do this in declarative pipeline?

like image 873
Daniel Steinmann Avatar asked May 29 '17 14:05

Daniel Steinmann


People also ask

Can I mix declarative and scripted pipeline?

Yes you can only if you want to have external function inside step block.

What are the 3 types of pipelines in Jenkins?

Different Types of Jenkins CI/CD Pipelines. Scripted Pipeline. Declarative Pipeline.


1 Answers

With the following code you can use the mailer plugin in the post section. This provides the expected behaviour:

pipeline {
  agent any
  stages {
      stage('test') {
        steps {
          script {
              // change to 'UNSTABLE' OR 'FAILED' to test the behaviour 
              currentBuild.result = 'SUCCESS'
          }
        }
      }
  }
  post {
        always {
          step([$class: 'Mailer',
            notifyEveryUnstableBuild: true,
            recipients: "[email protected]",
            sendToIndividuals: true])
        }
  }
}
like image 91
Philip Avatar answered Dec 24 '22 18:12

Philip