Create Email Notification from Jenkins Pipeline

People use Jenkins as a CI/CD tool. one example is to create a pipeline where people several tasks to be done automatically. There is one problem though, how do you know if your job is successful or failed? if your job can be done shortly, you can just wait and see it in Jenkins, but what if it is a long process, and you want to be notified about the result while you are doing something else. In this article, I will create an Email notification alert, to notify us about the result of the job, though you can use many other platforms to accept the notification such as Slack, etc.

Before we can use the email notification feature, we need to create an SMTP relay. Sendgrid can help us to set that up.

Sendgrid Setup

Create a virtual machine instance in Google Cloud Console, and then search for Sendgrid Email API in cloud marketplace and subscribe, just pick the free 1-month option.

After all of that, you will be redirected to create a Sendgrid account. You also need to set up a verified sender after that before sending an email, otherwise, you wouldn’t be able to.

In order to for Sendgrid to send an email, you need to create a Verified Single Sender first. Click Setting, and then click Sender Authentication or you can just click Create a Single Sender button in Dashboard.

An SMTP mail relay is a process of transferring an email from one server to another for delivery. In this case, the servers are SendGrid and Gmail, since the notification will be sent to my personal Gmail account. Here’s how you do it, Click Email API on the left part of the webpage, and then click Integration Guide.

Choose SMTP Relay and create an API key. The generated key will be used as our SMTP Relay password. Don’t forget to copy the API key before closing the page.

Jenkins Configuration Setup

In order for Jenkins to send an email, we need to install Email Extension Plugin. After the plugin is installed, go to Manage Jenkins and Click Configure System. Find Jenkins Location and change the value of System Admin e-mail address be the email address you used as a verified sender in Sendgrid. This can be just the email “abc@email.com” or it could be something like “Jenkins Notification ”.

To set up the email notification setting, go to Extended E-mail Notification. According to this part, your SMTP server will be smtp.sendgrid.net, and there are several options of ports, if you want your email to be encrypted, which is safer, you need to use 465 as your port. Click Advance as there are several hidden fields that we need to fill, such as SMTP Username and Password. For SMTP Username, we just need to put apikey and we use the generated API Key as password. Don’t forget to check Use SSL since we want the email to be encrypted, leave the rest of the fields by default and click Save.

Pipeline Setup

A new pipeline job needs to be created in order to use the email notification feature. In this case, we named the job SendGrid email test, click pipeline, and click save. In the configuration, go to the pipeline set to set up the pipeline. We use the Groovy scripting language to create the pipeline. Put this script as the pipeline script

Jenkins Script Explanation

We create SendEmailNotification() method that takes a string that we use as the result. We create a method so we can use it twice, when the build succeeds or when it fails. We also need to put the method below node , otherwise, the script will result in an error. Node consist of two parts, try and catch , we put all the stages inside try , and whatever needs to happen when the build fails inside catch . The three stages, which are dummy stages, are run unit tests , build and post-build . We send the email in post-build stage by calling SendEmailNotification() and put SUCCESSFUL as the parameter.

SendEmailNotification() method

def to = emailextrecipients([ 
requestor()
])

define a variable that store all the email addresses that we need to send the notification to. We can just use the method provided by the plugin, emailextrecipients(), we also need to throw a list of parameters, to tell jenkins whose email that we want to send the email to. in this case, we only use requestor() since we want to send the one who trigger the build. If you are wondering, how the plugin know the email address of the trigger? well, you need to log in first to jenkins, and you registered that account using an email address, so make sure you use a real email address to see the result. There are other options, such as developers(), to add the email of anyone who checked in code for the last build. You also can add acomma (or whitespace) separated list of email address like this. You can find more options here.

def to = emailextrecipients([ 
"abc@email.com",
"def@email.com"
])
def subject = "$ - Build #$ $"

The subject variable consist of this string $ - Build #$ $ , we are going to give the job name, the build number, and the result as the subject.

def content = '$'

For content variable, we use a template script provided by the plugin that will style the content of the email and will give us more information. There are two options of template, html and text , but we are going to use html because it will look nicer.

Before sending the email we need to check if the to variable is defined and not empty, then we can send the email. we use the emailext plugin, and fill some value. for body , we use content variable, text/html as mimeType , subject variable as subject , to variable as to , and set the attachLog to be true so the email will be attached with a log file that consist of things that you see in console output

if(to != null && !to.isEmpty()) emailext(body: content, mimeType: 'text/html', 
subject: subject,
to: to, attachLog: true )
>

If you click Build Now, Jenkins will run the job and will send the email notification to you if the job is finished, in this case, the job would be successful because all the stages we created in the pipeline are just dummy stages.

Email Notification Result

If sending the email is successful, you will get an email like this,

If you don’t get any email, you can check the console output and see if you get this type of error,

If so, you probably need to check your verified sender configuration in Sendgrid, because that is the error I got when I forgot to create a Verified Sender. The job will still be marked as Success but the email will not be sent.

I hope you find it useful.

Thanks for reading 😀

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