在管道工作流程中使用 Jenkins 'Mailer' [英] Use Jenkins 'Mailer' inside pipeline workflow

查看:22
本文介绍了在管道工作流程中使用 Jenkins 'Mailer'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Jenkinsfile 中利用来自 Jenkins 的现有 Mailer 插件定义管道构建作业.鉴于以下简单的失败脚本,我希望每次构建都会收到一封电子邮件.

I'd like to leverage the existing Mailer plugin from Jenkins within a Jenkinsfile that defines a pipeline build job. Given the following simple failure script I would expect an email on every build.

#!groovy

stage 'Test'
node {
    try {
        sh 'exit 1'
    } finally {
        step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
    }
}

构建的输出是:

Started by user xxxxx
[Pipeline] stage (Test)
Entering stage Test
Proceeding
[Pipeline] node
Running on master in /var/lib/jenkins/jobs/rpk-test/workspace
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ exit 1
[Pipeline] step
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

如您所见,它确实记录了它在失败后立即执行管道step,但没有生成电子邮件.

As you can see, it does record that it performs the pipeline step immediately after the failure, but no emails get generated.

利用 mailer 的其他自由式作业中的电子邮件工作正常,它只是通过管道作业调用.

Emails in other free-style jobs that leverage the mailer work fine, its just invoking via pipeline jobs.

这是使用 Jenkins 2.2 和邮件程序 1.17 运行的.

This is running with Jenkins 2.2 and mailer 1.17.

是否有不同的机制可以让我调用失败的构建电子邮件?我不需要 mail 步骤的所有开销,只需要关于失败的通知和恢复.

Is there a different mechanism by which I should be invoking failed build emails? I don't need all the overhead of the mail step, just need notifications on failures and recoveries.

推荐答案

In Pipeline failed sh 不会立即将 currentBuild.result 设置为 FAILURE 而其初始值为 null.因此,像 Mailer 这样依赖于构建状态的构建步骤可能看起来不正确.

In Pipeline failed sh doesn't immediately set the currentBuild.result to FAILURE whereas its initial value is null. Hence build steps that rely on the build status like Mailer might work seemingly incorrect.

您可以通过添加调试打印来检查它:

You can check it by adding a debug print:

stage 'Test'
node {
    try {
        sh 'exit 1'
    } finally {
        println currentBuild.result  // this prints null
        step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
    }
}

整个管道都由 Jenkins 提供的异常处理程序包裹,这就是为什么 Jenkins 最后将构建标记为失败.

This whole pipeline is wrapped with exception handler provided by Jenkins that's why Jenkins marks the build as failed in the the end.

因此,如果您想使用 Mailer,您需要正确维护构建状态.例如:

So if you want to utilize Mailer you need to maintain the build status properly. For instance:

stage 'Test'
node {
    try {
        sh 'exit 1'
        currentBuild.result = 'SUCCESS'
    } catch (any) {
        currentBuild.result = 'FAILURE'
        throw any //rethrow exception to prevent the build from proceeding
    } finally {
        step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
    }
}

如果您不需要重新抛出异常,您可以使用 catchError.它是一个内置的 Pipeline,可捕获其范围内的任何异常,将其打印到控制台并设置构建状态.示例:

If you needn't to rethrow the exception you can use catchError. It is a Pipeline built-in which catches any exception within its scope, prints it into console and sets the build status. Example:

stage 'Test'
node {
    catchError {
        sh 'exit 1'
    } 
    step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true])
}

这篇关于在管道工作流程中使用 Jenkins 'Mailer'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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