1. 程式人生 > >jenkins配置郵件及增強版郵件通知

jenkins配置郵件及增強版郵件通知

環境
jenkins:2.5 
作業系統:win7 
伺服器:centos6 
工具:CRT

需求
這個月有那麼一次jenkins在自動升級(SCM)時失敗了,那時剛好晚上7點,直到第二天早上有人點選時,才意識到升級失敗,所以想想,我還是配置下構建失敗的郵件通知吧!

安裝外掛
Email Extension Plugin

(安裝外掛,我喜歡先去下載hpi檔案,然後再去手動安裝)

jenkins預設的郵件通知
我先講解下,預設的。 
jenkins預設就有一個郵件通知,只是太簡單的,不能個性化或者說定製化。

設定系統管理員郵件地址


郵件通知


①SMTP伺服器:如果你使用的是公司郵箱,那麼就詢問你自己公司裡的運維人員吧;他們一般都知道,至少我就是問公司裡的運維人員;要是你打算使用QQ郵箱,那麼你需要設定下,網上有教程;網易郵箱預設開啟。 
說明:SMTP是一種協議 
②使用者預設郵件字尾:根據自己情況去設定 
③勾選使用SMTP認證,使用者名稱:根據自己情況設定,密碼也是。 
④SMTP埠:預設25

配置好了後,可以勾選 測試,在Test e-mail recipient輸入自己的郵箱,看下能否成功。

成功後,我們可以到job中進行配置: 


配置好了後,接下來測試,我下面是故意寫錯echo,使其構建失敗,驗證郵件。

傳送郵件的結果: 


可以看出這個郵件內容純文字,連個超連結都沒有,內容也不夠豐富!

接下來我們使用增強版的郵件通知
系統配置
在安裝好外掛後,你的系統設定裡面會有這麼一個設定:

接上面圖片: 


上面點選高階按鈕後: 


①SMTP server:和之前同理 
②Default user E-mail suffix:根據自己情況填寫 
③勾選Use SMTP Authentication,使用者名稱和密碼填寫自己的 
④SMTP port:預設25 
⑤Default Content Type:郵件文件型別 
⑥Default Recipients:預設接收人列表,已逗號進行分割

其他我都使用預設或者說沒有填寫,點選儲存。

注意: 
上面配置中,凡是以Default開頭的名稱,都可以在job的配置中當做變數使用。比如:預設的收件人地址:在單獨的job中可以這樣使用$DEFAULT_RECIPIENTS。

單個job的單獨配置
在增加構建後操作步驟,新增增強版郵件通知(看紅色圈住的部分): 


之後你會得到:


稍微講解下: 
①Disable Extended Email Publisher:勾選後,郵件就不傳送,看自己的情況嘍,如果你想除錯某些東西,又不想發郵件出去就可以勾選這個。 
②Project Recipient List:收件人地址;多個收件人郵件地址用逗號進行分割;想使用全域性預設配置的話,可以使用$DEFAULT_RECIPIENTS。 
③Project Reply-To List:允許回覆人的地址;想使用系統設定中的預設值的話,可以使用$DEFAULT_REPLYTO; 
④Content Type:郵件文件的型別,可以設定HTML等格式; 
⑤Default Subject:預設主題,也就是郵件標題;同理可以使用$DEFAULT_SUBJECT 
⑥Default Content:預設郵件內容;這裡是關鍵;我這裡使用的是模板${SCRIPT, template="groovy-html.template"};後面會講;當然不想使用模板的話,可以通過使用jenkins自身提供的變數來自己定義; 
⑦Attach Build Log:傳送的郵件是否包含日誌; 
下面幾個預設就好,最後一個Triggers非常關鍵; 
假設最後一個不改的話,郵件是接收不到的,這個是官方留下的一個大坑,一定要自己再新增一個Recipient List。

點選儲存,點選構建後,收到的郵件格式如下: 


遇到的問題
問題一
An attempt to send an e-mail to empty list of recipients, ignored.
1
這個問題,我一直卡著很久,差不多1天吧!始終不知道為什麼收不到郵件

