Jenkins sending email on post build

Gustavo Apolinario
4 min readMay 14, 2018

--

After Build you need communicate the persons who can be responsables for the build failure.

You may want send a slack notification too: https://medium.com/@gustavo.guss/quick-tutorial-of-jenkins-b99d5f5889f2

Installing Email Extended

Go to Manage Jenkins > Manage Plugins > click on tab Available and search for “Email Extension”.

If you find, install it. If you don’t find it, search in Installed tab because it can be installed.

Configuring Email Extended

To send email, the plugin needs the smtp configured.

Go to Manage Jenkins > Configure System > search for “Extended E-mail Notification”.

Configure the smtp.

Example: My configuration with gmail smtp.

Testing Mail sender

Go to home of jenkins and create a new job. Select pipeline and name it as “SendMail-test”.

Put this pipeline in new job:

pipeline {
agent any

stages {
stage('Ok') {
steps {
echo "Ok"
}
}
}
post {
always {
emailext body: 'A Test EMail', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Test'
}
}
}

This pipeline run in any jenkins agent: (you can has jenkins agents to run code outside your master jenkins server)

agent any

It has a stage to sample

stages {
stage('Ok') {
steps {
echo "Ok"
}
}
}

In post step, you can run any script you want. We have the mail sender in it.

post {
always {
emailext body: 'A Test EMail', recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']], subject: 'Test'
}
}

Save it and run clicking in “Build Now” on job menu.

The build will apear in stage view (if necessary reload the page).

Click on Build Number “#1” and click on “Console Output” on build menu.

The output will be like this:

Started by user GUSTAVO WILLY APOLINARIO DOMINGUES
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/SendMail-test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Ok)
[Pipeline] echo
Ok
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] emailext
Sending email to: abc gustavo.guss@gmail.com
Successfully sent to the following addresses: gustavo.guss@gmail.com
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Go to your email. You must receive a new email like this:

Changing Sender Name

You receive the email, but the sender name is ugly. To change it, go to Manage Jenkins > Configure System.

Search for “System Admin e-mail address” (In Jenkins Location).

Change it to: “Jenkins <jenkins@jenkins>”

Save and run the job again.

The new message has “Jenkins” as sender name.

Sending a post build email with job information

To send a real post build email, create a new pipelinejob and name it as “sample-sender-email”.

Put this pipeline:

pipeline {
environment {
//This variable need be tested as string
doError = '1'
}

agent any

stages {
stage('Error') {
when {
expression { doError == '1' }
}
steps {
echo "Failure"
error "failure test. It's work"
}
}

stage('Success') {
when {
expression { doError == '0' }
}
steps {
echo "ok"
}
}
}
post {
always {
echo 'I will always say Hello again!'

emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"

}
}
}

This script is a test to you try the post build. You can see more about post build here: https://medium.com/@gustavo.guss/how-to-do-post-build-in-jenkins-pipeline-d1e7233909b8

Changing the variable “doError” you can test the failure/success build.

Save and run this job. See your email and you will receive this:

Great! post build configured, you will receive email in all build.

You can change the mail to send only in failure if prefer. (change “always” to “failure”).

You can configure the default value for the plugn. To it, go to Jenkins > Configure System > Extended E-mail Notification

jenkins extended e-mail notification configuration for default values

Edit your default value and when you call, use something like that:

emailext body: "${DEFAULT_CONTENT}",
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
subject: "${DEFAULT_SUBJECT}"

Thanks.

You can see more at:

--

--

Gustavo Apolinario

I’m a FullStack Developer, working now with DevOps automations. I previously worked as a front-end, PHP Developer, and DevOps.