在 Jenkins 中,构建如何知道谁请求了它们?
我需要将构建请求者的用户名传递给实际正在执行工作的脚本。查看特定构建的控制台输出,第一行始终是“由用户 foo 启动”,因此 Jenkins 清楚地跟踪谁触发了构建。因此应该可以将该信息传递给工作。问题是,如何?
I need to pass the username of the requester of a build down to the script that is actually doing the work. Looking at the console output for a particular build, the first line is always "Started by user foo," so Jenkins is clearly keeping track of who triggered the build. So it should be possible to pass that information down to the job. The question is, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我尝试使用 Jenkins Build User Vars 插件 和通知 HipChat 房间某个用户启动了构建,但 HipChat 插件无法使用
BUILD_USER
变量,可能是因为 HipChat 操作发生在 Build User Vars 插件之前注入变量。所以我安装了 pre-scm-buildstep 插件 并添加:
]
I tried to use Jenkins Build User Vars plugin and notify a HipChat room that a build was started by a certain user, but
BUILD_USER
variable was not available to HipChat plugin, possibly because HipChat action happened before Build User Vars plugin injects the variable.So I installed pre-scm-buildstep plugin and added:
]
在您的作业中添加“执行系统 Groovy 脚本”:
In your Job add "Execute system Groovy script":
我设法得到它(在 Jenkins 2.58 上):
当然你需要在 Jenkins 中设置权限才能调用这些方法。
它并不总是您正在寻找的第 0 个 Cause 对象,例如,如果您重播另一用户的构建(没有测试这一点),它可能是另一个对象。
I managed to get it (on Jenkins 2.58):
Of course you need to set permissions in Jenkins to be able to call these methods.
It's not always the 0th Cause object you are looking for, e.g. it may be another one if you replay another user's build (did not test this).
user30997
请查看 Jenkins Build User Vars 插件,它执行您需要的操作:
它用于设置以下用户构建变量:
user30997
Please check out Jenkins Build User Vars plugin, it does what you need:
It is used to set following user build variables:
用户名没有放在易于获取的环境变量中,但您可以使用 xml(或 json 或 python)api - 一旦开始构建,
http://[jenkins-server]/job/[job-name]/[build -number]/api/xml
填充了详细信息:The username isn't put in an easy-to-fetch environment variable, but you can get it using the xml (or json or python) api - as soon as you start a build,
http://[jenkins-server]/job/[job-name]/[build-number]/api/xml
is populated with details: