配置邮件服务器地址:https://blog.csdn.net/shm19990131/article/details/107496877
在项目根目录编写 email.html ,并把文件推送到 Gitlab
如果有多个分支,那么每个分支内 根目录都要有此 email.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>
<ht size="2" width="100%" byte="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}consolea>li>
<li>构建 Url : <a href="${BUILD_URL}">${BUILD_URL}a>li>
<li>工作目录 : <a href="${PROJECT_URL}workflow-stage">${PROJECT_URL}workflow-stagea>li>
<li>项目 : Url : <a href="${PROJECT_URL}">${PROJECT_URL}a>li>
ul>
td>
tr>
<tr>
<td><font color="#0B610B">Changes Since Last
Successful Build:font>b>
<hr size="2" width="100%" byte="center" />td>
tr>
<tr>
<td>
<ul>
<li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changesa>li>
ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%mpre>",pathFormat="%p"}
td>
tr>
<tr>
<td><b><font color="#0B610B">Failed Test Resultsfont>b>
<hr size="2" width="100%" byte="center" />td>
tr>
<tr>
<td><pre
style="font-size: 11pt; font-family: Tahoma,Aarial,Helvetica,sans-serif">$FAILED_TESTSpre>
<br />
td>
tr>
<tr>
<td><font><font color="#0B610B">构建日志(最后100行):font>b>
<hr size="2" width="100%" byte="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>
pipeline 采用 Post - always 永久提交的方式,来发送邮件,具体脚本格式如下:
post { #提交
always { #不论结果如何都提交
emailext ( #提交一个邮件
subject: '\'构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}\'', #指定邮件标题
body: '${FILE,path="email.html"}', #指定邮件内容
to: '[email protected]' #指定邮件发到哪里,(指定邮箱)
)
}
}
完整 jenkinsfile脚本,修改后上传到 Gitlab 项目根目录,如下:
pipeline {
agent any
stages {
stage('pull code') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-manven', url: '[email protected]:cheliang/test2.git']]])
}
}
stage('build project') {
steps {
sh label: '', script: '''echo "======= 开始打包 ========"
source /etc/profile
mvn clean package
echo "======= 打包结束、开始部署 ======="'''
}
}
stage('publish project') {
steps {
deploy adapters: [tomcat9(credentialsId: 'b73170b6-fa08-4350-9d35-530b1eb19b75', path: '', url: 'http://192.168.168.5:8080/')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext (
subject: '\'构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}\'',
body: '${FILE,path="email.html"}',
to: '[email protected]'
)
}
}
}