How to limit Jenkins concurrent multibranch pipeline builds?

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

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


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

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

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'