Disable Jenkins concurrent builds based on parameters
06 May 2022 | tags: JenkinsRationale TL;DR
We want to limit Jenkins concurrent builds, but only depending on the parameters of the job. We don’t want to block multiple instances of the same if they use different parameter values.
The solution
We all know the old good disableConcurrentBuilds()
pipeline method. It’s been here with us for ages. However, we’d like to run multiple instances of the same job when the parameters provided to them are different. In my case, I’d like to trigger a backup job over multiple machines and limit the execution only if trying to backup the same machine multiple times (i.e.: because the scheduling is not right).
There’s a nice plugin called Throttle Concurrent Builds
that might come in handy. It basically allows us to block jobs at the job/ category level. It can also supports throttling on the parameters provided, allowing us to even set the maximum number of jobs to run even using different parameters.
In my case, I used a setup like this:
options {
...
throttleJobProperty(
categories: [],
limitOneJobWithMatchingParams: true,
maxConcurrentPerNode: 0,
maxConcurrentTotal: 4,
paramsToUseForLimit: 'SYSTEM',
throttleEnabled: true,
throttleOption: 'project',
)
}
In this case, the relevant arguments in my case (remember, limit by parameter values) are:
- throttleOption: project This ought limit at the pipeline level. The other possible value is host but I’m not quite sure yet
- throttleEnabled: true This one seems self explanatory
- paramsToUseForLimit: ‘SYSTEM’ Possible parameters to use. I believe this string is a comma-separated list of parameters to use. In my case, just one parameter
- limitOneJobWithMatchingParams Apply the policy to limit one job per matching params
I got more info by checking [2]. The plugin page at [1] gives some information but I believe it could be improved.
Have fun!
[1] https://plugins.jenkins.io/throttle-concurrents/ [2] https://github.com/jenkinsci/throttle-concurrent-builds-plugin/pull/68