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

環境
jenkins:2.5 
操做系統:win7 
服務器:centos6 
工具:CRThtml

需求
這個月有那麼一次jenkins在自動升級(SCM)時失敗了,那時恰好晚上7點,直到次日早上有人點擊時,才意識到升級失敗,因此想一想,我仍是配置下構建失敗的郵件通知吧!java

安裝插件
Email Extension Plugincentos

(安裝插件,我喜歡先去下載hpi文件,而後再去手動安裝)服務器

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

設置系統管理員郵件地址運維


郵件通知工具


①SMTP服務器:若是你使用的是公司郵箱,那麼就詢問你本身公司裏的運維人員吧;他們通常都知道,至少我就是問公司裏的運維人員;要是你打算使用QQ郵箱,那麼你須要設置下,網上有教程;網易郵箱默認開啓。 
說明:SMTP是一種協議 
②用戶默認郵件後綴:根據本身狀況去設置 
③勾選使用SMTP認證,用戶名:根據本身狀況設置,密碼也是。 
④SMTP端口:默認25單元測試

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

成功後,咱們能夠到job中進行配置: ui


配置好了後,接下來測試,我下面是故意寫錯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.五、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> </html>

相關文章
相關標籤/搜索