2016-09-27 40 views
4

如何在jenkins管道中添加一箇舊式的構建後任務,在構建失敗時發送電子郵件?我無法在管道的GUI中找到「後期製作操作」。我知道我可以包裝整個構建腳本try/catch,但是當構建腳本很大時,這看起來很難看,即使手動中止作業,也會繼續發送電子郵件。我想實現與基於前構建email-ext的後構建操作相同的功能。在jenkins管道故障上發送電子郵件

try { 
    // Do sth 
} catch(e) { 
    emailext body: '$DEFAULT_CONTENT', 
     recipientProviders: [ 
      [$class: 'CulpritsRecipientProvider'], 
      [$class: 'DevelopersRecipientProvider'], 
      [$class: 'RequesterRecipientProvider'] 
     ], 
     replyTo: '$DEFAULT_REPLYTO', 
     subject: '$DEFAULT_SUBJECT', 
     to: '$DEFAULT_RECIPIENTS' 
} 

回答

4

此刻你必須用try來使用這個過程,catch來獲得一個構建後的動作。

但是在未來這個步驟是計劃的(實際上是在審查中)。你可以閱讀這篇博客的進一步信息(見的聲明管道部分):

https://jenkins.io/blog/2016/09/19/blueocean-beta-declarative-pipeline-pipeline-editor/

或該票詹金斯吉拉:

https://issues.jenkins-ci.org/browse/JENKINS-38153

這pull請求:

https://github.com/jenkinsci/pipeline-model-definition-plugin/pull/13/

T他是博客文章的一部分:

聲明管道引入了postBuild部分,可以很容易有條件地在你的管道末端沒有在try複雜...管道腳本的運行抓東西。

postBuild { 
always { 
sh 'echo "This will always run"' 
} 
success { 
    sh 'echo "This will run only if successful"' 
} 
failure { 
    sh 'echo "This will run only if failed"' 
} 
unstable { 
    sh 'echo "This will run only if the run was marked as unstable"' 
} 
changed { 
    sh 'echo "This will run only if the state of the Pipeline has changed"' 
    sh 'echo "For example, the Pipeline was previously failing but is now successful"' 
    sh 'echo "... or the other way around :)"' 
} 
} 
2

採取行動,只有當生成的狀態已經改變,你可以使用post > changed block

並且爲了檢查,狀態已經改變到什麼狀態,可以使用script block結合檢查currentBuild.currentResult屬性的值。

像這樣:

pipeline { 
    ... 

    post { 
     changed { 
      script { 
       if (currentBuild.currentResult == 'FAILURE') { // Other values: SUCCESS, UNSTABLE 
        // Send an email only if the build status has changed from green/unstable to red 
        emailext subject: '$DEFAULT_SUBJECT', 
         body: '$DEFAULT_CONTENT', 
         recipientProviders: [ 
          [$class: 'CulpritsRecipientProvider'], 
          [$class: 'DevelopersRecipientProvider'], 
          [$class: 'RequesterRecipientProvider'] 
         ], 
         replyTo: '$DEFAULT_REPLYTO', 
         to: '$DEFAULT_RECIPIENTS' 
       } 
      } 
     } 
    } 

} 
0

這個答案也和我一起詹金斯版本。 2.96。

Jenkins pipeline email not sent on build failure - Stack Overflow

pipeline { 
    agent any 
    stages { 
     stage('Test') { 
      steps { 
       sh 'echo "Fail!"; exit 1' 
      } 
     } 
    } 
    post { 
     always { 
      echo 'This will always run' 
     } 
     success { 
      echo 'This will run only if successful' 
     } 
     failure { 
      mail bcc: '', body: "<b>Example</b><br>\n\<br>Project: ${env.JOB_NAME} <br>Build Number: ${env.BUILD_NUMBER} <br> URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "ERROR CI: Project name -> ${env.JOB_NAME}", to: "[email protected]"; 
     } 
     unstable { 
      echo 'This will run only if the run was marked as unstable' 
     } 
     changed { 
      echo 'This will run only if the state of the Pipeline has changed' 
      echo 'For example, if the Pipeline was previously failing but is now successful' 
     } 
    } 
} 
相關問題