How to get specific information about the current

2019-03-26 05:29发布

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

6条回答
我只想做你的唯一
2楼-- · 2019-03-26 06:16

The only way I got it to work for me was with build.properties.environment.BUILD_NUMBER

查看更多
狗以群分
3楼-- · 2019-03-26 06:18

Using Jenkins v2.17 this works for me:

echo "BUILD_NUMBER=${env.BUILD_NUMBER}"

查看更多
淡お忘
4楼-- · 2019-03-26 06:21

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:

manager.build.getEnvironment(manager.listener).each {
    manager.listener.logger.println(it);
}
查看更多
倾城 Initia
5楼-- · 2019-03-26 06:30

an environment variable (e.g. current JOB_NAME, BUILD_NUMBER etc.)

String jobName = System.getenv('JOB_NAME')
查看更多
一夜七次
6楼-- · 2019-03-26 06:31
${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }
查看更多
混吃等死
7楼-- · 2019-03-26 06:34

If you're using Groovy script within "Env Inject", you can get current build and current job by:

currentJob.getName()
currentBuild.toString()
查看更多
登录 后发表回答