Triggering a Jenkins job on a timer from a job run through polling - jenkins

We currently poll svn and run a job if there are any changes. We then trigger a job if the initial job passes.
Additionally, I'd like to trigger a second job that only runs once a day. So if the initial job (job 1) runs 40 times, job 2 would also run 40 times, but job 3 would only run 1 time. (It can be decoupled as long as job 3 knows exactly what machine the last instance of job 1 ran on)
My initial thought was to use a plugin similar to Node stalker (https://wiki.jenkins-ci.org/display/JENKINS/Node+Stalker+Plugin) to just get the value of the node the previous run was on. The plugin doesn't appear to be working (it runs on whatever node as if the plugin does nothing).
Is there another way of doing this?

I am unaware of another way to do this in a similar manner to node stalker, however two other options come to mind.
The ugly:
If all the machines have a network drive they can access just keep a text file and when a machine completes job 1 and 2 successfully then it updates that text file with a unique identifier for that name, then Job three reads that file and it knows who ran jobs 1 and 2 last.
The less ugly:
This one depends on how long it takes to run jobs 1 and 2 (shorter is better, longer may not be feasible). Run a 4 job chain:
job3 launcher -> job 1 -> job 2 -> job 3.
This way you can track what machine is being used for job 1 and job 2 and pass those along as build variables into job 3.

Related

Remove unnecessary jobs from build pipeline on Jenkins

I'm a bit new to Jenkins and I'm having an display issue with Build Pipeline plugin. I'm executing some jobs in parallel using JobFanIn Plugin, i.e. the next job in the pipeline will only be executed when all the previous jobs are concluded. However, the Build Plugin believes that all jobs will trigger a new instance of the last job. The execution goes right, but the display isn't.
Bellow is possible to observe what is happening in practice. The GENERATE TEST REPORT job will only be triggered when all TEST jobs are finished and this occurs ok. But since the Build Pipeline plugin expects 3 instances of this job to happen, only one happens and the others appeat as pending forever.Any ideas?
First image displaying what is happening
Second image displaying what is supposed to happen

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 - Triggering Scheduled Jobs

Is there a way to trigger a scheduled job?
I am building a pipeline of connected jobs which trigger each other once complete. I want one of the jobs to be scheduled to run at a particular time of the day. So I want to be able to essentially add to a queue to trigger at a later time.
Is that possible?
Cheers
To schedule a Jenkins job to run at a specific time, go to the job's landing page, click Configure from the left-side menu, scroll down to 'Build Triggers' section and pick 'Build Periodically'.
There you can specify the cron job formatted string to denote when and how often you'd like this first job to be scheduled for running.
If that job is not the first job in line, you can always use 'Quiet Period' option under 'Advanced Project Options' which puts in a delay in that job before the build steps actually run. You can specify the number of seconds you want that job to wait for before it actually executes.
This plugin seems to be piggybacking on the 'Quiet Period' feature though I haven't tried it myself: https://wiki.jenkins-ci.org/display/JENKINS/Schedule+Build+Plugin. You might have luck using it to your advantage.
You could use the Jenkins Workflow plugin suite (mentioned in your tag, perhaps unintentionally). It has a sleep step. If you wanted the next stage of the pipeline to run at a particular time of day, rather than after a fixed interval, you could do some simple computation with java.util.Calendar to determine the seconds between now and then.

Is there a way to resume from a failed job in Jenkins?

I am using 4 Jenkin jobs for Continuous Build and Deployment.The jobs are created for label creation, build using the label, deploy to server1 and deploy to server2 and so on..
The Create_label build will run the MYSQL query in database and get the label. Create_label job on success will invoke the Build-job with the label and upon successful completion of Build-Job, it will invoke Deploy-server1 job and so on..
If the build fails in Build-Job and after that If I start the 1st Create-Label job then it runs the job and creates the NEW label. I mean running the create-label every time will get you incremental label. I can only get the label from 1st job and then pass to 2nd job. Can anyone suggest how to resume from the failed job with out running the first job and start with second job using the old label created(from the last build in 1st job). any work around to accomplish this type of situation in jenkins.
1.Create-Label
2.Build-Job
3.Deploy-server1
4.Deploy-server2
So it seems, you have 2 issue at the moment:
Issue 1 --> If any job fails, the next time it should skip the successful jobs and restart only from the failed job.
Probable Solution --> You could use Build Pipeline plugin, https://wiki.jenkins-ci.org/display/JENKINS/Build+Pipeline+Plugin , and if the job fails at say second job, then all you have to do is restart the second job from the pipeline and it would continue from there.
Issue 2 --> For job 2, you not only want to restart it but have to take the input, Label Number, from the previous successful job 1, without restarting job 1
Unfortunately, I don't have a clean automated solution for this. The only thing I can think of requires manual intervention, i.e. since this job is parameterized, change the default value of the parameter to the Label Number from the last successful job 1 before restarting it from plugin(The above plugin doesn't ask for parameters while running the job)
or let Job1 login the label number in some centralized location and let job B take the parameter from there instead of taking it from jobA.
Thanks,
Manish Joshi
I am using MultiJob plugin and you can always just click "Resume build" in the parent job to continue from a failed job.
And in this case the parameters for its execution will be all the same as on those previous run which failed.

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