Jenkins配置邮箱服务发送

Jenkins配置邮箱服务发送

安装Email Extension Template 插件`

image-20220215163219994

image-20220215163315357


邮箱开启smtp服务

在邮箱的设置-->账户 页面,点击开启POP3/SMTP 服务

注:qq 邮箱的smtp 服务器地址:smtp.qq.com .其加密ssl 使用端口 465


image-20220217075545298

image-20220217075809207


Jenkins 设置邮箱相关参数

Manage Jenkins->Configure System

image-20220215163538622


在Jenkins本地系统配置,配置系统管理员邮箱地址。System Admin e-mail address

image-20220217080523170


配置邮箱通知设置。 1, 填写smtp 服务器地址(SMTP server); 2,填写smtp 端口(SMTP port),不加密是25端口,ssl加密则是465端口 ;3,点击高级配置(advance)

image-20220217081114425


凭证Credentials ,点击Add ,下拉,选择Jenkins

image-20220217081423567

Kind 类型 使用 Username with passwd ; Username 输入邮箱地址

Password 输入邮箱的授权码!!!!!!!!


image-20220217081702629


勾选Use SSL ,使用ssl加密

image-20220217081851191


Default user e-mail suffix ,默认邮箱后缀

Default Content Type 邮件发送的格式使用HTML

Default Recipients 接收地址(别人发给你的)

image-20220217082112307



在E-mail Notification 邮箱通知设置,SMTP server 依旧填写smtp服务器地址,Default user e-mail suffix 填写默认邮箱后缀。 然后点击高级advanced

image-20220217082440142


勾选使用SMTP凭证,用户名为邮箱地址,密码为授权码

勾选使用SSL加密, SMTP port 的端口为465

填写会恢复邮件地址Reply to Address

image-20220217082918280


勾选Test configuration by sending test e-mail

填写一个邮箱地址,用于测试是否可以收到邮件。点击测试

如果收到邮件,只能确认E-mail Notification 部分配置正确,无法保证Extended E-mail Notification 部分或者其他部分是否正确

image-20220217083219134

收到的测试邮件

image-20220217083510955



准备邮件内容

idea 切换提交分支master

image-20220217100404247


在项目目录下创建html 文件,文件名email.html

在项目目录,右击,选择New -->选择HTML File 创建html 文件。文件名为email.html

image-20220217100616664

image-20220217100729457


<!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>项目名称&nbsp;:&nbsp;${PROJECT_NAME}</li>
                <li>构建编号&nbsp;:&nbsp;第${BUILD_NUMBER}次构建</li>
                <li>触发原因:&nbsp;${CAUSE}</li>
                <li>构建日志:&nbsp;<a

                        href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                <li>构建&nbsp;&nbsp;Url&nbsp;:&nbsp;<a href="${BUILD_URL}">${BUILD_URL}</a></li>
                <li> 工 作 目 录 &nbsp;:&nbsp;<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                <li>项目&nbsp;&nbsp;Url&nbsp;:&nbsp;<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="&nbsp;&nbsp;&nbsp;&nbsp;%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>

变量获取指南

image-20220217101243994

image-20220217101225535


邮件模板提交到gitlab的master分支上

image-20220217101438920

image-20220217101459719


修改Jenkinsfile文件,配置发送邮件的代码

添加的部分,和stages同级。注意,要把email地址修改为自己的邮件地址.

    post {
        always {
            emailext(
            subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}', to: '3025391653.com'
            )
        }
    }

Jenkinsfile全部代码

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '9a04c4aa-6116-4246-82c3-dbfc2c8bc2d5', url: 'git@192.168.23.201:zhangsan/web_demo.git']]])
            }
        }
         stage('build project') {
            steps {
                sh 'mvn clean package'
            }
        }
        stage('deploy item') {
            steps {
                deploy adapters: [tomcat8(credentialsId: '5cf315b3-86b3-43ca-b8d7-db867310f6e0', path: '', url: 'http://192.168.23.203:8080')], contextPath: '/', war: 'target/*.war'
            }
        }
    }
    post {
             always {
                 emailext(
                 subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',body: '${FILE,path="email.html"}',  to: '3025391653@qq.com'
                 )
             }
         }
}    

image-20220217102147108


测试

image-20220217102843993

image-20220217103314847

posted @ 2022-02-17 10:35  知己一语  阅读(845)  评论(0编辑  收藏  举报