Jenkinsfile | Declarative pipeline | Run a Job after some specified time (in background) - jenkins

I have a requirement to run a destroy job (which will destroy the instances) after testing. The testing will take around 1hr. So the instances can be destroyed after that, adding some leisure time, say after 2hrs.
Jenkins file
Run job-1
Run job-2 - deploy in lower environment
Run job-3 - destroy lower environment after 2hrs of current-time
Run job-4
Run job-5 - after 3hrs
All the jobs should run one after another without wait. And there I am stuck !!
timer - will wait until the given time completes and abort :(
sleep - will wait until the given time and runs next job/whatever :(
trigger - will trigger the job but with cron functionality :(
I am ok to use trigger if my requirement can be accomplished with it.
Or any groovy code to set trigger time (set cron time [currentTime + 3hrs])!
Or
simply - I want to run a cronjob ONLY for one time [just after 3hrs of Now]
Note: I am a newbee to groovy

Use Quiet period set to 120 mins for the last job - Job-5
Groovy syntax is build job: 'Job-5',quietPeriod: 120, wait: false

Related

Need help to schedule jobs in Jenkins

I have scheduled my job in jenkins as "0 0-12/3 * * 1-3". This will run monday to wednesday from 12am to 12pm for every 3 hours.
My question is, during the above interval if any build execution is PASS, then it should skip remaining scheduled interval for that week!!!
How to do this? can anyone Help me?
You could try working the other way: Schedule your job to run only once per week. Then add either the Naginator or Periodic Reincarnation plugin to your Jenkins server to restart a failed build.
If that should be more liberal (as in: don't always start the job on the first possible day of the week, but rather check if a week has elapsed since the last run), you could try the following:
Install https://wiki.jenkins.io/display/JENKINS/Run+Condition+Plugin to check for further conditions
Let your job write a file at the end of a successful run
Check through that plugin whether
the file does not exist: run the job
the file is at least one week old: run the job
else: skip the job

Is it possible to pause a stage until a time is reached in a Jenkins declarative pipeline?

I have a requirement to only allow deployments to production in a specific time range, e.g.: between 04:00 and 06:00.
My Jenkins pipeline is triggered by a Git webhook and starts a flow consisting of the usual stages (Build, Test, Deploy Dev, Deploy Pre, Deploy Pro).
When we reach the Deploy Pro stage I would need to check whether the current time is in the allows range to go on. Otherwise I need to pause the pipeline (freeing the Jenkins agent) until that time range is reached.
Is is possible to implement this? How?
To do this, you can use sleep method, which can get different units: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS (by default it's SECONDS).
E.g. sleep(time:3,unit:"SECONDS") (or just sleep 3 - it's the same).
So, check whether the current time is in the allows range, and if it's not, then specify time for sleep method as value of 04:00 - <current time> (you need to use some arithmetics here to get right value in seconds or minutes, but it shall be not hard).
But if all the other stages are quite fast, I recommend you to use Poll SCM option (with specifying time range) for all pipeline, because it's much more easier and the result will be the same - job will be finished only after 04:00.
Your Jenkins pipeline is triggered by a Git webhook and starts a flow consisting of the usual stages (Build, Test, Deploy Dev, Deploy Pre, Deploy Pro).
Option 1:
skip the deployment using 'when' condition and if time is not between
Option 2: separate the pipelines for CI and CD And CD will trigger only during that or keep in pause stage using shell cmd

jenkins msbuild and long delays

I have a jenkins job that calls a windows batch command, called "build" - which just calls msbuild, zip and a few other trivial things.
It works perfectly.
Except - at the end of the batch file - once we've exited - Jenkins just pauses. For like 20 minutes. And then it continues running postbuild stuff as if nothing happened.
I get the feeling its trying to kill of child processes or something, but anyone got any ideas? I currently have a 22 minute build, where 2 minutes of it are spent actually building.
I faced the same issue and, at least here, it was due to Jenkins job waiting for all forked processes to end, when msbuild, by default, is spawning node processes which remain active for later reuse (see nodereuse flag of msbuild, which is set to true by default).
Once nodereuse has been set to false, the waiting time at the end of jenkins job vanished. Worth a try on your side.

Periodical build and start another job at JENKINS

I have the following scenario in JENKINS:
Job1 - Will run periodically every 10 minutes, but should stop the periodic construction when it gives "SUCCESS". Attention to this, he must build until he succeeds every 10 minutes, when he succeeds, he must activate the next job.
Job2 - Must be triggered by Job1 only once;
How to solve? Are there any plugins for this questioning?

Skip pipeline execution on several condition

I have one pipeline build called, for example UAT. This build is scheduled every 3 minutes. And another build called DEV. DEV is scheduled every minute. The task is: to run UAT only if the last DEV execution was SUCCESS. If not - skip the execution. And run it after other 3 minutes with the same condition.
How can I achieve that ?
Don't schedule your UAT job as a separate job but instead trigger the launch once your first DEV pipeline finishes with success.
As you are using pipelines you actually have 2 solutions :
1)
Don't call another job but just call a Groovy function to integrate the DEV part, such as :
node() {
stage "UAT"
// Your existing UAT pipeline content here
stage "DEV"
git 'http://urlToYourGit/projectContainingYourDevScript'
pipeline = load 'functions.groovy'
pipeline.dev()
}
2) Just call a second Jenkins job with this kind of line :
node() {
stage "UAT"
// Your existing UAT pipeline content here
build job: "dev-job"
}
With these 2 solutions you can configure your first job to run every minute and it will trigger the second part/job only if the first one finishes with success (otherwise Jenkins will just fail the build as it would normally do).

Resources