How to enforce synchronous execution of separate Jenkins jobs? - jenkins

Our Jenkins server is configured with two primary jobs to build apks. Each of those jobs has a child job that installs the APK onto an Android device that is attached to the build server and executes UI tests.
For example:
Project-A-apk
Project-A-tests
Project-B-apk
Project-B-tests
where
Project-A-apk kicks off Project-A-tests
Project-B-apk kicks off Project-B-tests
and
both Project-A-tests and Project-B-tests install and run on the same test device.
The issue is, we can't have the test jobs running at the same time, as they will both try to interact with the same device.
Is there a way to configure a job to wait until some other job (not in it's parent chain) before executing?

I use the Throttle Concurrent Builds Plugin to control when jobs should run concurrently.
Setup a category name such as android-device with Maximum Concurrent Builds Per Node set to 1. Assign this category to the jobs that run the test on the android device. Once the plugin is installed there is a place to assign the category on the job configuration page.
All jobs with the same category name assigned will execute serially instead of concurrently.

We use the Jenkins Exclusion Plugin to manage our builds that have to share a couple of different DB resources. This plug works by allowing you to define Critical Blocks in your build steps. Critical blocks will only run if they can acquire a specific resource. At the end of the block the resource is released. This means that you don't have to block an entire job, just the parts that you need the resource for.

You should try the Build Blocker Plugin:
This plugin keeps the actual job in the queue if at least one name of
currently running jobs is matching with one of the given regular
expressions.
Another way would be to limit the number of Jenkins executors to one. This would ensure that only one job is running and other wait in queue. However, this might block other future jobs that do not access the test device.

One more: Heavy Job Plugin
You can weight your Android jobs to block all executors.

Related

What happens to running Jenkins builds when Jenkins is preparing for safe shutdown?

Does the current running build on Jenkins still completes when you click on "Prepare for Shutdown"?
I want to take admin control on Jenkins to make some config changes so I am clicking on "Prepare for Shutdown" but I want to ensure it completes the current builds before shutting down.
As cloudbees describes,
The Prepare for Shutdown feature prevents any more jobs from running. When all jobs are finished, you still need to restart/stop the instance manually.
It is the http://<jenkins.server>/quietDown command
They also describe various other options:
http://<jenkins.server>/restart
http://<jenkins.server>/safeRestart
http://<jenkins.server>/exit
http://<jenkins.server>/safeExit
http://<jenkins.server>/quietDown
http://<jenkins.server>/cancelQuietDown
Personally, I think they should have called it something different, like setToIdle or blockQueue
In your case, you will want to quietDown, then safeExit or safeRestart (to be safe). Normally you can just safeRestart.
There are some nuances regarding pipelines or jobs that trigger downstream jobs.
There are some plugins that add a button to achieve the same effect.
Saferestart just adds buttons.
Lenient shutdown talks about downstream jobs and also lets you manage nodes.
There's other to stop all jobs (bruteforce) and re queue them after (nice admin). Others also manage the queue.
IIRC, pipelines will complete the stage and can resume after the restart or cancel.

Jenkins plugin code that should excute before any kind of job is run in Jenkins

I am new to Jenkins plugin development. M trying to write a plugin that should be executed before any Multi configuration type job runs in Jenkins.
In this plugin I want to write rules that will check what configuration parameters user has selected while submitting the job, based on selected parameters, I want to decide whether to allow the job to run or to restrict it.
User should be shown reason as to why that job cannot be run in the Console Output.
Does anyone have any ideas which class I need to extend or which interface I need to implement in order to get a hook into Jenkins job run?
You could look at the Matrix Execution Strategy which allows for a groovy script to select which matrix combinations to run. I would think if your script threw an exception it would stop the build.
For background, the multi configuration projects run a control job (or flyweight) which runs the SCM phase then starts all the actual combinations. This plugin runs after the flyweight SCM checkout.
If nothing else, this will give you a working plugin to start from
Disclaimer: I wrote this plugin
Blocked queue job plugin was what I needed
Out of the box that plugin supports two ways to block the jobs -
Based on the result of last run of another project.
Based on result of last run of the current project
In that plugin the BlockQueueItemTaskDispatcher.java extends Jenkin's QueueTaskDispatcher providing us a hook into Jenkins logic to allow or block the jobs present in the queue from running.
I used this plugin as a starting point for developing a new plugin that allows us to restrict project based on parameters selected and the current time. Ultimate goal is to restrict production migrations from running during the day.
Overriding the isBlocked() method of QueueTaskDispatcher gave access to hudson.model.Queue.Item instance as an argument to me. Then I used the Item instance's getParams method to get access to build parameters selected by the user at runtime. Parsed the lifecyle value from it. Checked the current time. If lifecycle was Production and current time was day time then restricted the job by returning non null CauseOfBlockage from isBlocked() method. If that condition was false, then returnedCauseOfBlockage as null allowing the queued job to run.

Block upstream jenkins job while downstream is running

I know about the build blocker plugin but doesn't seem to work. Running Jenkins 1.609.
Job A triggers Job B.
I configured Job A to block if job B is running.
If you trigger A and while A is running you trigger A again, once the first A finishes, it triggers B. Then you get both A and B running which shouldn't happen. I guess that when the waiting A checks if B is running, it might be the case that Jenkins is in a middle state where it finished A but not yet really triggered B and therefore we get both of the jobs running.
How can I get this situation to work?
No need to use plugin. I have been using below solutions for a year.
Job Config --> Advanced Project Options --> Block build when upstream project is building
I would say it depends if you have a chain of jobs which need to be blocked.
I can suggest:
Build Blocker Plugin
Here you can set a regular expression to block certain jobs.
With:
.*PART_OF_YOUR_JOB_NAME*
You can block every job which matches this string

Jenkins - Running concurrent jobs with "circular" parameter

I'd like to run several builds concurrently in Jenkins for the same job. I run at maximum 3 builds concurrently. I want each build to run with a parameter that must be unique from a pool for parameters. For instance, pool=[1, 2, 3]: The 1st build picks "1", 2nd picks "2" and the 3rd picks "3".
I must ensure that different builds can't pick the same parameter.
After building, the parameter is available again.
How can I do it?
Alternative: How can I count the number of builds running in this project and pass it as parameter?
At first, select the checkbox button named build-concurrently-if-neccesary to ensure the same job could build concurrently. you'd better read the help-html seriously before
The isolated environments for building different jobs make that data could not be shared each other in a simple way.
Here is a solution that trigger the buildWithParameters link by jenkins rest api to control the pool in the program procedure of your own.
add a string-parameter in job's config.
post the string parameter to http://$JENKINS_SERVER_URL/job/$JOB_NAME/buildWithParameters
Maybe it's the most convenient way if no available plugin found.
I found a plugin in github and asked the author to publish it. It works well and solves my problem.
Jenkins Parameter Pool Plugin

How can I configure execution start between dependent jobs?

My Jenkins server is set up with two jobs A and B say.
Job A is triggered from changes in subversion, runs unit tests and if successful, creates a WAR and deploys it to another environment.
If Job A succeeds, then Job B triggers. This job runs tests against the deployed WAR.
The problem is that the deployment process takes a while and the WAR is not ready in time for when Job B starts and tries to use it.
I'm looking for ideas on how to delay Job B until the WAR is up and running.
Is there a way, once Job B is triggered to wait for x seconds? I really don't want to put it into the tests in Job B if I can avoid it.
Thanks
There is definitely a way for a job to wait - just put sleep into the first shell build step. Alternatively, you can set 'Quiet period' - it's in Advanced Project Options when you create a build.
That, however, is a band-aid solution to be employed only if other approaches fail. You may try the following: if there is a way to make the deployment process (that job A triggers) right before it finishes to touch a file that Jenkins has access to, then you can use FSTrigger Plugin. See use case 3 there.
The most reliable way to make this work would be to make job A not complete until the deployment is successful, e.g. by testing for a valid response from the URL of the deployed web app. This blog post describes one way to do that.

Resources