Jenkins: how to block a job based on results of another job? - jenkins

Job1 runs whenever code is delivered.
Job2 runs three times per day.
These are independent, non-hierarchical jobs.
My requirement is to run Job2 only if Job1 results in SUCCESS or UNSTABLE.
I checked out BuildResultsTrigger. It's not working.
Also, I'm aware of pipeline plugin but that'll be a long term goal.
For now, I need a quick way to allow Job2 to run, only if the last execution of Job1 was SUCCESS or UNSTABLE. Job2 can start, as long as it does not complete its build steps. If I can insert a build step to the beginning of Job2 to check Job1 result and somehow make the Job2 fail, that would work for me.

Use build triggers to achieve this task..you have many choice 1. triggers, 2.Multijob plugin, 3. Build flow plugin

#user2984213 the easiest and fastest way would be to:
At the end of the job 1 create a file somewhere and call it like green_light.txt if job is successful. Otherwise - force remove the file.
Then at the start of the job 2 check the file existence.

Related

How to make a cycled sequence of jobs in Jenkins?

I'm new to Jenkins. I have a stack of 5 different jobs. I want them to execute endlessly, meaning the first one will start after successfull finish of 5th. Is there a way to do this? Is Jenkins pipeline plugin the right tool to make this?
If you already have 5 non-pipeline jobs, the easiest way is to
set a downstream job on each job as it makes a loop.
In the configuration page of each job, Add post-build action > choose Build other projects > Enter a name of a downstream job.
in pipeline there is a build step (https://jenkins.io/doc/pipeline/steps/pipeline-build-step/), but I haven't tried if this can take trigger itself (most probably).

How to trigger a Jenkins job by another job at a certain time?

I have two jobs, JobA and JobB, JobA runs every day at 13.00 and registers some payments. I want JobA to trigger JobB which verifies the payments, if and only if JobA is successful and JobB needs to be run the next day at 04.00
Any idea how to do this?
BR
I have not been able to find anything that will do this out of the box. You can, of course, schedule a job to build periodically, but that's not all that you want.
You could try one of these 2 ideas (I have not implemented either myself).
Set JobB to build periodically at 4am, but deactivate the job. Create an intermediary job with a build trigger to build if JobA is successful (e.g. set JobA in the projects to watch section). The intermediary job would run code to activate JobB - groovy code using the groovy plugin would be easiest, or you could use the rest api via shell / batch script. Then as the last build step in JobB, run a similar script to deactivate the job again.
The Schedule Build Plugin allows you to schedule future builds. However, it appears that this is a manual process. If you can figure out how to programatically fire off a scheduled build through this plugin, you could add that code to an intermediary job that is setup in the same way as mentioned in option #1.
You can use build job: step in pipeline (https://jenkins.io/doc/pipeline/steps/pipeline-build-step/) and quite period in seconds to trigger the job at required time.
EDIT:
The way i did
def currentDate = GregorianCalendar.getInstance()
If you want next day at 5 am
def plannedDate = new GregorianCalendar(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH) + 1, 5, 0)
def quietPeriod = (plannedDate.getTime().getTime() - currentDate.getTime().getTime())/1000

Jenkins to run 10 jobs sequestially and few parallely and only run jobs what I choose

Jenkins to run 10 jobs sequentially and few parallely and let say 1-2 jobs are successful and job3 failed. I fixed the job3.Now I want to again trigger the job but only want to trigger it from job3.All 10 jobs have string parameter.
I can pick an choose what jobs to trigger. how can I achieve it.Please help.
The whole point of CI is to have multiple runs frequently, so that in next run you can see them all stable. Not sure why you wanted to do this. However, You can take 2 approaches.
You can use conditional Step plugin along with multiphase plugin,
wrap you build step with condition and pass whether you want to run
it or not. only if the condition is passed then the individual jobs
will be triggered.
You can use jenkins-cli and trigger the jobs
which ever you wish.

Excluding a job from the Jenkins Job Queue when a build is in progress

I have a long-running job (10 hours) scheduled to run twice a day (at 11am/11pm).
I generally only want to commence the build at 11am/11pm.
If the previous build fails, I sometimes want to start the next build early (e.g. 9am).
How do I do it? If I manually kick off the build at 9am, the scheduled build will go into the queue at 11am, and will execute as soon as the first build completes. I don't want that, if I manually start, I want to skip the scheduled build.
Another way of thinking of it ... I want to ignore a scheduled (or manual) build request if there is a build in progress.
To do what I'm wanting I've added the following into conditional build steps at the start and end of the job, conditional on the Build Cause of UserCause.
jenkins.model.Jenkins.getInstance().getItem('Test').disable();
jenkins.model.Jenkins.getInstance().getItem('Test').enable();
Peter,
https://wiki.jenkins-ci.org/display/JENKINS/Run+Condition+Plugin
Above plugin will be useful for you.kindly go through the documentation.
Try this Plugin:
https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin
It will allow you to block job when last build is in progress.

How to execute only the most recent queued job in Jenkins?

I've got a commit build project in Jenkins which schedules an acceptance build project on completion. Since commits come in faster than the acceptance build job finishes, after a short time there are now six queued acceptance build jobs. I would like the acceptance build project to work like the "Poll SCM" functionality - On completion, start the most recently queued job, skipping the rest.
I can't use the "Build after other projects are built" without more hacks since I need to pass information from the commit build job to the acceptance build job.
#l0b0,
Jenkins behavior is to coalesce builds so that the queue only contains the currently running build and one enqueued job. The depth only increases if the newly enqueued jobs takes parameters that differ from what's already on the queue.
So I'm gathering that your downstream (acceptance) job takes some sort of parameters, but you need to supply more details of how it's working.
If you're using parameterized trigger plugin then you should check out this existing SO thread
More generally speaking, you should look into your parameters. It sounds like you are passing too much information from upstream to downstream jobs, resulting in the the Jenkins queue treating them as distinct parameters when then is not necessarily the case.
Are you passing in the run number of the last successful upstream job as a parameter? If so, then yeah you've got problems. What you should do instead is use the Promoted Build Plugin on the upstream job to mark the last successful build, and then have the downstream job simply jump to the most recent promoted build.
Hope that helps.

Resources