3.Jenkins邮件配置
前提
需自行开启邮箱服务器,126,163,qq,sina都可以
目的
当构建后,要将结果以邮件形式发送给指定管理员
插件下载
Email Extension Template
配置
-
Manage Jenkins->Configure System->Jenkins Location
配置发件邮箱
-
Manage Jenkins->Configure System->Extended E-mail Notification
配置邮件服务器
创建Jenkins项目
- 添加Pipeline 脚本
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post { #以上流程执行完后发邮件
always { #不管流程是否报错都执行
emailext(
subject: '项目名:${PROJECT_NAME},第几次构建:${BUILD_NUMBER},构建结果:${BUILD_STATUS}!',
body: '邮件正文,可在Manage Jenkins->Configure System->Extended E-mail Notification->Default Content Type中配置发送html',
to: '476852847@qq.com' #接收邮件的人,多人用,隔开
)
}
}
}
- 执行Bulid,即可收到邮件
发送HTML的通知
最终收到的结果如下:
第一步,修改发送邮件格式
Manage Jenkins->Configure System->Extended E-mail Notification->Default Content Type中配置发送html
第二步,在项目中添加html文件
html内容
<!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>
html存放位置
可调整,调整后下面的路径也要调整
Pipeline脚本
发邮件在下方post{}中
def remote = [:]
remote.name = 'test'
remote.host = '192.168.0.104'
remote.user = 'root'
remote.password = '123'
remote.allowAnyHosts = true
pipeline {
agent any
stages {
stage('拉取代码' ) {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'ff8ba3ca-be5f-4535-bb37-08de5ef208f9', url: 'https://gitee.com/RollBack2010/jekins-study.git']]])
}
}
stage('编译代码 ') {
steps {
sh 'mvn clean package'
}
}
stage('停止服务 ') {
steps {
sshCommand remote: remote, command: "bash /usr/local/project/stop.sh"
}
}
stage('发布运行 ') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'jarPath', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local/project
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target/', sourceFiles: 'target/demo-0.0.1-SNAPSHOT.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
post {
always {
emailext(
subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} -${BUILD_STATUS}!',
body: '${FILE,path="email.html"}', #会去项目根目录找"email.html"文件,路径修改了要同步修改
to: '476852847@qq.com'
)
}
}
}
第三步,执行Build
以上参考传智播客笔记