# Disable concurrent builds only for some branches in multibranch-projects

**URL:** <https://community.jenkins.io/t/disable-concurrent-builds-only-for-some-branches-in-multibranch-projects/1247>\
**Category:** Ask a question\
**Created:** [January 14, 2022, 12:14pm UTC](https://community.jenkins.io/t/disable-concurrent-builds-only-for-some-branches-in-multibranch-projects/1247 "2022-01-14T12:14:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ysmaoui](https://avatars.discourse-cdn.com/v4/letter/y/d9b06d/32.png) [@ysmaoui](https://community.jenkins.io/u/ysmaoui)\
**Post date:** [January 14, 2022, 12:14pm UTC](https://community.jenkins.io/t/disable-concurrent-builds-only-for-some-branches-in-multibranch-projects/1247/1 "2022-01-14T12:14:41Z")

</div>

Hi

is it possible in multibranch pipelines to disable concurrent builds for some branches, but allow concurrency for other branches?

it is possible to implement the “wait until previous builds are done” behavior manually at the beginning of a branch build, or use lock()

but this would take an executor and influence the buildDuration instead of waiting in the queue for the previous build

is there a way to avoid this?

---

<div class="post-metadata">

**Author:** ![ysmaoui](https://avatars.discourse-cdn.com/v4/letter/y/d9b06d/32.png) [@ysmaoui](https://community.jenkins.io/u/ysmaoui)\
**Post date:** [January 18, 2022, 4:51pm UTC](https://community.jenkins.io/t/disable-concurrent-builds-only-for-some-branches-in-multibranch-projects/1247/2 "2022-01-18T16:51:46Z")

</div>

I ended up using lock and a closure that decides when to lock (no concurrency) and when not to lock ( concurrency allowed ) based on the branch as input

```auto
public withLock(String branch, Closure closure){

    boolean noConcurrency = branch in ['master'] // branches for which no concurrent builds are allowed
                            
    if (noConcurrency) {
        lock( // only one build enters the lock
            resource: "${env.JOB_NAME}", // use the job name as lock resource to make the mutual exclusion only for builds from the same branch/tag
            inversePrecedence: true // latest build in the queue will get the lock/ start first
        ) {
            milestone() // aborts build older than this build that have been waiting for the lock
            closure()
        }
    }
    else {
        
        closure()
    }
}

// pipeline code

withLock (env.BRANCH_NAME){
    // lock is before any node call
    node(){
        // here the logic of the pipeline
    }
}

```

**NOTE** this will mess up the build duration statistics though - so the builds that are waiting can have up to the double of the normal build duration
