本文内容:Jenkinsfile+Email Extension Plugin插件取代jenkins自带邮件发送功能,自定义发送邮件

环境:Jenkins+Email Extension Plugin,拉取github仓库代码

注意事项:由于我是手写jenkinsfile,不是利用jenkins web界面进行配置,所以这里只讲述jenkinsfile+Email Extension Plugin,如何在jenkins web界面进行配置不在这里叙述,这里也不进行讲解Jenkinsfile语法。

配置步骤

    1.在Jenkins中安装Email Extension Plugin插件

        在Jenkins首页面板中找到系统管理,点击进入

                              Jenkinsfile+git+Email Ext配置邮件发送                              

         在系统管理 页面找到插件管理,点击进入

                                Jenkinsfile+git+Email Ext配置邮件发送

        在插件管理页面点击可选插件,在右上角搜索Email Extension Plugin,点击安装

                                     Jenkinsfile+git+Email Ext配置邮件发送

    2.配置Email Extension Plugin代替Jenkins自带邮件通知功能

        在系统管理页面找到系统设置,点击进入

                                    Jenkinsfile+git+Email Ext配置邮件发送

        在系统设置页面找到Jenkins Location,填写你的Jenkins URl,和你系统管理员邮件地址

                              Jenkinsfile+git+Email Ext配置邮件发送        

        接着在系统设置页面找到Extended Email Notification:

                                   Jenkinsfile+git+Email Ext配置邮件发送

        填写 SMTP server:SMTP server填什么?

                                                  网易免费企业smtp:smtp.ym.163.com

                                                  网易付费企业smtp:smtpqiye.163.com

                                                  这里列举了两个网易的,其他的可以百度

        将Default Content Type选为HTML(text/html),以html方式发送邮件

      勾选Use SMTP Authentication

      在User Name填写发送邮箱,要和上一步 系统管理员邮件地址一致,否则会报错,Jenkins默认用该账号发送邮件

      在Password填写邮箱密码 

      填写SMTP port,我这里为465

      Charset默认为UTF-8 

      以上在Jenkins web前端的配置就结束了

    3.编写邮件模板

        我这里找了一个邮件模板,放在email.html文件,文件内容如下

