Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convince jenkins to share a build number for several jobs?

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:

  • it will set the build number for a single job and you cannot know all how to setup it for all branches because they can be removed or added at any time
  • it doesn't set it for current build

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.

like image 712
sorin Avatar asked Jul 02 '13 15:07

sorin


People also ask

Can we build multiple jobs at a time using Jenkins?

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

How do I allow concurrent builds in Jenkins?

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.

How many jobs can Jenkins handle?

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.

How many builds are included in the build trend for each Jenkins job?

Jenkins keeps the last 30 builds of all our jobs.


1 Answers

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
like image 107
dnozay Avatar answered Sep 30 '22 18:09

dnozay