I do have a requirement from the development team to setup the build system so each build will have an unique build number for all branches.
The builds are made by jenkins using jobs for each branch.
There is a jenkins plugin that can setup the next buildnumber for a job but this is kinda useless for at least two reasons:
How to we get the build numbers: we do make a HTTP call with the branch name and the revision number in git/mercurial. Based on this the centralized sever is giving us a build number as a response. If you call it twice with the same parameters, you will get the same response (desired behaviour).
Now, how can we tweak jenkins to use the same build numbers as us? Obviously I could use the build number returned from the script, but the job number would be different and I doubt jenkins will know that I touched the BUILD_NUMBER variable inside my script.
Mainly, what I need is some kind of pre-job-start script that I can run, one that would run before the build number is assigned to the job.
Yes it's possible. Doc: If this option is checked, Jenkins will schedule and execute multiple builds concurrently (provided that you have sufficient executors and incoming build requests.)
Concurrent builds are enabled by default. That is why the is the disableConcurrentBuilds() function. So there is no need to add any additional code to your pipeline to enable concurrent builds.
These equations assume that the Jenkins controller will have 5 cores with one core per 100 jobs (500 total jobs/controller) and that teams will be divided into groups of 40.
Jenkins keeps the last 30 builds of all our jobs.
You can use the Environment Injector Plugin to evaluate a Groovy script before your run. I have almost the same requirement, however for me, only the jobs with the same job_prefix_
in their name share the same unique nextBuildNumber
(in other words, other jobs with job_prefix2_
in their name share a different nextBuildNumber
).
In the Evaluated Groovy Script
section, please use:
import jenkins.model.*
// Access to the Jenkins instance
jenkins_instance = jenkins.model.Jenkins.instance
// Select jobs that match.
job_name = "^job_prefix_.*"
allItems = jenkins_instance.items
chosenJobs = allItems.findAll{ job -> job.name.matches(job_name) }
// Get the max
build_number = chosenJobs.collect{ it -> it.nextBuildNumber }.max()
// Increase next build number
currentJob.nextBuildNumber = build_number + 1
// and use it.
def map = [BUILD_NUMBER: build_number]
return map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With