如何使用 Groovy 获取 Jenkins 当前构建项目的具体信息?
在 Jenkins/Hudson 中,在 Postbuild Groovy 脚本的帮助下,我想获得以下内容之一:
- 环境变量(例如当前的 JOB_NAME、BUILD_NUMBER 等)
- 当前项目的特定构建号的结果
- 构建号当前项目中最后一次不成功的构建目前
我只找到了以下方法,但它相当有限:
def item = hudson.model.Hudson.instance.getItem("GroovyMultipleFailTest")
def build = item.getLastBuild()
build.getNumber()
In Jenkins/Hudson, with the help of a Postbuild Groovy script, I would like to get one of the following:
- an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)
- the result of a specific build number of the current project
- the build number of the last not successful build in the current project
At the moment I only found the following way, but it's rather limited:
def item = hudson.model.Hudson.instance.getItem("GroovyMultipleFailTest")
def build = item.getLastBuild()
build.getNumber()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用 Jenkins v2.17 这对我有用:
echo "BUILD_NUMBER=${env.BUILD_NUMBER}"
Using Jenkins v2.17 this works for me:
echo "BUILD_NUMBER=${env.BUILD_NUMBER}"
博·佩尔森(Bo Persson)给出了最佳答案,但有点短。
要从 Groovy Postbuild 中的构建访问环境变量,您可以从构建中获取它们。此示例代码对于将 BUILD 的所有环境变量转储到控制台非常有用:
Bo Persson had the best answer, but was a little short.
To access the environment variables from the build in the Groovy Postbuild, you can grab them from the build. This sample code is useful for dumping all of the BUILD's environment variables to the console:
如果您在“Env Inject”中使用 Groovy 脚本,您可以通过以下方式获取当前版本和当前工作:
If you're using Groovy script within "Env Inject", you can get current build and current job by:
我让它为我工作的唯一方法是使用
build.properties.environment.BUILD_NUMBER
The only way I got it to work for me was with
build.properties.environment.BUILD_NUMBER
我尝试了本文和其他文章中的各种方法,看起来下面唯一的方法以及
build.properties.environment.BUILD_NUMBER
(已投票)在 Jenkins 执行系统 Groovy 中为我工作脚本构建步骤groovy命令类型I tried various approaches in this article and others, and it looks like the only one put below and also
build.properties.environment.BUILD_NUMBER
(upvoted) are working for me in Jenkins Execute system Groovy Script build step groovy command type