Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

释放双眼,带上耳机,听听看~!

这篇重点介绍发送邮件这个方法,或者叫指令,是因为发送邮件这个功能太常用了。这个指令是mail,然后配合几个属性就可以在代码里发送一个比较完整的邮件。我们知道在jenkins中构建之后一般都会执行结果通知,告知构建人和其他项目人员构建的结果是什么状态,一般是失败,成功,取消三种结果。在通过pipeline代码发送邮件之前,你的需要学会在jenkins服务器上配置好smtp邮件服务器。

1.Jenkins服务器上配置smtp服务

这个我在jenkins基础文章有详细介绍,请移步到https://blog.csdn.net/u011541946/article/details/78075250 去学习如何在jenkins上配置QQ作为邮件发送服务。

Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

上面这个图,可能存在错误,需要勾选使用SSL协议。点击上面测试配置如果发送成功,说明邮件配置成功。

2.方法mail

在pipeline代码中通过mail和配合其他options可以完成发送邮件。邮件内容一般是普通的text或者html格式的。我这篇文章都会介绍。先来看看官网对mail的介绍。

Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

看到有很多选项,有些是可选的(带optional),有些是必须的。接下来我就先写必须部分的发送邮件的pipeline代码。

1)最简单的邮件内容

pipeline代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10      stage("send mail test") {
11          steps{
12              script {
13                  mail to: '570375381@qq.com',
14                    subject: "Running Pipeline: ${currentBuild.fullDisplayName}",
15                    body: "Something is wrong with ${env.BUILD_URL}"
16              }
17          }
18      }
19  }
20}
21
22

收邮件结果截图

Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

收到邮件和代码对比,邮件标题和正文内容都是对的上的。

2)稍微完整的存文本邮件

上面我只用了subject和body两个选项,下面我多使用几个参数。看看效果。

pipeline代码


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
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10      stage("send mail test") {
11          steps{
12              script {
13                  mail to: '5xxx1@qq.com',
14                  cc: 'xxxxx@qq.com',
15                  charset:'UTF-8', // or GBK/GB18030
16                  mimeType:'text/plain', // or text/html
17                    subject: "Running Pipeline: ${currentBuild.fullDisplayName}",
18                    body: "Something is wrong with ${env.BUILD_URL}, just for test send mail via pipeline code"
19              }
20          }
21      }
22  }
23}
24
25

上面我都写了备注,唯一注意的mimeType这个选项默认就是text/plain,下面我要介绍html格式如何写。

发送结果

Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

3)html格式的邮件模板

邮件内容也就是body是可以以html格式来发送的,我这里html+css写的一个简单的邮件模板。具体css内容请看我github项目。我会在module文件里写一个发送邮件的方法,这个方法需要加载css然后写html文件内容和格式,然后pipeline stage文件调这个方法,传相关参数就是可以。

module方法代码


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
1def send_email_results(status,GITBranch,to_email_address_list) {
2    def fileContents = readFile env.WORKSPACE + '/testdata/basic_style.css'
3    def subject = "Jenkins Job : " + env.JOB_NAME + "/" + env.BUILD_ID + " has " +  status
4    def result_url = env.BUILD_URL + "console"
5    
6
7    def text = """
8  <html>
9  <style type="text/css">
10  <!--
11  ${fileContents}
12  -->
13  </style>
14  <body>
15  <div id="content">
16  <h1>Summary</h1>
17  <div id="sum2">
18      <h2>Jenkins Build</h2>
19      <ul>
20      <li>Job URL : <a href='${env.BUILD_URL}'>${env.BUILD_URL}</a></li>
21       <li>Build Result URL : <a href='${result_url}'>${result_url}</a></li>
22      </ul>
23  </div>
24  <div id="sum0">
25  <h2>GIT Branch</h2>
26  <ul>
27  <li>${GITBranch}</li>
28  </ul>
29  </div>
30  </div></body></html>
31  """
32
33    mail body: text, subject: subject,  mimeType: 'text/html', to: to_email_address_list
34}
35

pipeline调用代码


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
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10      stage("init") {
11          steps{
12              script {
13                  module_test = load env.WORKSPACE + "/pipeline/module/pipeline-demo-module.groovy"
14                  println "1 + 1 = 2"
15              }
16          }
17      }
18  }
19  post{
20      failure {
21          script {
22              module_test.send_email_results("Failed","Master","xxxx@qq.com,xxxx@qq.com")
23          }
24      }
25      success {
26          script {
27              module_test.send_email_results("Success","Master","xxxx@qq.com")
28          }
29      }
30  }
31}
32
33

收邮件效果

Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-3-方法mail

我的jenkins 测试job:http://65.49.216.200:8080/job/pipeline_basic_steps/39/

这个功能很常用,所以就提取到方法成模块,如果要更好的邮件模板,需要找前端开发人员进行对应调整。邮件正文还可以显示其他项目相关的参数,变量等。

 

 

 

 

 

 

 

 

 

 

 

 

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

Apache日志分析

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索