Jenkins2 学习系列13 -- 邮件和Slack通知 里发送邮件的内容是写死到了pipeline里,这样不太灵活

emailext (
to: 'mafeifan@qq.com', 
subject: "Job [${env.JOB_NAME}] - Status: ${currentBuild.result?: 'success'}", 
body: 
"""
<p>EXECUTED: Job <b>\' ${env.JOB_NAME}${env.BUILD_NUMBER}\'
</b></p><p>View console output at "<a href= "${env.BUILD_URL}">
${env.JOB_NAME}${env.BUILD_NUMBER}</a>"</p>
<p><i>(Build log is attached.)</i></p>
""")
1
2
3
4
5
6
7
8
9
10

这里我们通过 Config File Provider 插件,创建邮件模板,然后实现复用的目的,具体步骤:

  1. 安装插件,略
  2. 安装后管理页面多了 "Managed files" 菜单项, 进入后点 Add a new Config

image.png

Type 选择 "Extended Email Publisher Groovy Template"

image.png

ID 可以自行填写

image.png

Content 填写如下

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>  
</head>
 
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td>(本邮件是程序自动下发的,请勿回复!)</td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称 : ${JOB_NAME}</li>
                    <li>构建编号 : 第${BUILD_NUMBER}次构建</li>
                    <li>触发原因 : ${CAUSE}</li>
                    <li>构建日志 : <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>工作目录 : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>

        <tr>
            <td><b style="color='#0B610B'">历史变更记录:</b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="&nbsp;&nbsp;&nbsp;&nbsp;%p"}
            </td>
        </tr>
        <tr>
            <td><b style="color='#0B610B'">构建日志(最后100行):</b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><p><pre>${BUILD_LOG, maxLines=100}</pre></p></td>
        </tr>
    </table>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  1. 最后修改流水线脚本,通过插件提供的configFileProvider实现获取文件 如果是脚本式流水线。
node () {
    stage('email'){
        echo "测试发送邮件"
        // 设置生成模板文件
        configFileProvider([configFile(fileId: '0ad43176-c202-4ebc-aaff-441ef79f49d8',
                                       targetLocation: 'email.html', 
                                       variable: 'failt_email_template')]) {
            //  读取模板
            template = readFile encoding: 'UTF-8', file: "${failt_email_template}"
            //  发送邮件
            emailext(subject: '任务执行失败',
            	     attachLog: true,
            	     recipientProviders: [requestor()], 
            	     to: '32*****47@qq.com',
            	     body: """${template}""")
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

如果是声明式流水线

post {
        always {
            configFileProvider([configFile(fileId: 'email-groovy-template-cn', targetLocation: 'email.html', variable: 'content')]) {
               script {
                   template = readFile encoding: 'UTF-8', file: "${content}"
                   emailext(
                       to: "${email_to}",
                       subject: "Job [${env.JOB_NAME}] - Status: ${currentBuild.result?: 'success'}",
                       body: """${template}"""
                   )
               }
           }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

大致流程,configFileProvider 通过传入的 fileId 读取具体文件,然后通过 targetLocation 给模板起起名,假如 WORKSPACE=/var/jenkins_home/workspace/email-test,targetLocation: 'email.html',执行时,通过构建日志你会发现 copy managed file [Groovy Email Template] to file:/var/jenkins_home/workspace/email-test/email.html

# 参考

Jenkins Email 邮件配置

上次更新: 9/8/2019, 9:59:21 PM