1. 程式人生 > >Jenkins pipeline 使用 郵件擴充套件傳送郵件

Jenkins pipeline 使用 郵件擴充套件傳送郵件

一. 在使用 jenkins  郵件拓展的過程中,郵件內容的變數找了好久,現把總結內容整理如下;

pipeline{
    agent{label 'master'}
    environment {
        
        gitpullerr = 'noerr'
        sendmail = 'no'
        git_url = '
[email protected]
:/git/reps/pipeline.git'         git_key = '50e27d86-37e0-4a5b-82db-4ea5ea9492bf'         git_branch = 'dev'       }     triggers { pollSCM('* * * * *') }     stages {             stage('Get Code') {             steps {                 checkout([$class: 'GitSCM', branches: [[name: ${git_branch}]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: ${git_key}, url: ${git_url}]]])             }         }               stage('kkk'){             steps {                 script {                     try {                                             echo "${sdsdas}"                                          }catch(exc) {                         gitpullerr = 'err' ;                         //throw exc                     }                                      }                 echo "$gitpullerr"             }           }         stage('checkpullvalue'){                          when {                 //environment name: 'gitpullerr', value: 'err'                 branch 'dev'             }             steps{                     echo "value: $gitpullerr,print "             }         }             }     post{                             success {             script {                 if (sendmail == 'yes') {            emailext body: '''<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><br />             <b><font color="#0B610B">構建資訊</font></b>             <hr size="2" width="100%" align="center" /></td>         </tr>         <tr>             <td>                 <ul>                      <li>構建名稱:${JOB_NAME}</li>                     <li>構建結果: <span style="color:green"> ${BUILD_STATUS}</span></li>                      <li>構建編號:${BUILD_NUMBER}  </li>                     <li>GIT 地址:${git_url}</li>                                         <li>GIT 分支:${git_branch}</li>                     <li>變更記錄: ${CHANGES,showPaths=true,showDependencies=true,format="<pre><ul><li>提交ID: %r</li><li>提交人:%a</li><li>提交時間:%d</li><li>提交資訊:%m</li><li>提交檔案:<br />%p</li></ul></pre>",pathFormat="         %p <br />"}                 </ul>             </td>         </tr>     </table> </body> </html> ''', subject: '${PROJECT_NAME}', to: '
[email protected]
,'                 }             }         }                   failure {                          script {                 if (gitpullerr == 'noerr') {                 emailext body: '''<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </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><br />             <b><font color="#0B610B">構建資訊</font></b>             <hr size="2" width="100%" align="center" /></td>         </tr>         <tr>             <td>                 <ul>                      <li>構建名稱:${JOB_NAME}</li>                     <li>構建結果: <span style="color:red"> ${BUILD_STATUS}</span></li>                       <li>構建編號:${BUILD_NUMBER}  </li>                     <li>GIT 地址:${git_url}</li>                                         <li>GIT 分支:${git_branch}</li>                     <li>變更記錄: ${CHANGES,showPaths=true,showDependencies=true,format="<pre><ul><li>提交ID: %r</li><li>提交人:%a</li><li>提交時間:%d</li><li>提交資訊:%m</li><li>提交檔案:%p</li></ul></pre>",pathFormat="%p <br />"}                 </ul>             </td>         </tr>         <tr>             <td><b><font color="#0B610B">構建日誌 :</font></b>             <hr size="2" width="100%" align="center" /></td>         </tr>         <tr>             <td><textarea cols="150" rows="30" readonly="readonly"                     style="font-family: Courier New">${BUILD_LOG}</textarea>             </td>         </tr>     </table> </body> </html> ''', subject: '${PROJECT_NAME}', to: '
[email protected]
'             }else {                 echo 'scm pull error ignore send mail'             }                      }         }     }      }