Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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()
like image 335
datka Avatar asked Nov 14 '11 11:11

datka


People also ask

How can I see my Jenkins details?

On your Jenkins server, browse to /env-vars. html . On my localhost test server, the path is this: http://localhost:8080/env-vars.html/. That will give you a list of the environment variables available to your job.

How do I run a Groovy script in Jenkins build?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name.

How do I view Jenkins scripts?

Visit "Manage Jenkins" > "Manage Nodes". Select any node to view the status page. In the menu on the left, a menu item is available to open a "Script Console" on that specific agent.


2 Answers

${manager.build.getEnvironment(manager.listener)['BUILD_NUMBER'] }
like image 140
Great88 Avatar answered Sep 23 '22 21:09

Great88


Using Jenkins v2.17 this works for me:

echo "BUILD_NUMBER=${env.BUILD_NUMBER}"

like image 32
Wilson Mar Avatar answered Sep 21 '22 21:09

Wilson Mar