使用Groovy脚本获取构建请求者(Jenkins / email-ext)

toiithl6  于 2023-06-21  发布在  Jenkins
关注(0)|答案(2)|浏览(152)

我想在构建后脚本中获取构建请求者的用户名和/或电子邮件地址。

  • Sidenote:我想要请求者,以便我可以在email-ext插件的pre-send脚本中动态设置构建后电子邮件通知的电子邮件发件人。*

AbstractBuild内置了对AbstractBuild.getCulprits()AbstractBuild.hasParticipant(User user)的支持,但我找不到检索请求者的方法。在reference list中也找不到任何对User类的有用引用。

qcuzuvrc

qcuzuvrc1#

我设法通过构建的Cause解决了这个问题,作为recommended in this answer
构建请求者可以在构建的原因中找到的原因是完全有意义的,当你考虑它时:并非每个构建都由用户直接触发。如果它是由用户触发的,则构建的原因列表包含一个Cause.UserIdCause,以及用户的ID和名称。
这段代码对我很有用。它通过cause从构建中提取用户名,并设置From和ReplyTo标头。我在email-ext Jenkins插件的pre-send脚本中使用它。

import javax.mail.internet.InternetAddress

cause = build.getCause(hudson.model.Cause.UserIdCause.class);
username = cause.getUserName()
id = cause.getUserId()
email = new InternetAddress(String.format("%s <%s@example.com>", username, id))

msg.setFrom(email)
msg.setReplyTo(email);
7kjnsjlb

7kjnsjlb2#

只需发送邮件给构建请求者(即你在你的侧边所陈述的是你实际的目标/问题),你可以在管道中使用recipientProviders中的requestor()的emailext步骤。
来自email-ext插件文档的示例:

emailext body: 'Test Message',
    recipientProviders: [developers(), requestor()],
    subject: 'Test Subject',
    to: 'test@example.com'

(This在问题发布后添加了一个功能:更新日志)

相关问题