How can I configure Jenkins multibranch pipelines to use option with different args for branches & pull requests in a single Jenkinsfile?

i am working with only declarative jenkins multibranch pipelines and i would like to use

  • options { disableConcurrentBuilds(abortPrevious: false) } when building branches
  • and options { disableConcurrentBuilds(abortPrevious: true) } when building pull requests.

options

disableConcurrentBuilds

Disallow concurrent executions of the Pipeline. Can be useful for preventing simultaneous accesses to shared resources, etc. For example: options { disableConcurrentBuilds() } to queue a build when there’s already an executing build of the Pipeline, or options { disableConcurrentBuilds(abortPrevious: true) } to abort the running one and start the new build.

Is it possible to configure Jenkins multibranch pipelines to use different arguments for disableConcurrentBuilds() options based on branch type in a single Jenkinsfile?

Perhaps it could be done like this:

options {
    disableConcurrentBuilds(abortPrevious: env.CHANGE_ID != null)
}

A similar construct works with the buildDiscarder option for sure.

(However, if you didn’t want to have any disableConcurrentBuilds for branches at all, then this kind of thing would not work; see comments in JENKINS-41392.)