Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent concurrent builds of different branches in the same repository within a bitbucket organization job style?

I have a BitBucket organization job configured in my Jenkins, which is configured to scan the whole organization every 20 minutes and if it identifies a commit in any of the organization's repositories it triggers an automatic build.

Sometimes, more than one branch is being changed at a certain time and this causes Jenkins to trigger more than one build of the same project.

One of these projects should never allow concurrent builds as it uses resources which are being locked when a build runs, this causes other branches where commits are being pushed to trigger but they always fail because their main resource is locked by the first instance of the build.

I'm aware to the Throttle Builds plugin and it looks perfect for freestyle/pipeline jobs but in the case of organization scanning I cannot configure anything in the repositories under the organization, just the organization itself, the same goes for Hudson Locks and Latches plugin.

Anyone knows any solution?

like image 746
Itai Ganot Avatar asked Feb 05 '23 05:02

Itai Ganot


1 Answers

I had a similar problem, and wanted to make sure that each branch of my multibranch pipeline could only execute one build at a time. here's what I added to my pipeline script:

pipeline {
  agent any
  options {
    disableConcurrentBuilds()  //each branch has 1 job running at a time
  }
  ...
  ...
}

https://jenkins.io/doc/book/pipeline/syntax/#options

[Update 09/30/2017]

You may also want to check out the lock & milestone steps of Declarative Pipeline.

Lock

Rather than attempt to limit the number of concurrent builds of a job using the stage, we now rely on the "Lockable Resources" plugin and the lock step to control this. The lock step limits concurrency to a single build and it provides much greater flexibility in designating where the concurrency is limited.

stage('Build') {
  doSomething()
  lock('myResource') {
    echo "locked build"
  }
}

Milestone

The milestone step is the last piece of the puzzle to replace functionality originally intended for stage and adds even more control for handling concurrent builds of a job. The lock step limits the number of builds running concurrently in a section of your Pipeline while the milestone step ensures that older builds of a job will not overwrite a newer build.

stage('Build') {
  milestone()
  echo "Building"
}
stage('Test') {
  milestone()
  echo "Testing"
}
  • https://jenkins.io/blog/2016/10/16/stage-lock-milestone/
  • https://jenkins.io/doc/pipeline/steps/pipeline-milestone-step/
  • https://jenkins.io/doc/pipeline/steps/lockable-resources/
like image 110
kongkoro Avatar answered Feb 07 '23 10:02

kongkoro