发送有关詹金斯管道故障的电子邮件 [英] Send email on jenkins pipeline failure

查看:124
本文介绍了发送有关詹金斯管道故障的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何向jenkins管道中添加一个老式的后构建任务,该任务在构建失败时发送电子邮件?我在管道的GUI中找不到构建后操作".我知道我可以包装整个构建脚本try/catch,但是当构建脚本很大并且即使手动中止作业仍继续发送电子邮件时,这看起来很丑.我想实现与以前的基于email-ext的后生成操作相同的功能.

How can i add to a jenkins pipeline an old-style post-build task which sends email when the build fails? I cannot find "Post-build actions" in the GUI for a pipeline. I know that i can wrap the entire build script try/catch, however this seems ugly when the build script is large and continues to send emails even when the job was aborted manually. I would like to achive the same functionality as with the previouse email-ext based post-build action.

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'
    throw err
}

推荐答案

此答案适用于我的Jenkins版本. 2.96.

This answer worked on my Jenkins ver. 2.96.

在构建失败时未发送詹金斯管道电子邮件-堆栈溢出

 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>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: "foo@foomail.com";  
         }  
         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'  
         }  
     }  
 }

这篇关于发送有关詹金斯管道故障的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