Jenkins 整合邮件服务

Posted by Vito on December 9, 2023
  • Jenkins 安装 Email Extension Template 插件

  • Jenkins 设置邮件参数

  • 编写邮件模板,在 pipeline 会引用该邮件模板文件发送邮件
    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
    
    <!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>以下为${PROJECT_NAME}项目构建信息</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>构建状态:${BUILD_STATUS}</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">历史变更记录:</font></b>
          <hr size="2" width="100%" align="center" />
        </td>
      </tr>
      <tr>
        <td>
          ${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>
    </table>
    </body>
    </html>
    
  • pipeline 邮件通知脚本
    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
    
    pipeline {
        agent any
      
        stages {
            stage('pull project') {
                steps {
                    git branch: 'dev', credentialsId: '4488bfb8-2d68-4419-a79e-ccbb9928c3fe', url: 'git@git.zhch.lan:test/demo.git'
                }
            }
            stage('build project') {
                steps {
                    sh 'mvn clean package -Dmaven.test.skip=true'
                }
            }
            stage('publish project') {
                steps {
                    deploy adapters: [tomcat9(credentialsId: '5beb06bc-2ae4-4232-bfe7-3c3aea7aabcb', path: '', url: 'http://web-demo.zhch.lan/')], contextPath: '/demo', war: 'target/*.war'
                }
            }
        }
      
        post {
          always {
            emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: 'hczhch@ymail.com'
          }
        }
    }
    
  • PS