简体   繁体   English

Jenkins管道电子邮件未在构建失败时发送

[英]Jenkins pipeline email not sent on build failure

I am using following step in my pipeline jenkins job: 我在管道jenkins工作中使用以下步骤:

step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my@xyz.com', sendToIndividuals: true])

But no email was sent when the build failed (ie error). 但是当构建失败时(即错误)没有发送电子邮件。 Any pointers why? 有什么指针为什么?

PS Emails can be sent from this server, I have tested that. PS电子邮件可以从这台服务器发送,我已经测试过了。

Use Declarative Pipelines using new syntax, for example: 使用新语法使用声明性管道,例如:

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: "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'
        }
    }
}

You can find more information in the Official Jenkins Site: 您可以在Jenkins官方网站上找到更多信息:

https://jenkins.io/doc/pipeline/tour/running-multiple-steps/ https://jenkins.io/doc/pipeline/tour/running-multiple-steps/

Note that this new syntax make your pipelines more readable, logic and maintainable. 请注意,这种新语法使您的管道更具可读性,逻辑性和可维护性。

You need to manually set the build result to failure, and also make sure it runs in a workspace. 您需要手动将构建结果设置为失败,并确保它在工作区中运行。 For example: 例如:

try {
    throw new Exception('fail!')
} catch (all) {
    currentBuild.result = "FAILURE"
} finally {
     node('master') {
        step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'my@xyz.com', sendToIndividuals: true])
    }   
}

The plugin is checking currentBuild.result for the status, and this isn't normally changed until after the script completes. 该插件正在检查currentBuild.result的状态,并且通常在脚本完成之后才会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM