Jenkins email "modifiable template"

Posted by adamhhh on Thu, 10 Mar 2022 07:41:36 +0100

This is the first day for me to participate in the 2022 first update challenge. See the activity details: 2022 first revision challenge

Last one, Jenkins Pipeline combines Gitlab to realize automatic construction of Node project We have implemented the function of automatic construction.

In teamwork, when the project is abnormally "or successfully" constructed, it needs to notify relevant personnel by email, which involves the function of Jenkins email sending.

Install Email Extension

Email Extension The plugin allows you to configure email notifications. Through this plug-in, you can customize the sender, receiver and content of email.

Manage Jenkins -> Manage Plugins -> Available . Search Email Extension installation

Configure Email Extension parameters

Manage Jenkins -> Configure System -> Extended E-mail Notification

Point 1: Simple Mail Transfer Protocol (SMTP) applies by yourself. If you don't know how to operate, please find the answer online. (maybe it's not difficult...)

Point 2: the default email address.

Build script

use Jenkins Pipeline combines Gitlab to realize automatic construction of Node project Jenkinsfile script.

pipeline {
    agent any
    
    tools { 
        nodejs "nodejs" 
    }
    
    stages {
        stage('Dependency') {
            steps {
                sh 'npm install'
            }
        }
        stage('Build') { 
            steps {
                sh 'npm run clean' 
                sh 'npm run build' 
            }
        }
    }
    
    // Added script content
    post {
        always {
            emailext body: '${DEFAULT_CONTENT}',
                subject: '${DEFAULT_SUBJECT}',
                to: '${DEFAULT_RECIPIENTS}',
                from: '${env.DEFAULT_FROM_EMAIL}'
        }
    }
}

This is obvious: the default sending mailbox, the default receiving mailbox, the default sending subject, and the default sending content. Is to obtain the value set in the previous step. The effect is as follows:

Build failed:

Successfully built:

Comparing the above two figures, the construction failed and the construction succeeded. The font of the construction result is actually different colors. It's amazing

If you look at the screenshot in configuring Email Extension parameters in the previous section, you may notice:

<h2><font color="${BUILD_STATUS}">Construction results - ${BUILD_STATUS}</font></h2>

color="${BUILD_STATUS}" well, with this, the font of construction results returned in different states is different in color? Driven by us, we build pipeline's always - > changed. Trigger the build information from failure to success, and the following results are obtained:

It seems that I think too much. It's not different states and different colors. However, if you don't mind this bug, you can use

Upgrade requirements

Sending mail needs to be distinguished by specific colors according to different states. For example: success - > #27ae60, failure - > #e74c3c, other colors - > #f4e242

Eh, this is not easy? Just add judgment conditions to the default sending template.

However, if you don't support it, it's too bad. You have to find another way ~ (heavy fog)

After a search, Groovy Template can solve this problem.

Let's change the following pipeline script:

# ...
post {
    always {
        emailext body: '''${SCRIPT, template="my-email-template"}''',
            subject: '${DEFAULT_SUBJECT}',
            to: '${DEFAULT_RECIPIENTS}',
            from: "${env.DEFAULT_FROM_EMAIL}"
    }
}
# ...

Let's build it. The build was successful, but the email received:

Groovy Template file [my-email-template] was not found in $JENKINS_HOME/email-templates.

Email prompt, $Jenkins in the machine_ There is no my email template template under home / email templates. (laughing and crying)

What if you don't have permission? Can we do it in other ways?

Look up the information again, eh, yes- Email Extension Plugin is not loading groovy template added via Config File Provider Plugin . We solve this problem through the following configuration file:

Step 1: operate on jenkins platform, manage jenkins - > managed files

Step 2: click Add a new Config

Step 3: click Extended Email Publisher Groovy Template and click Submit to Submit

Step 4: enter the name and related content. Please remember the name and use it later.

Due to the length of the screenshot, the Content here is not completely intercepted. Please stamp the complete Content Groovy_template.html , the key codes are as follows:

  .tr-title {
    background-color: <%= (build.result == null || build.result.toString() == 'SUCCESS') ? '#27AE60' : build.result.toString() == 'FAILURE' ? '#E74C3C' : '#F4E242' %>;
  }

For the complete official template of Groovy, please see Official template Groovy Email Template

Old fellow, (old laugh)

Step 5: fix your pipeline file. Make sure you enter the correct name "managed:Groovy Email Template".

# ...
post {
    changed {
        emailext body: '''${SCRIPT, template="managed:Groovy Email Template"}''',
            subject: '${DEFAULT_SUBJECT}',
            to: '${DEFAULT_RECIPIENTS}',
            from: "${env.DEFAULT_FROM_EMAIL}"
    }
}
# ...

Everything is ready, we rebuild. The results are as follows:

Mmm ~ the effect is good (sprinkle flowers) 🎉🎉🎉) end

What other methods do readers have? You can leave a message. For more information, please stamp Jimmy Github Blog