73

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?

jmreicha
  • 3,155
  • 7
  • 32
  • 39

5 Answers5

110

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()
    }
}
Yuri
  • 4,254
  • 1
  • 29
  • 46
jmreicha
  • 3,155
  • 7
  • 32
  • 39
  • 5
    You can do the same for declarative syntax. Here are [the docs](https://jenkins.io/doc/book/pipeline/syntax/#options) – Vadim Kotov Jun 29 '17 at 14:48
  • 57
    This currently does not work for branches, i.e. concurrent builds will still run if there were commits to different branches – anydoby Jun 26 '18 at 13:46
  • Any solution for multi branches? – guyromb Mar 08 '19 at 09:50
  • Correct, this does NOT work with a multi-branch pipeline. Bummer – Chris F Mar 28 '19 at 17:18
  • I don't believe there is no way to disable concurrent build in multibranch pipeline. – xbmono Oct 01 '19 at 07:12
  • 1
    See also [JENKINS-35359 Limit concurrent builds for 1 MultiBranch project](https://issues.jenkins-ci.org/browse/JENKINS-35359) – Arend v. Reinersdorff Oct 14 '19 at 09:42
  • 6
    disableConcurrentBuilds() will disable concurrent builds on a branch by branch (PR by PR basis). Still better than nothing - at least it will curb the resource hogging that happens when one committer pushes say 10 times to the same branch or PR. – Steven the Easily Amused Jan 28 '20 at 22:44
  • 2
    use lockable resources: https://plugins.jenkins.io/lockable-resources/. This plugin allows defining lockable resources (such as printers, phones, computers, etc.) that can be used by builds. If a build requires a resource which is already locked, it will wait for the resource to be free. – Jroger Feb 27 '20 at 19:37
  • The [throttle concurrent builds](https://plugins.jenkins.io/throttle-concurrents/) may be a better option for some which includes a variety of mechanisms to support blocking or latching concurrent builds, depending on your needs. – dan Dec 08 '21 at 20:44
11

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'
Jazzschmidt
  • 989
  • 12
  • 27
8

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

030
  • 10,842
  • 12
  • 78
  • 123
3

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

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

I got the solution for multibranch locking too, with de lockable-resources plugin and the shared-libs here is :

Jenkinsfile :

@Library('my_pipeline_lib@master') _
myLockablePipeline()

myLockablePipeline.groovy :

    call(Map config){
        def jobIdentifier = env.JOB_NAME.tokenize('/') as String[];
    
        def projectName = jobIdentifier[0];
        def repoName = jobIdentifier[1];
        def branchName = jobIdentifier[2];
    
        //now you can use either part of the jobIdentifier to lock or 
    limit the concurrents build
        //here I choose to lock any concurrent build for PR but you can choose all
    
        if(branchName.startsWith("PR-")){
           lock(projectName+"/"+repoName){
              yourTruePipelineFromYourSharedLib(config);
           }
        }else{
           // Others branches can freely concurrently build
           yourTruePipelineFromYourSharedLib(config);
        }
}

To lock for all branches just do in myLockablePipeline.groovy :

call(Map config){

def jobIdentifier = env.JOB_NAME.tokenize('/') as String[];

def projectName = jobIdentifier[0];
def repoName = jobIdentifier[1];
def branchName = jobIdentifier[2];

 lock(projectName+"/"+repoName){
    yourTruePipelineFromYourSharedLib(config);
 }
}
Aris
  • 46
  • 3