2

How can I exclude specific days from a Jenkins scheduld?

For example: every 5 minutes from 7 to 17 and from monday to friday. H/05 7-17 * * 1-5

But, if the day of the week is a public holiday, it should not run. How can I configure this?

Thx

3 Answers3

2

Currently Jenkins crontab does not support a complicate logic such public holidays exclusion. However, there are some options out there you can use to accomplish that:

First

For my project I create my own holiday database, which it is a file containing the days I want to exclude eg.:

# /path/to/holidays
# New Year's Day
01-01-2017
# Christmas
12-25-2017

and I check it using a Jenkins shell script, as it was proposed here.

For example for the above file format:

#!/bin/bash

TODAY="`date +%m-%d-%Y`"
if grep -q $TODAY /path/to/holidays; then
   echo Skipping holiday for $*
   exit 0
fi

$*

Second

A more robust solution but more complicate is to create your own plugin based on the Run Condition Example Plugin in which you exclude the public holidays of your Country such as this plugin.

Community
  • 1
  • 1
afxentios
  • 2,334
  • 2
  • 20
  • 23
  • Thx for the hints.... ...but for the first one. I have Jenkins runnings as a Win Service. Not Linux. An idea for Win Services? –  Dec 19 '16 at 10:09
  • Sorry I am not familiar with Windows Task Scheduler but you may find helpful this post: http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron. Maybe a Windows guru can provide you with some tips to replicate the first approach on a batch script. – afxentios Dec 19 '16 at 10:19
  • 1
    Thanks. Looks like scripting Win start and stop Jenkins depending on o holiday routine.... –  Dec 19 '16 at 11:11
1

There is another option now, with the Jenkins Working Hours Plugin.

Using the plugin, you can configure both working hours: configure working hours Or Holidays as 'Excluded Days':

Excluded Days

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'
  }
}

Sample job (declarative pipeline):

pipeline {
  agent any
  options {
    enforceBuildSchedule()
  }

  stages {
    stage('Do some stuff') {
      steps {
        echo 'this can wait til morning'
      }
    }
  }
}

Sample job with branches parameter (works in both declarative and scripted):

node {
  properties([enforceBuildSchedule(branches: ['dev', 'qa', 'prod')])
  stage('Do some stuff') {
    echo 'this can wait til morning'
  }
}
pczeus
  • 7,195
  • 4
  • 34
  • 50
0

Using the idea of one of the answers and using Declarative + Script block I created this:

        stage('Check if Today is Holiday') {
            steps {
                // Based on idea from https://stackoverflow.com/a/41219757/7820857
                script {
                    IS_HOLIDAY = sh(script: 'grep -q $(date +%Y-%m-%d) /etc/holidays', returnStatus: true)
                    if (IS_HOLIDAY == 0) {
                        currentBuild.result = 'ABORTED'
                        error ('Today is Holiday according to the file /etc/holidays inside the Jenkins server')
                    }
                }
            }
        }

This will depend on the file /etc/holidays inside the Jenkins server. Adding this additional Stage before will help you to identify if the day mentioned is holiday and exit with error message, or not and continue with the rest of the stages.

I will like that the Working Hours Plugin worked for this but they queue the jobs in case that the day is inside the Excluded days, but I need to cancel the job execution. A feature request exist for that user case.

julian-alarcon
  • 129
  • 2
  • 8