直到Google到這個地方: 
https://stackoverflow.com/a/37167955/6952713

https://issues.jenkins-ci.org/browse/JENKINS-34731?focusedCommentId=257221&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-257221 
才知道,官方給了一個大坑。

問題二
Exception raised during template rendering: No signature of method: hudson.model.FreeStyleBuild.getExactRuns() is applicable for argument types: () values: [] Possible solutions: getActions(), getActions(java.lang.Class) groovy.lang.MissingMethodException: No signature of method: hudson.model.FreeStyleBuild.getExactRuns() is applicable for argument types: () values: [] Possible solutions: getActions(), getActions(java.lang.Class) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58) at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:49) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117) at SimpleTemplateScript3.run(SimpleTemplateScript3.groovy:106) at groovy.text.SimpleTemplateEngine$SimpleTemplate$1.writeTo(SimpleTemplateEngine.java:168) at groovy.text.SimpleTemplateEngine$SimpleTemplate$1.toString(SimpleTemplateEngine.java:180) at hudson.plugins.emailext.plugins.content.ScriptContent.renderTemplate(ScriptContent.java:127) at hudson.plugins.emailext.plugins.content.ScriptContent.evaluate(ScriptContent.java:68) at hudson.plugins.emailext.plugins.content.AbstractEvalContent.evaluate(AbstractEvalContent.java:64) at org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro.evaluate(DataBoundTokenMacro.java:199) at 
1
這個錯誤是我在使用郵件模板時,報的錯! 
我的步驟如下: 
我在參考官方 :https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin 
中的Script content這一章節內容時,下載了兩個Template Examples 
jenkins-matrix-email-html.template 
jenkins-generic-matrix-email-html.template

按照官方教程在jenkins home(/home/jenkins/dataspace)目錄中建立了email-templates資料夾,並把那兩個模板上傳上去了,之後在job配置中的Default Content中寫入:

${SCRIPT, template="jenkins-matrix-email-html.template"}
1
然後構建時,就報錯! 
這一塊也困擾我很久!直到Google到了這麼一段話(網上資料真心少);

http://jenkins-ci.361315.n4.nabble.com/Email-Template-Testing-Exception-td4807117.html

內容如下:

Looks like you are trying to test a matrix template with a non matrix job.

意思是說:

看起來像你在一個非matrix job中測試一個matrix模板

之後又去查詢什麼是matrix job; 
這是一篇關於matrix的官方教程 
Building a matrix project

看到裡面內容卻是構建一個Multi-Configuration Projects;這時就有點迷糊了!接著檢視Matrix Project Plugin這個外掛;裡面的講解有這麼一句話:

Multi-configuration (matrix) project type.

基本可以肯定matrix job就是Multi-configuration project(構建一個多配置專案)。也就是說我要使用jenkins-matrix-email-html.template這個模板就必須建立與之對應的job;

經過測試後,果然可以,配置都是類似的:郵件結果如下: 


那麼matrix job這個用的場景多嗎? 
這是官方給出的經驗:

Experience with Hudson - Building matrix project

其講解到:

比如 你的專案想在jdk 1.4 、1.5、1.6中進行測試,你就可以通過建立這種型別的job;

使用自定義模板
通過上面遇到的問題,我們有時會想自己來定義模板! 
使用自己的模板有兩種方法:

方法一 不使用模板
在Default Content這一欄中,自己通過Token來寫: 
(這裡的token不要翻譯成令牌,因為不知道翻譯成啥,就保留原文token吧)

<!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>
                <h2>
                    <font>來自Mr.Jenkins的郵件通知</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;:${CAUSE}</li>
                    <li>構建日誌&nbsp;:&nbsp;<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                    <li>單元測試報告&nbsp;:<a href="${BUILD_URL}testReport/">${BUILD_URL}testReport/</a></li>
                    <li>工作目錄&nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>

                </ul>
            </td>
        </tr>
                <tr>
            <td><b><font color="#0B610B">構建日誌:</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td><textarea cols="80" rows="30" readonly="readonly"
                    style="font-family: Courier New">${BUILD_LOG}</textarea>
            </td>
        </tr>
    </table>
</body&