前段時間給公司內部專案搭建了一套介面自動化框架,基於實際使用,需要配置自動傳送郵件功能,將
執行結果傳送給相關負責人。Jenkins本身也提供了一個郵件通知功能,但在提供詳細的郵件內容、自定義郵
件格式、自定義郵件配置等方面有一定的侷限性。本文圍繞Jenkins郵件外掛來講一下郵件配置,由於公司內
部保密協議,我將企業郵箱換成QQ郵箱來講解。
安裝郵箱外掛
在Jenkins主頁點選Manage Plugins,選擇Available,在搜尋欄中輸入“Email Extension”,安裝即可,
我這裡已經安裝過,所以顯示在Updates中。
開通POP3/SMTP服務
使用QQ郵箱需要開通這個服務,後續在Jenkins郵件配置中要用到。登入QQ郵箱,點選首頁的設定-賬戶,
驗證手機號或密保來開啟POP3/SMTP服務。開通後點選生成授權碼,複製授權碼備用。
在Jenkins中進行設定
配置Jenkins Location
配置Extended E-mail Notification
SMTP Server:smtp.qq.com SMTP Port:465 SMTP Username:QQ郵箱地址 SMTP Password:剛剛生成的授權碼 Default user E-mail suffix:@qq.com Charset:UTF-8 Default Content Type:HTML(text/html) Default Recipients:QQ郵箱地址
配置E-mail Notification
填寫內容和Extended E-mail Notification中一致即可,勾選Use SSL。
配置郵件通知
進入需要配置的專案,點選configure>>>Post-build Actions,增加構建步驟Extended E-mail Notification。
專案使用過程中,對郵件內容模板進行了更改,增加了更多需要的內容,抓取了部分執行日誌。
<!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 class="table" width="1000" border="1" cellspacing="0" cellpadding="5"> <caption align="center"><h3>專案構建資訊</h3></caption> <thead> <tr> <th>構建專案-<b>${PROJECT_NAME}</b></th> </tr> </thead> <tbody> <tr> <td><font color="#CC0000"><b>構建結果 - ${BUILD_STATUS}</b></font></td> </tr> <tr> <td>測試數量-${TEST_COUNTS, var="total"}</td> </tr> </tbody> <tr> 本郵件由系統自動發出,無需回覆!<br/> 各位同事,大家好,以下為${PROJECT_NAME}專案構建資訊</br> </tr> <tr> <td><br/> <b><font color="#0B610B">構建報告</font></b> <hr size="2" width="100%" align="center"/> <ul> <li>構建報告:<a href="${BUILD_URL}allure/">${PROJECT_URL}allure/</li> </ul> </td> </tr> <tr> <td><br/> <b><font color="#0B610B">構建資訊</font></b> <hr size="2" width="100%" align="center"/> <ul> <li>專案名稱: ${PROJECT_NAME}</li> <li>構建編號: 第${BUILD_NUMBER}次構建</li> <li>觸發原因:${CAUSE}</li> <li>構建狀態:${BUILD_STATUS}</li> <li>構建日誌:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> <li>構建 Url: <a href="${BUILD_URL}">${BUILD_URL}</a></li> <li>工作目錄: <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li> <li>專案 Url: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li> </ul> </td> </tr> <tr> <td> <b><font color="#0B610B">構建詳細資訊</font></b> <hr size="2" width="100%" align="center"/> <ul> <li>BRANCH_NAME: ${BRANCH_NAME}</li> <li>CHANGE_ID: ${CHANGE_ID}</li> <li>CHANGE_URL: ${CHANGE_URL}</li> <li>CHANGE_TITLE: ${CHANGE_TITLE}</li> <li>CHANGE_AUTHOR: ${CHANGE_AUTHOR}</li> <li>CHANGE_AUTHOR_DISPLAY_NAME: ${CHANGE_AUTHOR_DISPLAY_NAME}</li> <li>CHANGE_AUTHOR_EMAIL: ${CHANGE_AUTHOR_EMAIL}</li> <li>CHANGE_TARGET: ${CHANGE_TARGET}</li> <li>BUILD_NUMBER: ${BUILD_NUMBER}</li> <li>BUILD_DISPLAY_NAME: ${BUILD_DISPLAY_NAME}</li> <li>BUILD_ID: ${BUILD_ID}</li> <li>JOB_NAME: ${JOB_NAME}</li> <li>JOB_BASE_NAME: ${JOB_BASE_NAME}</li> <li>BUILD_TAG: ${BUILD_TAG}</li> <li>EXECUTOR_NUMBER: ${EXECUTOR_NUMBER}</li> <li>NODE_NAME: ${NODE_NAME}</li> <li>NODE_LABELS: ${NODE_LABELS}</li> <li>WORKSPACE: ${WORKSPACE}</li> <li>JENKINS_HOME: ${JENKINS_HOME}</li> <li>JENKINS_URL: ${JENKINS_URL}</li> <li>BUILD_URL: ${BUILD_URL}</li> <li>JOB_URL: ${JOB_URL}</li> <li>GIT_COMMIT: ${GIT_COMMIT}</li> <li>GIT_PREVIOUS_COMMIT: ${GIT_PREVIOUS_COMMIT}</li> <li>GIT_PREVIOUS_SUCCESSFUL_COMMIT: ${GIT_PREVIOUS_SUCCESSFUL_COMMIT}</li> <li>GIT_BRANCH: ${GIT_BRANCH}</li> <li>GIT_LOCAL_BRANCH: ${GIT_LOCAL_BRANCH}</li> <li>GIT_URL: ${GIT_URL}</li> <li>GIT_COMMITTER_NAME: ${GIT_COMMITTER_NAME}</li> <li>GIT_AUTHOR_NAME: ${GIT_AUTHOR_NAME}</li> <li>GIT_COMMITTER_EMAIL: ${GIT_COMMITTER_EMAIL}</li> <li>GIT_AUTHOR_EMAIL: ${GIT_AUTHOR_EMAIL}</li> <li>MERCURIAL_REVISION: ${MERCURIAL_REVISION}</li> <li>MERCURIAL_REVISION_SHORT: ${MERCURIAL_REVISION_SHORT}</li> <li>MERCURIAL_REVISION_NUMBER: ${MERCURIAL_REVISION_NUMBER}</li> <li>MERCURIAL_REVISION_BRANCH: ${MERCURIAL_REVISION_BRANCH}</li> <li>MERCURIAL_REPOSITORY_URL: ${MERCURIAL_REPOSITORY_URL}</li> <li>SVN_REVISION: ${SVN_REVISION}</li> <li>SVN_URL: ${SVN_URL}</li> </ul> </td> </tr> <tr> <td> <h4><font color="#0B610B">失敗用例</font></h4> ${FAILED_TESTS}<br/> </td> </tr> <tr> <td> <h4><font color="#0B610B">最近提交(#$GIT_REVISION)</font></h4> <!-- <ul> ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat=" <li>%d [%a] %m</li> "} </ul> <--> </td> </tr> <tr> <td> <b><font color="#0B610B">變更資訊:</font></b> <hr size="2" width="100%" align="center"/> </td> </tr> <tr> <td> <ul> <li> 上次構建成功後變化 : ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"} </li> </ul> </td> </tr> <tr> <td> <ul> <li>上次構建不穩定後變化 : ${CHANGES_SINCE_LAST_UNSTABLE, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"} </li> </ul> </td> <tr> <td> <ul> <li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li> </ul> </td> </tr> <tr> <td> <ul> <li>變更集:${JELLY_SCRIPT,template="html"}</li> </ul> </td> </tr> <tr> <td><b><font color="#0B610B">構建日誌 (最後 200行):</font></b> <hr size="2" width="100%" align="center"/> </td> </tr> <tr> <td><textarea cols="120" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=200}</textarea> </td> </tr> </table> </body> </html>
測試構建郵件
相關的配置都完成後,在Jenkins配置介面勾選"Test configuration by sending test e-mail",傳送測試郵件。