Jenkins Pipeline with Email-Ext Plugin

Invoke groovy templates in the Jenkins email-ext plugin

Guide to use Email-ext plugin in Jenkins pipeline

We are going to use email-ext groovy template to generate the custom emails with HTML template in the body for your email look like below picture.

How to invoke groovy templates in the Jenkins email-ext plugin?

Installation of email-ext plugin

Step 1

Install the Email extension plugin in the Jenkins as seen below

I have created a choice parameter in Jenkins by using a variable “Name” as parameter to be passed in the pipeline script in jenkins and two choices are “App1” and “App2”

Step 2

Create a template file called email-html.template or whatever name you like.

You need to place the email-html.template file inside $WORKSPACE directory on Jenkins machine. may be like this “$WORKSPACE /mail-html.template”

You can keep wherever you want. Here we simply specify the template location.

Make sure that “Jenkins” should own this file. Set the correct permissions to the template file for Jenkins to access.

Click the link to download the email-html.template file with html and groovy code. You can modify to suit your needs.

Configurations Breakdown for template file

Line 3-33 -> deals with the html elements that you needs your email body to look like.

line 35, 36 -> The script is to make Environment variables added in pipeline to be accessible in email-ext template.

you can use the following in your Groovy script to access environment variables set in your pipeline.

In my case, I have used “Name” and “cest” env variables that I have added in pipeline script to get accessed in template.

line 55 -> I want the value of env variable “Name” to be populated. So I have called that.

Jenkins Pipeline Script Configuration

pipeline { agent any stages { stage('Email') { steps{ script { cest = TimeZone.getTimeZone("CEST") def cest = new Date() println(cest) def mailRecipients = '[email protected]' def jobName = currentBuild.fullDisplayName env.Name = Name env.cest = cest emailext body: '''${SCRIPT, template="email-html.template"}''', mimeType: 'text/html', subject: "[Jenkins] ${jobName}", to: "${mailRecipients}", replyTo: "${mailRecipients}" } } } } }

Configuration Breakdown for Jenkins pipeline Script

In Line 18, I have made my Pipeline script to load email-html.template as body content.

In line 11, I want the “cest” timezone to be updated in body of email.

Triggering the Jenkins Pipeline build for email-ext

Once we select the parameter, You can see that the value of the Parameter got updated the mail as “App1” as seen below picture.

This is how the email template that we have created looks like below.

Points to Note

Issue

Exception raised during template rendering: Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod java.lang.String java.lang.Object

Solution

If you get the above issue, then you need to navigate to “Manage jenkins” -> “In process Script Approvals” and you need to approve all scripts. For running groovy in sandbox, you need to approve the script.