Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent two Jenkins projects/builds from running concurrently?

Tags:

jenkins

hudson

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.

like image 986
jackr Avatar asked Apr 12 '12 00:04

jackr


People also ask

What is concurrent builds in Jenkins?

Jenkins allows for parallel execution of builds for a Job. Job configuration page has a check box, "Execute concurrent builds if necessary". Also, in the master node configuration set the "# of executors" field to more than 1. Once these two are done, parallel job execution is enabled.

What is the purpose of chaining in Jenkins?

Job chaining in Jenkins is the process of automatically starting other job(s) after the execution of a job. This approach lets you build multi-step Jenkins build pipelines or trigger the rebuild of a project if one of its dependencies is updated.

Can we run multiple builds in Jenkins?

Sometimes multiple builds are daisy-chained together to create a pipeline, but even with a pipeline, each step in the chain represents a single build job being run. In this Jenkins Matrix plugin example, we will show you how a single Jenkins build job can actually run multiple, parameterized builds simultaneously.


2 Answers

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.
like image 76
Jason Swager Avatar answered Sep 23 '22 10:09

Jason Swager


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

like image 36
gaoithe Avatar answered Sep 23 '22 10:09

gaoithe