Jenkins管道电子邮件未发送构建故障



我在管道中使用以下步骤jenkins job:

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

但是,当构建失败(即错误(时,没有发送电子邮件。有任何指针为什么?

P.S。电子邮件可以从该服务器发送,我已经对此进行了测试。

使用新语法使用声明管道,例如:

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

您可以在Jenkins官方网站中找到更多信息:

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

请注意,这种新的语法使您的管道更可读,逻辑和可维护。

您需要手动将构建结果设置为故障,并确保其在工作空间中运行。例如:

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

插件正在检查 currentBuild.result是否状态,直到脚本完成后,这通常才会更改。

最新更新