3

I am trying to create Multibranch Pipeline Jobs using Job DSL, but I want to disable concurrent builds on each branch. I have tried the following code snippet but it didn't work, "Do not allow concurrent builds" is still unchecked on new branches.

multibranchPipelineJob("${FOLDER_NAME}/${JOB_NAME}") {
    branchSources {
        git {
            remote("https://gitlab.com/${REPO_PATH}")
            credentialsId('gitlab_credentials')
            includes('*')
        }
    }
    configure { 
        def factory = it / factory(class: 'com.cloudbees.workflow.multibranch.CustomBranchProjectFactory')
        factory << disableConcurrentBuilds()
    }
    orphanedItemStrategy {
        discardOldItems {
            numToKeep(1)
        }
    }
}

I also tried this in configure closure:

factory << properties {
    disableConcurrentBuilds()
}

But this one caused following exception to be thrown:

19:03:50 groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method groovy.util.Node#leftShift.
19:03:50 Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
19:03:50    [class groovy.util.Node]
19:03:50    [class java.lang.String]
farukdgn
  • 31
  • 2
  • I have this need as well. I notice that in my jenkins instance the jobDSL api docs indicate that `disableConcurrentBuilds()` property is NOT supported in multibranch pipeline jobs. I would also point you to this related discussion: https://gist.github.com/tknerr/c79a514db4bdbfb4956aaf0ee53836c8 – timblaktu Nov 05 '20 at 18:23

1 Answers1

0

I have this need as well. I notice that in my jenkins instance the jobDSL api docs indicate that disableConcurrentBuilds() property is NOT supported in multibranch pipeline jobs.

I just returned to a related discussion I was having with @tknerr in which he pointed out that there IS a rate limiting feature available to multibranch pipelines via jobDSL.

My team just ran into a problem with pollSCM triggering running amok due to this Jenkins bug, and so I'm implementing this in jobDSL to make our jobs more robust to this. Like you, I had wanted to just "disableConcurrentBuilds" like can be done in pipelines, but since rate limiting appears to be the only solution currently available to multibranch pipelines, I experimented with putting this in our jobDSLs:

  strategy {
    defaultBranchPropertyStrategy {
      props {
        rateLimitBranchProperty {
          count(2)
          durationName("hour")
        }
      }
    }
  }

This is of course a horrible workaround, since it places a nasty dependency in the jobDSL of needing to know how long builds take, but i'm willing to accept this alternative to having to push disableConcurrentBuilds option to the Jenkinsfile on hundreds of branches.

It is also barely even effective at achieving the goal, since we want to allow concurrent builds across branches, but want to prevent individual branch jobs from being built "too fast".

We should check if there is a feature request in Jenkins for this (your original request).

In my jenkins instance (v2.222.3, Pipeline:multibranch v2.22), the setting is described here for applying it to "all branches":

https://<my_jenkins_fqdn>/plugin/job-dsl/api-viewer/index.html#path/multibranchPipelineJob-branchSources-branchSource-strategy-allBranchesSame-props-rateLimit

and here for applying it to specific branches:

https://<my_jenkins_fqdn>/plugin/job-dsl/api-viewer/index.html#path/multibranchPipelineJob-branchSources-branchSource-strategy-namedBranchesDifferent-defaultProperties-rateLimit

EDIT: Also wanted to link to a related Jenkins issue here.

timblaktu
  • 375
  • 3
  • 11