How to get specific information about the current build project in Jenkins using Groovy?

In Jenkins / Hudson, using the Postbuild Groovy script, I would like to get one of the following:

  • environment variable (e.g. current JOB_NAME, BUILD_NUMBER, etc.).
  • the result of a specific build number of the current project
  • build number of the last failed build in the current project

At the moment, I have found only the following method, but it is rather limited:

def item = hudson.model.Hudson.instance.getItem("GroovyMultipleFailTest") def build = item.getLastBuild() build.getNumber() 
+7
source share
6 answers
 ${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] } 
+4
source

Bo Persson had the best answer, but he was a little short.

To access environment variables from the assembly in Groovy Postbuild, you can grab them from the assembly. This sample code is useful for dumping all BUILD environment variables to the console:

 manager.build.getEnvironment(manager.listener).each { manager.listener.logger.println(it); } 
+3
source

If you use the Groovy script within Env Inject ", you can get the current build and current job:

 currentJob.getName() currentBuild.toString() 
+2
source

Using Jenkins v2.17, this works for me:

echo "BUILD_NUMBER = $ {env.BUILD_NUMBER}"

+1
source

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

 String jobName = System.getenv('JOB_NAME') 
0
source

The only way to make me work for me: build.properties.environment.BUILD_NUMBER

0
source

All Articles