Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit Jenkins concurrent multibranch pipeline builds?

I am looking at limiting the number of concurrent builds to a specific number in Jenkins, leveraging the multibranch pipeline workflow but haven't found any good way to do this in the docs or google.

Some docs say this can be accomplished using concurrency in the stage step of a Jenkinsfile but I've also read elsewhere that that is a deprecated way of doing it.

It looks like there was something released fairly recently for limiting concurrency via Job Properties but I couldn't find documentation for it and I'm having trouble following the code. The only thing I found a PR that shows the following:

properties([concurrentBuilds(false)])

But I am having trouble getting it working.

Does anybody know or have a good example of how to limit the number of concurrent builds for a given, multibranch project? Maybe a Jenkinsfile snippet that shows how to limit or cap the number of multibranch concurrent builds?

like image 925
jmreicha Avatar asked Jan 05 '17 19:01

jmreicha


People also ask

How do I disable some branches in Multibranch Jenkins builds?

Enter the branch in "Exclude branch" and save the settings. If you don't have control of the project settings, the easy way is to rename the Jenkinsfile in the project/branch. This configuration will define to trigger a build if a branch/project has "Jenkinsfile" in it. Hence, renaming it will not trigger a build.

What is concurrent builds in Jenkins?

Jenkins allows for parallel execution of builds for a Job. Job configuration page has a check box, "Execute concurrent builds if necessary". Also, in the master node configuration set the "# of executors" field to more than 1. Once these two are done, parallel job execution is enabled.

What is the difference between pipeline and Multibranch pipeline in Jenkins?

Jenkins Pipeline Vs. Multibranch Pipeline. A multibranch pipeline is meant for building multiple branches from a repository and deploy to multiple environments if required. A pipeline job supports both pipeline steps to be added in Jenkins configuration and form SCM.

What is Throttle concurrent builds in Jenkins?

Introduction. This plugin allows for throttling the number of concurrent builds of a project running per node or globally.


2 Answers

Found what I was looking for. You can limit the concurrent builds using the following block in your Jenkinsfile.

node {   // This limits build concurrency to 1 per branch   properties([disableConcurrentBuilds()])    //do stuff   ... } 

The same can be achieved with a declarative syntax:

pipeline {     options {         disableConcurrentBuilds()     } } 
like image 72
jmreicha Avatar answered Sep 18 '22 08:09

jmreicha


Limiting concurrent builds or stages are possible with the Lockable Resources Plugin (GitHub). I always use this mechanism to ensure that no publishing/release step is executed at the same time, while normal stages can be build concurrently.

echo 'Starting' lock('my-resource-name') {   echo 'Do something here that requires unique access to the resource'   // any other build will wait until the one locking the resource leaves this block } echo 'Finish' 
like image 35
Jazzschmidt Avatar answered Sep 18 '22 08:09

Jazzschmidt