2016-09-27 44 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' 
     } 
    } 
} 
相关问题