Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Emailing Results

Jeremy Poulin edited this page Nov 29, 2018 · 3 revisions

Sometimes, an effective way of monitoring the results of a test involve emailing those results upon test completion. You can find an example of how this can be done in the Ansible Engine Test, and I will summarize the essentials below.

The Email-Ext Plugin

The first thing to do to begin working with email is to ensure that the email-ext plugin installed. You can do this by going to your Jenkins, navigating to Manage Jenkins > Manage Plugins > Installed and search for Email Extension Plugin.

Once you've confirmed that is present, you need to configure it. You can do this by going to Manage Jenkins > Configure System and scrolling down to Extended E-mail Notification.

Here, the two most important things to set are the SMTP server and the Default user E-mail suffix.

SMTP server

Should be set to something like smpt.example.com.

Default user E-mail suffix

Should be set to something like @example.com.

Feel feel to look through and fill out any other default values you may deem relevant. Finally, hit Save or Apply to ensure your changes are made permanent.

Updating the Test Template

The key edits that need to be made to the test template are that recipients list should be added as a parameter, and that a call to the emailext step should be made. These are described in further detail below.

Recipients List Parameter

This is an optional addition since your recipient list may be fixed. It's useful, however, at times where you may want to add individuals for specifics builds or to turn off emails for a test build by overriding it to a blank list of recipients.

string(
    defaultValue: '',
    description: 'Semi-colon delimited list of email notification recipients.',
    name: 'EMAIL_SUBSCRIBERS'
)

Performing the Email

First, you want to build strings that will encapsulate the subject and body of your email. Once that is done, you can send an email by simply calling the emailext build step with the parameters listed below.

def emailBody = "Results for ${env.JOB_NAME} - Build #${currentBuild.number}\n\nResult: ${currentBuild.currentResult}\nURL: $BUILD_URL"
    if (errorMessages) emailBody += "\nErrors: " + errorMessages
 
    emailext(
      subject: "${env.JOB_NAME} - Build #${currentBuild.number} - ${currentBuild.currentResult}",
      body: emailBody,
      from: 'multiarch-qe-jenkins',
      replyTo: 'multiarch-qe',
      to: "${params.EMAIL_SUBSCRIBERS}",
      attachmentsPattern: 'artifacts/tests/**/**/artifacts/**/*.*'
    )

Parameters

Here are the key parameters to sending an email. A full list is available in the Jenkins Pipeline Reference docs.

subject

The subject of your email.

body

The body of your email.

from

The sender of the email as it appears when received.

replyTo

The email address that recipients should direct their responses to.

to

The list of recipients.

attachmentsPattern

Specifies the path to the files that should be attached. Uses ant-glob syntax.