How to limit Jenkins concurrent multibranch pipeline builds?

JenkinsGroovyConcurrencyJenkins Pipeline

Jenkins Problem Overview


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?

Jenkins Solutions


Solution 1 - Jenkins

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()
    }
}

Solution 2 - Jenkins

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'

Solution 3 - Jenkins

As @VadminKotov indicated it is possible to disable concurrentbuilds using jenkins declarative pipelines as well:

pipeline {
    agent any
    options { disableConcurrentBuilds() }
    stages {
        stage('Build') {
            steps {
                echo 'Hello Jenkins Declarative Pipeline'
            }
        }
    }
}

disableConcurrentBuilds

> Disallow concurrent executions of the Pipeline. Can be useful for > preventing simultaneous accesses to shared resources, etc. For > example: options { disableConcurrentBuilds() }

Solution 4 - Jenkins

Thanks Jazzschmidt, I looking to lock all stages easily, this works for me (source)

pipeline {
  agent any
  options {
    lock('shared_resource_lock')
  }
  ...
  ...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjmreichaView Question on Stackoverflow
Solution 1 - JenkinsjmreichaView Answer on Stackoverflow
Solution 2 - JenkinsJazzschmidtView Answer on Stackoverflow
Solution 3 - Jenkins030View Answer on Stackoverflow
Solution 4 - JenkinsfranciscorodeView Answer on Stackoverflow