Jenkins - Trigger Job after certain time - jenkins

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

Related

Can I automate builds without having acces to P4 server?

I am trying to setup a jenkins server for the very first time. I have synced it to the Perforce server I use, and I have created a workspace for that. Now I would like jenkins to start building everytime a change is submited, I have been researching through the topics here, and I found this link: How to trigger a Jenkins build on a Perforce submit. But it mentions that in order to do that I have to create a script on the P4 server On the Perforce server, it is possible to create triggers or, in other words, scripts to be run on a particular event - for example after a change-commit., I do not know what it means to create a script on the P4 server. Does it mean I need to have physical access to the server? I am just connecting to a remote server. I am kinda lost here...
Ideally, you'd have access to be able to create a trigger. If you can't do that, the next best thing is to create a cron job or scheduled task (depending on your OS) to check to see if there is something new. I've run similar jobs to:
Run p4 sync -n to see if there is anything new
If there is something new, a) sync it and build it (your choice of everything, or you could write something that would test, say, the first changelist for which there is something new, then keep doing so changelist-by-changelist)
If there is already a build in progress, don't run.
My jobs were set to check every five minutes, but I even had a project where once/hour was enough.

Jenkins: Trigger a future build once a job is completed

I can't seem to find the best way to do that.
I have a Jenkins job I ran manually (say a job that creates a temporary file somewhere).
I want to be able to clean this up (e.g. delete the temporary file) after 2-3 hours.
I was thinking adding a Post Deploy action that will run a job with some parameters + some delay. But I can't find anything like that.
I've noticed several schedule plugins or REST API options, but nothing that as simple as "When Succesfully Building A, Build B in X minutes".
Suggestions?
Suggestions?
You could exploit the Quiet period.
job1 -> (sleep -> fake-job) -> job2
so let job1 trigger the fake-job whch has a quiet period of 7200 seconds and this fake-job will trigger your cleanup job.
You can use Parameterized Trigger Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin).
Here you can pass predefined parameter to Project to build like sleep_time parameter and you can handle it in triggered Jenkins job. Also you can configure Trigger build on Stable / Unstable / always trigger conditions.
I hope it will be useful for you.

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 builds all starting late

We've inherited a set of Jenkins builds. They all seem to start 90-100 seconds after the desired time. For example, a build with a schedule of */5 * * * * starts at :01:37, :06.29, :11:43, etc., instead of :00, :05, :10, etc. that I would expect.
There are a few builds set to run at /5, but they are all delayed and anyway only last a few seconds each.
I see a global 'quiet period' setting of 5.
The system as a whole does not seem busy. There are usually idle executors, and often nothing at all is building.
For most of the builds this isn't a concern, but there are a couple that we would like to make as precise as possible.
Is my expectation wrong? Is there a config option I'm missing? I should add that I am new to Jenkins and may be missing something obvious.
Thanks
We did not find the cause of Jenkins jobs starting late. We hacked up a workaround by having Jenkins start a script on the remote server that sleeps until the desired time. This creates a new problem by tying up a Jenkins executor for several minutes, so we have the remote script spawn a wait task and then return to Jenkins immediately. This creates another problem in that the output of the remote script is lost because when it completes it has no connection to Jenkins anymore. We get around that by having the remote script write its results into a tmp file, and returning the results of the previous run.
So we have a seriously hacked-up solution that actually works fine for our purposes.
We updated Jenkins from 1.492 to 1.565 and the problem went away. Jobs now start within a few seconds of the expected time.

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