34

I have two Jenkins projects that share a database. They must not be run simultaneously. Strictly speaking, there is no particular dependency between them beyond non concurrency, but at the moment I partially manage this constraint by running one "downstream" of the other. This works most of the time, but not always. If a source control change happens while the second is running, the first will start up again, and they'll be running concurrently and probably both fail miserably.

This is similar, but not identical, to How to prevent certain Jenkins jobs from running simultaneously? The difference is that I don't have a "number of threads" problem -- I'm already only running at most one thread of any given project at any one time, even in the case where two (different-project) builds stomp each other. This seems to rule out all the several suggestions in that thread.

Community
  • 1
  • 1
jackr
  • 1,407
  • 1
  • 14
  • 29
  • The Locks and Latches plugin mentioned in the answer to the question you quoted should work here as well. – gareth_bowles Apr 12 '12 at 00:20
  • 2
    Locks & Latches works for my case, but it's on [the "potential deprecation" list](https://wiki.jenkins-ci.org/display/JENKINS/Proposed+Plugin+Deprecation). If it does get deprecated, looks like the newer [Exclusion-Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Exclusion-Plugin) will do the job for me (and [Throttle Concurrent builds](https://wiki.jenkins-ci.org/display/JENKINS/Throttle+Concurrent+Builds+Plugin) for the other, counted, case). – jackr Apr 19 '12 at 18:55

4 Answers4

26

The Locks and Latches plugin should resolve your problem. Create a lock and have both jobs use the same lock. That will prevent the jobs from running concurrently.

  1. Install the plugin in "Manage Jenkins: Manage Plugins."
  2. Define (provide a name for) your lock(s) in "Manage Jenkins: Configure System."
  3. For each job you want to participate in the exclusion,
    1. in ": Configure: Build Environment," check "Locks",
    2. and pick your lock name from the drop list.
jackr
  • 1,407
  • 1
  • 14
  • 29
Jason Swager
  • 6,421
  • 6
  • 41
  • 56
  • Here's the complete solution, since it took me a bit of research and flailing: – jackr Apr 19 '12 at 18:45
  • 3
    Install the plugin in "Manage Jenkins: Manage Plugins." Define (provide a name for) your lock(s) in "Manage Jenkins: Configure System." For each job you want to participate in the exclusion, in ": Configure: Build Environment," check "Locks", and pick your lock name from the drop list. – jackr Apr 19 '12 at 18:50
  • 1
    @jackr You should edit your comment into this answer to make it complete! – chown Sep 12 '15 at 01:20
  • The mentioned plugin has now been marked for deprecation. One possible replacement suggested is the [Throttle Concurrent Builds](https://plugins.jenkins.io/throttle-concurrents/) plugin. – Java Devil Mar 04 '21 at 20:41
13

The Lockable Resources Plugin. Simple and working well for me May 2016.

Install the plugin. In Manage Jenkins > Configure System go to Lockable Resources Manager.
Select Add Lockable Resource. Enter values for field: Name and hit Save. Warning: Do not enter spaces in Name field.

In Jenkins > job_name > Configure > General, Select checkbox: This build requires lockable resources. Enter name or names in value for field: Resources.

Start a build. Under build #number select Locked Resources. You should see something like:This build has locked the following resources: resource_name - resource_description.

Start a different build which uses the same resource. You will see Build Queue in Jenkins status/menu showing job name. Hover text shows Started by, Waiting for resources resources_list, Waiting for time.

(also resource tags/labels can be used)

Adding screenshot of Job Configuration page as there seems to be a problem for some users where "This build requires lockable resources" is not visible: ** when the checkbox is not selected you should only see "[_] This build requires lockable resources" enter image description here

gaoithe
  • 4,218
  • 3
  • 30
  • 38
  • 1
    Good plugin but i don't see In Jenkins > job_name > Configure > General, Select checkbox: This build requires lockable resources. Enter name or names in value for field: Resources. for pipeline job. Is there any work around? – Jitesh Sojitra Dec 04 '16 at 06:13
  • Can you describe what your problem is in more detail ? Do you see "This build requires lockable resources"? Do you have the plugin installed? – gaoithe Dec 05 '16 at 09:41
  • @JiteshSojitra I have same situation as you. Did you figure out why? – Jack Lu Mar 22 '19 at 03:49
  • I'm sorry but we have moved to CircleCI after started using cloud tools :) – Jitesh Sojitra Mar 22 '19 at 06:14
  • @JiteshSojitra Thank you all the same. It's annoying that everything I read said there's such an option but I just cannot see it. :) – Jack Lu Mar 22 '19 at 06:25
  • @gaoithe Just can't see it. I can already configure a resource from Manage Jenkins > Configure System > Lockable Resource Manager, but there's no `This build requires lockable resources` option in job configuration page. – Jack Lu Mar 22 '19 at 06:30
  • @JackLu There is a funny thing with some (recent?) version of kudos the Lockable Resources management page does not appear in the menu. But you can type in the URL /lockable-resources/ and it works. e.g. ```http://myjenkins:port/lockable-resources/``` But actually that's not your problem I think. Sorry. In the job configuration page, if it is not showing, . . hummm, . . is there a problem loading the plugin I wonder, san you see anything in jenkins system log at jenkins init time or when you click in job config ? – gaoithe Mar 22 '19 at 12:16
  • @JackLu what type of job ? I have "Freestyle project" jobs. – gaoithe Mar 22 '19 at 12:21
  • @gaoithe I noticed the "project type" trick soon after I asked. I was using a multi-branch pipeline project. Now I know about the difference and why I was not seeing the option. Thank you very much. You would have helped me if I didn't realize it myself. – Jack Lu Mar 25 '19 at 07:18
  • For scripted pipelines, use can use the syntax: `lock('buildlock1') { stage('build stage" { doMyBuild() } }` – seren Sep 24 '20 at 18:41
