DEVOPS技術實踐_17:Jenkins使用擴展郵件功能發送郵件

一 環境準備

1.1 安裝插件Email Extension

系統管理-管理插件-安裝Email Extension插件html

1.2 配置

配置jenkins郵箱的全局配置:系統管理-系統設置-完成郵箱配置-測試是否配置正確,而後配置擴展郵件功能node

完成項目job的郵箱配置:配置-增長構建後操做步驟Editable Email Notificationgit

1.3 配置擴展功能

1.4 構建一個任務

1.5 點擊構建

 

1.6 構建結果

郵件內容github

  

二 使用自定義郵件

2.1 配置

此處添加爲本身定義的郵件docker

內容api

<!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 構建測試

控制檯輸出bash

郵件內容app

三 使用模板發送郵件

本次實驗使用的jenkins使用的是docker容器啓動的,具體參考https://www.cnblogs.com/zyxnhr/p/11795523.htmlide

3.1 goovy模板

https://github.com/jenkinsci/email-ext-plugin/blob/master/src/main/resources/hudson/plugins/emailext/templates/groovy-html.template 參考別人的,也能夠本身去jenkins官網下載post

把文件拷進容器

[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: '283365585@qq.com' } } }

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

 

郵件部署完成

相關文章
相關標籤/搜索