How to Setup Jenkins Email Notification (Detailed Guide)

Jenkins email notfication

In this blog, you will learn about setting up Jenkins Email notification.

Email notification for Jenkins build status is an important part each pipeline. It allows developers and relevant teams to get instance feedback on their pipeline job.

SMTP Credentials (Prerequisite)

You need to have valid SMTP credentials to setup the email notification.

If you are setting it up for your organisation, you can get the SMTP details from the network team.

If you are using AWS you can make use of AWS SES to create SMTP credentials.

Setup Jenkins Email Notification

Follow the steps below to set up the Jenkins Email notification.

Step 1: Create Credentials of SMTP Username and Password

The first step is to create a Jenkins credential with SMTP username and password.

Go to Manage Jenkins -> Credentials and create a global credential.

creating jenkins credentials

Select the credentials kind as Username with password and specify your SMTP username and password.

creating Jenkins credential with SMTP username and password

Then, press the Create button to create the credential.

Step 2: Configure SMTP Server on Jenkins

The next step is to configure the SMTP server on Jenkins. For that, go to

Manage Jenkins -> System scroll down to the Jenkins Location section and enter your from address, which is your SMTP server’s name.

enter smtp from address under jenkins location section

You have to specify the from address in the following format

example@<smtp-server-name>

For example, my SMTP server’s name is devopsprojects.dev, and I have given it as [email protected].

Now, scroll down to the Extended E-mail Notification section and specify your SMTP server address, port, select SSL and select the credentials you created in the previous step.

configuring smtp server on jenkins

Make sure you have given your SMTP server’s address and click the Save button to save the configurations.

Step 3: Test Jenkins Email Notification on a Job

Once the SMTP server is configured, you can add email notification on each job.

An example Jenkins pipeline code for email notification is given below (highlighted code).The post block is responsible for sending email notifications.

pipeline {
    agent {
        label 'kubeagent'
    }

    stages {
        stage('Code Checkout') {
            steps {
                sh "echo 'Checkout Completed'"
            }
        }
        stage('Build') {
            steps {
                sh "echo 'Build Completed'"
            }
        }
    }
    post {
        always {
            mail to: '[email protected]',
                 subject: "Jenkins Build Notification: ${currentBuild.fullDisplayName}",
                 body: """\
                 Build Status: ${currentBuild.currentResult}
                 Project: ${env.JOB_NAME}
                 Build Number: ${env.BUILD_NUMBER}
                 Build URL: ${env.BUILD_URL}
                 """
        }
    }
}

The label ‘kubeagent’ on the agent block is the label of my agent make sure to replace it with your agent label.

Also, make sure to update the email ID before starting the build.

Now, Create a new pipeline using the above pipeline code.

creating a new pipeline job

Give a name to the job and select the type as Pipeline

creating a new pipeline job

On the next page, go to Advanced Project Options and paste the above pipeline code inside the script block

creating a new pipeline job

Once the Job is created click the Build now button to start the build

triggering the pipeline

You can see the build is successful

output of a successful build

And you will receive an email notification on the specified email as shown below

email notification for build success

Now, let’s see what happens if the build gets failed.

I have added the following stage in the pipeline to fail the build.

stage('Test') {
    steps {
        sh "exit 1"
    }
}

You will receive the below mail for failed build

email notification for failed build

If you are using freestyle job, you can configure the email notification in the post build actions as shown below.

email notification in Jenkins freestyle job

Email Notification Using Shared Library

Follow that steps given below to setup email notification using Jenkins shared library.

Step 1: Configure Jenkins Shared Library

We will be using the shared library in the pipeline, you can find the script in the following GitHub repository.

https://github.com/techiescamp/jenkins-shared-library

If you already have a shared library, you can make use of the EmailNotification Function and the related HTML template from the resources folder.

If you want to use our shared library repo, follow the below steps to configure it.

Go to Manage Jenkins –> System and scroll down to the Global Trusted Pipeline Libraries section and configure shared library as shown below

configuring shared library on jenkins
configuring shared library on jenkins

Step 2: Test Jenkins Email Notification with Shared Library

An example pipeline code for email notification is given below

@Library('jenkins-shared-library@master') _
pipeline {
    agent {
        label 'kubeagent'
    }

    stages {
        stage('Git Checkout') {
            steps {
                sh "echo 'Checkout Completed'"
            }
        }
        stage('Build') {
            steps {
                sh "echo 'Build Completed'"
            }
        }
    }
    post {
        always {
            script {
                emailNotification("[email protected]")
            }
        }
    }
}

Make sure to update the Email ID before starting the build.

The groovy script for email notification and its template is placed inside the following directory

├── resources
│       └── notify.tpl
├── src
└── vars
      └── emailNotification.groovy

The groovy script uses the notify.tpl template in the resource folder, you can get the directory structure and code used in this lesson from Github.

Create a pipeline with above pipeline code, and once the build is completed, you will receive an email notification on the specified email as shown below

email notification got from a successful build

And, this is the notification you will get for a failed build.

email notification got from a failed build

You can see that the URL for the build, the branch, and the build details are given directly in the email notification itself.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like