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

7

15

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()
Moorefield answered 14/11, 2011 at 11:59 Comment(0)
N
7

Using Jenkins v2.17 this works for me:

echo "BUILD_NUMBER=${env.BUILD_NUMBER}"

Nord answered 17/8, 2016 at 4:1 Comment(0)
G
5
${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }
Guzman answered 11/12, 2012 at 19:1 Comment(0)
T
3

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);
}
Treasatreason answered 29/1, 2013 at 23:57 Comment(1)
I'll probably remember next time, but how do I get "manager" again?Monda
B
2

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

currentJob.getName()
currentBuild.toString()
Byers answered 27/5, 2015 at 19:16 Comment(1)
How to get just the BUILD_NUMBER instead of the BUILD_NAME + #BUILD_NUMBER?Homegrown
F
1

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

String jobName = System.getenv('JOB_NAME')
Flita answered 14/11, 2011 at 14:6 Comment(3)
I tried that, but unfortunately it did not work. However, I succeeded getting the project/job name by manager.build.project.getName().Moorefield
It would appear this the one way works when running a 'system' groovy script - e.g. in the same VM as the Jenkins/Hudson process, while the System.getenv() method works when running an external script.Teahouse
This is accessing the machine environment variables, instead of the job ones.Confraternity
N
1

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

Naima answered 25/4, 2016 at 8:42 Comment(2)
While this syntax does work, it also comes with a downside: each call will leave 2 opened file handles on the build log (/var/lib/jenkins/jobs/jobname/builds/XX/log) on the master node. (link to opened Jira issue concerning this bug issues.jenkins-ci.org/browse/JENKINS-54012)Blancmange
Oh that's crappy xDNaima
A
0

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')
Aerialist answered 16/11, 2021 at 17:50 Comment(2)
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.Philibeg
I do not get what you mean by "the only one put below". I also do not see any explanation of the code in the post. Writing "I tested and it works, honest." is not an explanation (in case your prose, as it is meant, can be summarised like that).Philibeg

© 2022 - 2024 — McMap. All rights reserved.