有手就行9——项目构建细节4-配置邮箱服务器发送构建结果

有手就行9——项目构建细节4-配置邮箱服务器发送构建结果

 

安装Email Extension插件

 

 

Jenkins设置邮箱相关参数

Manage Jenkins->Configure System

 

 

有此选项设置

 

在添加邮件地址前,先开启邮箱这些功能:

 

 

这些东西后面有用

尤其授权码:切记

 

接着做:

jenkins本地系统配置:

 

点击添加:

 

 

 

 

 

 

 

 

 

 

 

准备邮件内容

切换提交分支master

 

在项目根目录编写email.html,并把文件推送到Gitlab,内容如下:

 

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!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>

  

然后把上面的邮件模板提交到gitlabmaster分支上

基本操作就不贴图了

 

 

 

然后 编写Jenkinsfile添加构建后发送邮件添加红色代码段 post部分

最后的to地址要是你的收件地址 格式不能转行

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
pipeline {
    agent any
 
    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'd5bb0e98-15f2-477f-8db7-2c33ecc6c644', url: 'git@20.0.0.20:niuma/web_demo.git']]])
            }
        }
        stage('build project') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('deploy item') {
            steps {
                deploy adapters: [tomcat8(credentialsId: '38dcb730-8901-41bb-b8d0-d1500aa9cf79', path: '', url: 'http://20.0.0.40:8080/')], contextPath: null, war: 'target/*.war'
            }
        }
    }
    post {
            always {
                emailext(
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}', to: '1321519531@qq.com'
                )
            }
        }
}

  

 

编写好就把jenkinsfile提交

 

 

然后去测试:

构建一个流水线,然后等待邮箱收件:

 

 

posted @   隐姓埋名4869  阅读(67)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示