How can I configure execution start between dependent jobs? - jenkins

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.

Related

Jenkins - Trigger Job after certain time

I am trying to see if there is a plugin that can do what I want or something I am missing with regards to Jenkins triggers. To give you an example of what we want to do let me explain how things are happening currently.
A merge is made
Jenkins picks up on merge, pulling changes on remote build machine
Server is stopped
Build, checks, etc are done
Server is started
So the above is all well and good and working, however what we want to do is trigger the server stop and build after the merge is picked-up by Jenkins. Here is the catch though, it is a large project, with multiple tracks and we could have say 4-10 merges done within a 10-30 minute window. So obviously we do not want to have 4-10 jobs in the queue all running the same thing.
So what would be the best approach to achieving the above, i.e. Jenkins triggers based on merge, say waits for x minutes, if no other merges, then triggers the build process, if new merge reset counter back to x minutes and wait again?
Are there any plugins or triggers built into Jenkins that we can achieve this with? (I couldn't find anything obvious) Or is this a case we need to parameterise the build and have some script running?
Not aware of any plugin which does this. But if you're using the job type Pipeline or willing to convert it to Pipelines, then the following Jenkins pipeline will do the trick:
// Sleep for a certain time, in this case 20 seconds
sleep(20);
// Check if there is a newer build, if there is abort this one.
if (currentBuild.nextBuild != null) {
echo "Got newer build, aborting this one!"
currentBuild.result = Result.NOT_BUILT;
return;
}
// Do the rest of building here
You can run the below command from URL and it serves the purpose.
https:///build?delay=600sec

How can I incorporate a long delay into a Jenkins build process?

I am using Jenkins to deploy changes to a system which manages and runs lots of different jobs which are scheduled daily. We have a staging setup which does not write to the real database, and a production setup which does.
The Jenkins flow I would like to have a happen when a change is pushed is this
Run checks.
Deploy to the staging system.
Wait 24 hours.
Check logs to make sure that the staging system has not had any errors in the last 24 hours.
Deploy to the production system.
There could be more than one of these builds running concurrently at any time - eg. I push changes at 11 am, they are deployed to staging. At 5 pm I push more changes and they are also deployed to staging. The next day at 11 am the first set of changes only are deployed to prod. At 5pm that day the second set of changes are deployed.
Now, I have managed to build a system which does this, by using the Build Flow Plugin, and creating a job called wait_one_day which runs sleep $((24 * 60 * 60)) in a bash shell.
This doesn't seem like the most elegant solution, and has the disadvantage that I am tying up two Build Executors for 24 hours (one for the build flow job, and one for wait_one_day), each time we make a change.
Is there any better way of doing this, or any plugin which is designed to help with this process? Can a Jenkins job schedule another Jenkins job to run as a one-off?
I would equally be happy to hear about an alternative approach to solve the same problem if anyone has any suggestions or constructive criticism of my design.
There was similar SO question recently that I answered, although I'm not sure that my answer there exactly fits your scenario.
You could potentially dynamically create a job that does steps 4 & 5 which would run periodically every 24 hours. The catch here is that you would actually only run this job once, and have a build step in that job that deletes itself (groovy code or shell script). It would be easy enough to create a deactivated template job that you could just clone and then modify for the particular task. An intermediary job would be necessary which would trigger upon completion of any job that runs steps 1 and 2. The intermediary job would then create the temporary job from the template.
Alternatively, you could create some sort of handler, either within jenkins or external that would run off of some properties file or database containing the scheduling for when jobs need to be fired off. Granted, if you are going to go the route of writing a handler, you might consider putting in a little extra effort and writing a jenkins plugin...

Jenkins job wait for first successful build of other job

I have a Jenkins job that should not start building until another job has been built successfully at least once. They are not related per se, so I don't want to use triggers. Is there a way to do this?
Some background: I'm using SCM polling to trigger the second job. I've looked at the Files Found Trigger plugin, but that would keep on triggering the second job after the first on has been built. I've also found the Run Condition Plugin, but that seems to work only on build steps, not on the entire build.
Update - The second job copies artifacts from the first job. As long as the first job has never completed successfully, the Copy Artifact step fails. I am trying to prevent that failure, by not even attempting to build the second job until the first job has completed once.
One solution is to use the Build Flow plugin.
You can create a new flow job and use this DSL:
I've used 10 in the retry section but you can use any numbers.
This flow job can be triggered by monitoring the same SCM URL of your second job.
Update, here is a second solution.
You can use the HTTP Request plugin.
If you want to test that your first job has been built successfully at least once, you can test this URL:
http://your.jenkins.instance/job/your.job/lastSuccessfulBuild/
One example:
As my build has never been successful, the lastSuccessfulBuild URL doesn't exist. The HTTP Request changes my build status to failure.
Does it help?
The Block queued job plugin can be used for this: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin

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

How to enforce synchronous execution of separate Jenkins jobs?

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.

Resources