How to block a Jenkins job from running at certain times

9.9k Views Asked by At

I have a Jenkins job which restarts our small in-house production system. It is fully automated, so when we check-in code changes, we run tests and other verification steps and finally automatically deploy the new version.

It is usually not a problem restarting the system as it will be back shortly and people are aware that there might be a small outage now and then.

However there are times during the day when I know that the system is heavily used and thus would like to prevent any automated restarts.

Is there a way to do this, e.g. configure a timeframe where a job is not running at all? I.e. something like "on weekdays from 8 to 10" or any other cron-based schedule.

The only option that I can think of is to periodically disable and enable the job via some cli-commands, but hopefully there is a better way to do this.

3

There are 3 best solutions below

2
On BEST ANSWER

You can use the Conditional BuildStep plugin and set some timeframe where your job can be launched (or not).

Something like that:

enter image description here

0
On

You can also use Jenkins working-hours plugin maintain by @Jeff Pearce

The working hours plugin allows you to set up a schedule of allowable build times; projects can opt in to use the schedule to prevent them from running outside of configured allowable build times. If a build is scheduled during non-working hours then it is kept in the build queue until the next allowable time.

Jobs opt in via the enforceBuildSchedule job parameter, which is provided by this plugin. It can optionally take in a branches parameter to limit it's usage to only those branches. This only works in MultiBranchPipelines.

Usage:

 Sample job (scripted pipeline):
 
 node {   
   properties([enforceBuildSchedule()])   
   stage('Do some stuff')
   {
      echo 'this can wait til morning'   
   } 
 }
0
On

You can also use Jenkins Plugin https://plugins.jenkins.io/datetime-constraint/
It helps you set up the timeframes throw the UI. You can do as following

Monday before 10, after 8
Tuesday before 10, after 8
etc.

The pipelines will abort during these times automatically.