<!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>

        该插件的好处就是可以自定义邮件风格,只要编写属于你自己的email.html即可。以上模板是通用的,直接

        拷贝即可,不用担心环境变量不支持。当然,你也可以编写属于自己风格的html。

        将email.html文件上传到github中。

    4.编写Jenkinsfile(为啥把我的图打上它的log。。。。。。)

                                 Jenkinsfile+git+Email Ext配置邮件发送

        将邮件的设置放在emailext中

                subject:设置邮件主题

                body:设置邮件内容,这里使用${FILE,path="email.html"},将内容变为刚才设置的email.html中的内容

                to:就是你将要发送的邮箱

        上面是将邮件发送放在steps中,这样一旦构建失败,就不会发送邮件,所以一般是将发送邮件放在post模块

        我这里是设置always,可根据实际需求设置success,failed情况。

                                       Jenkinsfile+git+Email Ext配置邮件发送

        好了,经过以上步骤,你执行该Pipeline就可以在构建后发送邮件到你配置的邮箱中了。

       

    深入一下email.html

        通过上的email.html代码可以看出,该email.html和正常html没什么区别,唯一区别就是调用了Jenkins中的变量,

         在邮件中输出了变量的值。email-ext插件允许使用变量来动态插入数据到邮件的主题和内容主体中。所以我们

         只要知道有哪些变量,整个email.html文件就可以自由编写了

           下面介绍一下有关邮件的变量

        全局邮件变量
        ${FILE,path="PATH"} 包括指定文件(路径)的含量相对于工作空间根目录
        path文件路径,好比你用jenkins+git,他执行Pipeline的时候,找文件的路径就是从拉下来的代码开始
        ${BUILD_NUMBER} 当前构建的编号
        ${JOB_DESCRIPTION} 项目描述
        ${SVN_REVISION} svn版本号。还支持Subversion插件出口的SVN_REVISION_n版本
        ${CAUSE} 显示谁、通过什么渠道触发这次构建
        ${CHANGES } -显示上一次构建之后的变化
        ${BUILD_ID}显示当前构建生成的ID
        ${PROJECT_NAME} 显示项目的全名
        ${PROJECT_DISPLAY_NAME} 显示项目的显示名称
        ${JENKINS_URL} 显示Jenkins服务器的url地址
        ${BUILD_LOG_MULTILINE_REGEX}按正则表达式匹配并显示构建日志。
        ${BUILD_LOG} 最终构建日志。
        ${PROJECT_URL} 显示项目的URL地址。
        ${BUILD_STATUS} -显示当前构建的状态(失败、成功等等)
        ${BUILD_URL} -显示当前构建的URL地址。
        ${CHANGES_SINCE_LAST_SUCCESS} -显示上一次成功构建之后的变化。
        ${CHANGES_SINCE_LAST_UNSTABLE} -显示显示上一次不稳固或者成功的构建之后的变化。
        ${FAILED_TESTS} -如果有失败的测试,显示这些失败的单元测试信息。
        ${JENKINS_URL} -显示Jenkins服务器的地址。(你能在“系统配置”页改变它)。
        ${PROJECT_URL} -显示项目的URL。
        ${SVN_REVISION} -显示SVN的版本号。
        ${TEST_COUNTS} -显示测试的数量。

        如果你要查看所有可用的变量,你可以点击配置页的Content Token Reference的问号获取详细的信息。

    

    深入一下Jenkinsfile中的emailext

        通过上面的Jenkinsfile可以看出在jenkinsfile中配置邮件通知就是通过emailext模块

        emailext的写法为

        emailext (

            属性1:参数,

            属性2:参数,

            ...

            属性n:canshu 

        )

             整个邮件发送就是通过这些属性进行配置,下面介绍一下emailext中的属性(转自Jenkins官方文                                   档https://jenkins.io/doc/pipeline/steps/email-ext/#emailext-extended-email    

  •     subject
  •     Type: String
  •     body
  •     Type: String
  •     attachLog (optional)
  •     Type: boolean
  •     attachmentsPattern (optional)
  •     Type: String
  •     compressLog (optional)
  •     Type: boolean
  •     from (optional)
  •     Type: String
  •     mimeType (optional)
  •     Type: String
  •     postsendScript (optional)
  •     Type: String
  •     presendScript (optional)
  •     Type: String
  •     recipientProviders (optional)
        Array/List        Nested Choice of Objects
  • culprits
        Sends email to the list of users who committed a change since the last non-broken build till now. This list at least always       include people who made changes in this build, but if the previous build was a failure it also includes the culprit list          from there.
  • developers
        Sends email to all the people who caused a change in the change set.
  • brokenTestsSuspects

      Sends email to the list of users suspected of causing a unit test to begin failing. This list includes committers and        requestors of the build where the test began to fail, and those for any consecutive failed builds prior to the build in           which the test began to fail.

    • brokenBuildSuspects
    Sends email to the list of users suspected of causing the build to begin failing.
  • recipients
Sends email to the list of recipients defined in the "Project Recipient List."
requestor
Sends email to the user who initiated the build.
upstreamDevelopers
Sends email to the list of users who committed changes in upstream builds that triggered this build.
replyTo (optional)
  • Type: String
to (optional)
  • Type: String

emailextrecipients: Extended Email Recipients

  • recipientProvidersArray/List
    Nested Choice of Objects
  • culprits
Sends email to the list of users who committed a change since the last non-broken build till now. This list at least always include people who made changes in this build, but if the previous build was a failure it also includes the culprit list from there.
developers
Sends email to all the people who caused a change in the change set.
brokenTestsSuspects
Sends email to the list of users suspected of causing a unit test to begin failing. This list includes committers and requestors of the build where the test began to fail, and those for any consecutive failed builds prior to the build in which the test began to fail.
  • brokenBuildSuspects
        Sends email to the list of users suspected of causing the build to begin failing.
recipients
        Sends email to the list of recipients defined in the "Project Recipient List."
requestor
        Sends email to the user who initiated the build.
upstreamDevelopers
        Sends email to the list of users who committed changes in upstream builds that triggered this build.


        以上就是关于jenkinsfile配置邮件发送的全部步骤了,有不足之处欢迎指出

        

        更多关于Email Extension Plugin插件,及在jenkinsfile中的写法请移步

        jenkins官方文档对于Email-ext+plugin插件介绍

        https://jenkins.io/doc/pipeline/steps/email-ext/#emailext-extended-email

        Jenkisn社区对于Email-ext+plugin插件介绍:

        https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin

        

相关文章: