datka
datka

Reputation: 880

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

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

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

Upvotes: 15

Views: 52894

Answers (7)

Sasha Bond
Sasha Bond

Reputation: 1166

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

EnvVars envVars = build.getEnvironment(listener)
def n = envVars.get('BUILD_NUMBER')

Upvotes: 0

Wilson Mar
Wilson Mar

Reputation: 25

Using Jenkins v2.17 this works for me:

echo "BUILD_NUMBER=${env.BUILD_NUMBER}"

Upvotes: 7

SamGamgee
SamGamgee

Reputation: 564

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

Upvotes: 1

Noam Manos
Noam Manos

Reputation: 16971

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

currentJob.getName()
currentBuild.toString()

Upvotes: 2

w25r
w25r

Reputation: 882

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

Upvotes: 3

Great88
Great88

Reputation: 428

${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }

Upvotes: 5

Dónal
Dónal

Reputation: 187499

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

String jobName = System.getenv('JOB_NAME')

Upvotes: 1

Related Questions