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

查看:227
本文介绍了在管道工作流程中使用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和mailer 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.

推荐答案

在管道中失败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天全站免登陆