Jenkins 配置邮箱服务器发送构建结果
1. 安装Email Extension
插件
2. 设置邮箱并开启Imap
点击开启
Imap
开启成功
服务器地址:
POP3服务器: pop.163.com
SMTP服务器: smtp.163.com
IMAP服务器: imap.163.com
安全支持:
POP3/SMTP/IMAP服务全部支持SSL连接
其他邮箱类似
3. Jenkins
设置邮箱相关参数
Manage Jenkins
->Configure System
1. 设置系统发件人邮箱
2. 设置邮件参数
这里密码就是之前获取的授权密码
3. 设置Jenkins
默认邮箱信息
这里密码就是之前获取的授权密码
下面的测试发送邮件,我使用的163邮箱,填其他邮箱测试会报544错误,原因是被163邮箱当成垃圾邮件,这里可以先填入发件人邮件地址,测试成功即可
此时便可以收到邮件
4. 编写邮件内容
在具体项目根目录编写email.html
,并把文件推送到Gitlab
,内容如下:
<!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>项目名称 : ${PROJECT_NAME}</li>
<li>构建编号 : 第${BUILD_NUMBER}次构建</li>
<li>触发原因: ${CAUSE}</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">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=" %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><textarea cols="80" rows="30" readonly="readonly"
style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
</td>
</tr>
</table>
</body>
</html>
其中的变量可以在Jenkins
如下位置查看
Manage Jenkins
->Configure System
5. 编写Jenkinsfile
添加构建后发送邮件
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'cce455e2-ba69-459e-93bc-c58ce1e6278f', url: 'ssh://git@192.168.2.4:23456/itheima_group/web_demo.git']]])
}
}
stage('build code') {
steps {
sh label: '', script: 'mvn clean package'
}
}
stage('deploy code') {
steps {
deploy adapters: [tomcat9(credentialsId: 'da6aa960-cb2b-415f-a31e-e161ba704f28', path: '', url: 'http://192.168.2.5:8080')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: 'xxx@qq.com'
}
}
}
其中,post
代码片段,在jenkins
配置项目中的
post {
always {
// One or more steps need to be included within each condition's block.
}
}
发送邮件的代码生成
emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: 'xxx@qq.com'
将项目代码提交gitlab
PS:邮件相关全局参数参考列表:
系统设置->Extended E-mail Notification
->Content Token Reference
,点击旁边的?号
6. 测试
此时执行Build Now
成功发送构建邮件