Tuesday 3 October 2017

Customized Build email notifications in Jenkins

When using Jenkins for automated builds, as most of you guys may be aware, we can setup SMTP settings for email notifications of build related events. We will cover the topic of SMTP setup in another separate post. In this post, let us just stick to the email customization part. Note that the SMTP settings must be configured in Jenkins for this plugin to work smoothly. For customized email notifications, we will use the Email-ext plugin to replace the default Jenkins'email settings.

With this plugin, we can setup email settings for various trigger conditions like a successful build, aborted build or a build failure etc.


In today's illustration, we'll cover the notification for Failure - Any trigger. In other words, Jenkins will fire a notification the moment there is any kind of build failure. We will use the HTML formatting in the notification. Use the following variables in the job configuration as shown in below screenshot.


In the Content field, try the following piece of code as below :


As you see, we include information like the Jenkins job URL, the cause of the failure which will be a short description of the error, the changes that went into the build and a link to the console URL.

Note the line which includes a regex - we basically do a simple pattern matching i.e we look for the words "BUILD FAILURE" and then display only that part from the build error. In better words, we truncate the verbose build log and show only the relevant error information in the mail notification.

${BUILD_LOG_REGEX, regex="^.*?BUILD FAILURE.*?$", linesAfter=10, matchedLineHtmlStyle="width:100%", showTruncatedLines="false", defaultValue="For detailed information, please check job console link below to find cause of failure"}  

Apart from this, the plugin also allows us to optionally attach the build log. However note that the build logs usually tend to be of big sizes (depending on the size of the project) and could fill your mailboxes fast.


Now with the email notification configured, let us look into how the notifications will actually look like. So here's the output - check the screenshot below :



Signing out for now. Hope it helps. Ciao guys!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker