仅当手动触发构建时才如何向请求者发送电子邮件? [英] How to send an email to requester only if the build is triggered manually?

查看:185
本文介绍了仅当手动触发构建时才如何向请求者发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Jenkins中配置一个项目,以将电子邮件发送到常规预定构建的收件人组,但仅在手动触发构建的情况下发送给请求者.这可能吗?

I would like to configure a project in Jenkins to send emails to the recipients group for regular scheduled builds, but only to the requester in case the build is triggered manually. Is this possible?

推荐答案

您应该能够通过使用可编辑电子邮件通知"构建后操作的构建后脚本"触发器来完成此任务.您可以运行groovy脚本,最后一行评估为布尔值,该布尔值确定是否发送电子邮件.下面的屏幕快照显示了触发部分,该部分检查以查看构建是否由用户(手动)启动.

You should be able to accomplish this by using the "Script - After Build" trigger of the Editable Email Notification post-build action. You can run a groovy script, with the last line evaluating to a Boolean, which determines whether or not to send the email. The screenshot below shows the trigger section that checks to see if the build was initiated by a user (manually).

尽管如此,该脚本只会告诉您构建的直接原因是否是用户的手动操作.根据构建管道的设置方式,您的上游作业之一可能是手动启动的,因此我不确定在这种情况下是否要发送电子邮件.如果这样做,则必须遍历所有构建原因并寻找手动原因.

This script will only tell you whether or not the IMMEDIATE cause of the build was a manual user action, though. Depending on how your build pipeline is setup, one of your upstream jobs may have been initiated manually, so I'm not sure if you want an email sent in that case. If you do, you will have to iterate through all the build causes and look for a manual cause.

def boolean wasStartedManually(causes) {
    boolean manuallyStarted = false
    causes.each { cause ->
        if (cause.class == hudson.model.Cause$UserIdCause) {
            manuallyStarted = true
        }

        if (!manuallyStarted) {
            if (cause.class == hudson.model.Cause$UpstreamCause) {
                manuallyStarted = wasStartedManually(cause.upstreamCauses)
            }      
        }


    }
    return manuallyStarted
}

wasStartedManually(build.getCauses())

您将需要向您的作业中添加2个构建后的电子邮件操作,一个用于手动触发该作业,如果一个作业不是手动提交,则另一个.对于后者,您将运行相同的脚本,但只求结果取反.

You will need to add 2 post-build Email actions to your job, one for when the job is triggered manually, and then another if the job was not submitted manually. For the latter, you would run the same script, but just negate the results.

这篇关于仅当手动触发构建时才如何向请求者发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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