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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 04:06:07

问题


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

回答1:


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



回答2:


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



回答3:


Using Jenkins v2.17 this works for me:

echo "BUILD_NUMBER=${env.BUILD_NUMBER}"




回答4:


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

currentJob.getName()
currentBuild.toString()



回答5:


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

String jobName = System.getenv('JOB_NAME')



回答6:


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



来源:https://stackoverflow.com/questions/8121237/how-to-get-specific-information-about-the-current-build-project-in-jenkins-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!