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

标签 groovy jenkins email-ext

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

旁注:我想要请求者,所以我可以在 email-ext 插件的预发送脚本中动态设置构建后电子邮件通知的电子邮件发件人。

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

最佳答案

我设法通过构建的 Cause 解决了它,如 recommended in this answer .

在构建原因中可以找到构建请求者的原因在您考虑时非常有道理:并非每个构建都是由用户直接触发的。 如果它是由用户触发的,则构建的原因列表包含一个 Cause.UserIdCause,其中包含用户的 ID 和名称。

这段代码对我有用。它通过原因从构建中提取用户名并设置 From 和 ReplyTo header 。我在 email-ext 的预发送脚本中使用它Jenkins 插件。

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);

关于groovy - 使用 Groovy 脚本获取构建请求者 (Jenkins/email-ext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362820/

相关文章:

Jenkins 管道 emailext emailextrecipients : Can I also add specific, 个人电子邮件地址?

java - HQL Where IN 用于空列表崩溃

jenkins - BB10 从命令行启动应用程序

ssh - Maven Build 无法部署到 Javadoc 服务器 : missing SSH known_hosts

java - 如何使用jenkins中的管道插件将参数传递给并行进程

电子邮件扩展插件 - 无法让 BUILD_LOG_EXCEPT 工作

grails - 在应用程序启动期间对Grails合并的配置进行故障排除

groovy - 使用 gstring 访问列表属性元素

groovy - 向 Groovy DSL 添加可选子句

jenkins - 在 Jenkins 中访问要附加的文件时出错