Jenkins流水線執行完成後發送郵件通知

  • 2020 年 3 月 15 日
  • 筆記

安裝插件

Jenkins需要配置郵件通知,安裝插件Email Extension,然後進入系統管理-> 系統設置 ->Extended E-email Notification。這裡我使用的是QQ郵箱,填寫SMTP服務器地址smtp.qq.com 和端口 465注意要開啟SSL,密碼為授權碼。

配置插件

郵件通知的功能很重要,我們要為每條流水線都加上這個步驟,我們在共享庫中封裝一個toemail.groovy。新建文件src/org/devops/toemail.groovy。在這個文件中,我們寫了一段HTML代碼,主要展示Jenkins的構建信息。

package org.devops    //定義郵件內容  def Email(status,emailUser){      emailext body: """              <!DOCTYPE html>              <html>              <head>              <meta charset="UTF-8">              </head>              <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">                  <img src="http://192.168.1.200:8080/static/0eef74bf/images/headshot.png">                  <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">                      <tr>                          <td><br />                              <b><font color="#0B610B">構建信息</font></b>                          </td>                      </tr>                      <tr>                          <td>                              <ul>                                  <li>項目名稱:${JOB_NAME}</li>                                  <li>構建編號:${BUILD_ID}</li>                                  <li>構建狀態: ${status} </li>                                  <li>項目地址:<a href="${BUILD_URL}">${BUILD_URL}</a></li>                                  <li>構建日誌:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>                              </ul>                          </td>                      </tr>                      <tr>                  </table>              </body>              </html>  """,              subject: "Jenkins-${JOB_NAME}項目構建信息 ",              to: emailUser  }

測試郵件

#!groovy    @Library('jenkinslibrary@master') _    //func from shareibrary  def gitlab = new org.devops.gitlab()  def toemail = new org.devops.toemail()    //branch  branchName = branch - "refs/heads/"  currentBuild.description = "Trigger by ${userName} ${branch}"  gitlab.ChangeCommitStatus(projectId,commitSha,"running")      //pipeline  pipeline{      agent { node { label "build"}}      stages{            stage("CheckOut"){              steps{                  script{                      println("${branchName}")                  }              }          }      }        post {          always{              script{                  println("always")              }          }            success{              script{                  println("success")                  gitlab.ChangeCommitStatus(projectId,commitSha,"success")                  toemail.Email("流水線成功",userEmail)              }            }          failure{              script{                  println("failure")                  gitlab.ChangeCommitStatus(projectId,commitSha,"failed")                  toemail.Email("流水線失敗了!",userEmail)              }          }            aborted{              script{                  println("aborted")                  gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")                  toemail.Email("流水線被取消了!",userEmail)              }            }        }    }

接下來可以調試了,如果沒有問題會在郵箱中看到此封郵件。