5

EDIT: Below information is effective as of 04/10/2014

Exclusion plugin, https://wiki.jenkins-ci.org/display/JENKINS/Exclusion-Plugin Very useful if few build use the same resource - e.g. a test database. All you need to do is to update configuration of all jobs using this resource and as a result they will never run in parallel but wait for others to complete.

Taken from : http://www.kaczanowscy.pl/tomek/2012-07/jenkins-plugins-part-iii-towards-continuous-delivery

This plugin does block two or more jobs from running in parallel.
To test, do this for job1

  1. Configure
  2. Under Build Environment check "Add resource to manage exclusion."
  3. Then Add -> New Resource -> Name -> lock
  4. Under Build -> Add build step
  5. Critical Block Start
  6. Add build step -> Add whatever you want to add.(add sleep 15 to make sure it lasts longer to check concurrency.)
  7. Add build step -> Critical block end
  8. Repeat the above steps for job2, make sure you use the same lock name 'lock'.
  9. manually build both jobs concurrently.
  10. Monitor the run progress under jenkins -> Exclusion administration.
Vikdor
  • 23,934
  • 10
  • 61
  • 84
2.8a8a_G
  • 617
  • 1
  • 7
  • 16
2

1 December 2021
Use Build Blocker plugin, Install from Manage Jenkins > Plugin Manager Build Blocker plugin install using Jenkins UI

For example, you have two pipelines React-build and React-tests:
Go to React-build -> Configure -> Block build
if I don't need React-tests to run concurrently with the current React-build job, add it in the blocking list,

Regex expressions can also be used, i.e. to avoid concurrent builds for all projects starting with React-, add React-.* to the list,

Replace React-tests with any pipeline-name you want not to run parallel, with global or node level options, Blocking Builds Setup

When tried to run any blocked jobs together with configured React-build job, it gets moved to pending state, pending concurrent builds

Akshay Chandran
  • 985
  • 11
  • 16
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '21 at 13:12