Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins groovy pipeline - retrieve build number of built job

I have a pipeline my team is using to spin up cloud VM's and deploy a software stack to them. Part of this process is to bundle up the artifacts from the builds they select. Right now im just grabbing last success of the jobs listed but ive encountered issues of that job being built again in another process before the pipeline can create its bundle, making the bundle grab an artifact built with the wrong dependencies.

def DeployModule(jobName, jobBranch, serverHostName, database){
build job: jobName, parameters: [[$class: 'StringParameterValue', name: 'Branch', value: jobBranch], [$class: 'StringParameterValue', name: 'DatabaseAction', value: database], [$class: 'StringParameterValue', name: 'Profile', value: serverHostName]]
println "$jobName Succesfull"
}

Is there any way to alter my simple build job method to pull out the actual build number that was triggered? The pipeline console prints what build number is created im just not sure how to get it in my groovy code.

[Pipeline] build (Building tms-auto-build)
Scheduling project: tms-auto-build
Starting building: tms-auto-build #298
like image 343
Stephen Nichols Avatar asked Aug 26 '16 15:08

Stephen Nichols


People also ask

How do I find the build number in Jenkins pipeline?

BUILD_NUMBER is the current build number. You can use it in the command you execute for the job, or just use it in the script your job executes. See the Jenkins documentation for the full list of available environment variables.

How do I get a list of jobs in Jenkins?

Go to Script Console under Manage Jenkins, this script will print the name of all jobs including jobs inside of a folder and the folders themselves: Jenkins. instance. getAllItems(AbstractItem.


1 Answers

This was actually kind of a no brainer. if I just set build job: jobName to a variable, that variable is a RunWrapper

https://github.com/jenkinsci/pipeline-plugin/blob/d3f66c6f04d1d979957f02819b19291e2c35e276/support/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.java

RunWrapper as a .getNumber() that works perfect

def testing = build job: "tms-auto-build"
println testing.getNumber()
like image 186
Stephen Nichols Avatar answered Sep 20 '22 15:09

Stephen Nichols