Email notification every time able to provide us with Jenkin build a complete for us to send a mail notification, original Jenkins has been integrated in the simple email notification function, while at the same time and expand the function of a mail, E-mail notification can make us more thin, through this article you will learn how to use two plug-ins email notification.

Native mail

Let’s take a look at the implementation of native mailbox notification.

  1. First of all,Dashboard -> Manage Jenkins -> Configuration System -> Jenkins LocationTo configure the email address of administrator admin

  1. Log in to the email server to be configured. The following uses NetEase Email as an example to access the email website mail.163.com

Scroll down, click Add password in authorization Password Management, and generate an authorization password according to the steps, as shown in the picture:

  1. inConfiguration System -> E-mail NotificationConfigure an email server for Jenkins

Select Use SMTP Authentication, enter the NetEase email address in Username, and enter the authorization Password generated in step 2. SSL indicates whether to encrypt the email address. If SMTP Port is selected, Port 465 is used.

Charset uses the default UTF-8. You can check Test Configuration by Sending Test e-mal To Test whether the Configuration is successful. ·Email was successfully sent· The basic Email configuration is complete

  1. Based on the above steps, we have successfully configured a mailbox server for Jenkin, which can be used when we want the project to send an email to the specified mailbox after a successful build. Configure a post-build Actions for an Item and specify the mailbox for notification.

Email Extension Plugins

Expand native Email plugin, we just accomplished by simply of native when building at the end of the function for specified mailbox send a E-mail, so when we want to the content of the custom Email, hope to mail information to display complex components, such as the current building of serial number, name of the build time, build the JOB, We need Email Extension Plugins to do that for us

  1. Start by installing and configuring the plug-in from the Plugins Manager

After installing the plug-in, the Extended E-mail Notification bar will appear in Manage Jenkins -> Configuration. We can configure it again using the Configuration in the native plug-in

There are some fields you can skip below

  1. Create a file named emailtemplate.html in your project path and upload it to GIT.

<! DOCTYPEhtml>
<html>

<head>
    <meta charset="UTF-8">
    <title>${ENV, var="JOB_NAME"}- ${BUILD_NUMBER} time build log</title>
</head>
<body>
    <table>
        <tr>
            <td>
                <h2>
                    <span style="color: #2eabff">${BUILD_STATUS}</span>
                </h2>
            </td>
        </tr>
        <tr>
            <td>
                <br/>
                <b><span style="color: #4D90FE">Build information</span></b>
                <hr size="2" width="100%" align="center">
            </td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>Project name: ${PROJECT_NAME}</li>
                    <li>Build number: the ${BUILD_NUMBER} build</li>
                    <li>${CAUSE}</li>
                    <li>Build log:<a href="${BUILD_URL}console">${BUILD}console</a></li>
                    <li>Working Directory:<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>The URL of the project:<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td>
                <b><span style="color: #0B610B">Historical change record</span></b>
            </td>
        </tr>
        <tr>
            <td>
                <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a>
            </td>
        </tr>
        <tr>
            <td>
                <b><span style="color: #0B610B">Build log (last 100 lines)</span></b>
            </td>
        </tr>
        <tr>
            <td>
                <textarea readonly cols="80" rows="10">
                    ${BUILD_LOG, maxLines=100}
                </textarea>
            </td>
        </tr>
    </table>
</body>


</html>

Copy the code
  1. Add build success to Item to send mail

Email Ext does not seem to work in free-style projects, so I use pipeline projects as an example:

Pipeline script:

Pipeline {agent any stages {stage('pull') {steps {checkout(XXXXX) }}} post{// Always {// always execute ${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}', body: '${FILE, path=" emailtemplate.html "}', // to: '[email protected]')}}}Copy the code

You can specify the title, content, and recipient of the message using the Emailext method.

Test results:

Environment variables in EmailExt

We should also learn more about the environment variables in the mail extension, rather than sticking to someone else’s template

There is a small question mark to the right of the Content Token Reference in the last column of the Extended Email Notification under Configure System to view all environment variables. However, the BUILD_NUMBER, BUILD_STATUS, BUILD_URL, and so on used in the template above are mainly used