前段时间给公司内部项目搭建了一套接口自动化框架,基于实际使用,需要配置自动发送邮件功能,将
执行结果发送给相关负责人。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",发送测试邮件。