Jenkins - Customizing the EmailExt Plugin
• Thy DaiCategories: jenkins
Purpose: To guide and explain how the email extension plugin works and how to edit the template to generate a better email for build failures.
Old Email Logs:
New Email Logs:
Languages
- Groovy
- HTML/CSS
- JSP (Java Server Pages) + Jelly
- Java Regex
Software
- A code editor (ex. Atom)
- FTP client (ex. FileZilla)
Installation
-
Install the Email-ext plugin to your Jenkins instance.
-
In your Jenkins, click on “Manage Jenkins” and then “Configure System”.
- In the Extended E-mail Notifcation, use the following settings:
- Default Content Type:
HTML (text/html)
` - Default Subject¹:
"Failed: ${ENV,var="JOB_NAME")#$BUILD_NUMBER"
- Default Content Type:
- If you are using a shared libraries and calling the Email-ext plugin, ensure the body contains the following line:
'''${SCRIPT, template="groovy_html.template"}'''
- Otherwise, add the above line to the Default Content in the Jenkins UI Configuration.
- Otherwise, add the above line to the Default Content in the Jenkins UI Configuration.
- Create your Groovy HTML template (groovy_html.template), and store in $JENKINS_HOME/email-templates.
- This would be on the master Jenkins node.
- You may have to create this folder if it doesn’t exist.
Testing
Testing your email notification is a little tricky and incredibly informal. Unfortunately, it is just trial by fire and you can potentially overwrite the existing template. But thankfully, in the event that it does happen, we are able to manually restore the original template.
These instructions walk you through the way of creating and testing a new email template.
-
Name your template “test.template”.
-
In the Emailext plugin section of your shared libraries, change the template to:
'''${SCRIPT, template="test.template"}'''
-
FTP that over to the Jenkins node.
-
Run your build.
- If you wish to do this with less Git commits, just Replay a build and update the sendEmailNotification script with your test script name.
- If it does not email you, or it’s ignoring you, add the following section to the sendEmailNotification groovy script: to: CERNER_ID@cerner.net,
Resources
-
Regular Expressions (RegEx)
RegEx in Groovy (and especially with the RegEx) are a little confusing. Use JDoodle, an online Groovy IDE, to assist you in testing your RegEx code. I found this guide extremely helpful in navigating the world of Groovy RegEx.
¹ Environment Variables
${ENV, var="JOB_NAME"}
: This is extracted from the build’s properties. More information is found here. Add the following line to your template file to view the current build’s properties.
<% println build.properties.collect{it}.join('<br />') %>