DEVOPS technical practices _17: Jenkins mail function to send mail using the extended

A ready environment

1.1 Plug Email Extension

System Management - Management Plug - install Email Extension Plug

1.2 Configuration

Jenkins mailbox configuration global configuration: System Management - System Settings - complete mailbox configuration - test is configured correctly, and then configure extended mail features

To complete the project job mailbox configuration: Configuration - to increase post-build steps Editable Email Notification

1.3 Configuration extensions

1.4 Construction of a task

Click construct 1.5

 

1.6 build results

content of email

  

Second use custom message

2.1 Configuration

Here to add their own definition of mail

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><h2>
                    <font color="#0000FF">构建结果 - ${BUILD_STATUS}</font>
                </h2></td>
        </tr>
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>项目名称&nbsp;:&nbsp;${PROJECT_NAME}</li>
                    <li>构建编号&nbsp;:&nbsp;第${BUILD_NUMBER}次构建</li>
                    <li>SVN&nbsp;版本:&nbsp;${SVN_REVISION}</li>
                    <li>触发原因:&nbsp;${CAUSE}</li>
                    <li>构建日志:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>构建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                    <li>工作目录&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                    <li>项目&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                </ul>
            </td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">Changes Since Last
                        Successful Build:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                </ul> ${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>Failed Test Results</b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><pre
                    style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                <br /></td>
        </tr>
        <tr>
            <td><b><font color="#0B610B">构建日志 (最后 100行):</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <!-- <tr>
            <td>Test Logs (if test has ran): <a
                href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
                <br />
            <br />
            </td>
        </tr> -->
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
            </td>
        </tr>
    </table>
</body>
</html>

2.2 构建测试

控制台输出

邮件内容

三 使用模板发送邮件

本次实验使用的jenkins使用的是docker容器启动的,具体参考https://www.cnblogs.com/zyxnhr/p/11795523.html

3.1 goovy模板

https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template 参考别人的,也可以自己去jenkins官网下载

把文件拷进容器

[root@node6 opt]# docker cp /opt/groovy-html.template   jenkins_prod:/var/jenkins_home

[root@node6 opt]# docker exec -it jenkins_prod ls -lrt /var/jenkins_home

-rw-r--r--  1 root    root    10601 Nov  8  2019 groovy-html.template

3.2 进入容器配置

[root@node6 opt]# docker exec -it jenkins_prod /bin/bash

jenkins@b119e7f28b50:/$ cd /var/jenkins_home/

jenkins@b119e7f28b50:~$ mkdir email-templates

jenkins@b119e7f28b50:~/email-templates$ cp ../groovy-html.template  ./

jenkins@b119e7f28b50:~/email-templates$ ls -l

-rw-r--r-- 1 jenkins jenkins 10601 Nov  4 21:56 groovy-html.template

或者直接在这里下载

jenkins@b119e7f28b50:~/email-templates$ wget https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template

3.3 测试模板

3.4 配置

测试成功之后,修改这里

 构建一个成功的

 检查邮件内容

 

四 使用pipeline语法

4.1 生成流水线语法

使用流水线语法片段生成器,生成邮件服务语法,具体做法可参考前面配置

4.2 简单的流水线语法

这里只有简单的两个阶段,邮件发送,我想要的是不管前面是否构建成功,都会发送邮件通知,在使用脚本时流水线语法,没有研究出post的触动,所以这里使用声明式语法格式,post设定always,一直触发动作

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            echo 'Hello World'
         }
      }
   }
   post{                                                         //这里使用post语法触发,alway是是无论前面执行是否成功,都会出发邮件动作
       always{
           emailext attachLog: true, 
           body: '${SCRIPT, template="groovy-html.template"}', 
           recipientProviders: [developers()], 
           subject: '$DEFAULT_SUBJECT', 
           to: '[email protected]'           
       }       
   }
}

4.3 构建测试结果

控制台输出

Started by user ning
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipelintest
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] emailext
Sending email to: 283365585@qq.com
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

4.4 邮件内容

 参考文档:https://www.cnblogs.com/apple2016/p/7763002.html

         https://www.cnblogs.com/sparkdev/p/7397394.html

 

邮件部署完成

Guess you like

Origin www.cnblogs.com/zyxnhr/p/11823680.html