Jenkins Build Pipeline Scheduled Trigger

How can a build pipeline be scheduled to execute at a certain time of the night just like a regular job can be?


Declarative pipeline has triggers directive, one uses it like this:

triggers { cron('H 4/* 0 0 1-5') }

I took it from Pipeline Syntax docs


You can set the job parameters using the following syntax:

properties([pipelineTriggers([cron('H 23 * * *')])])

Adding this line to your build script or Jenkinsfile will configure the job to run every night at 11PM.


Complete Example (taken from docs) Ref: https://jenkins.io/doc/book/pipeline/syntax/#triggers

pipeline {
    agent any
    triggers {
        cron('H */4 * * 1-5')
    }
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Into the main job configuration of your pipeline (the first), set the "Build periodically" checkbox, and specify the schedule that you want.

follow the syntax indications.

the field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:
 MINUTE HOUR DOM MONTH DOW
 MINUTE   Minutes within the hour (0–59)
 HOUR The hour of the day (0–23)
 DOM  The day of the month (1–31)
 MONTH    The month (1–12)
 DOW  The day of the week (0–7) where 0 and 7 are Sunday.

 To specify multiple values for one field, the following operators are available. In the order of precedence,

    * specifies all valid values
    M-N specifies a range of values
    M-N/X or */X steps by intervals of X through the specified range or whole valid range
    A,B,...,Z enumerates multiple values

 Examples:

 # every fifteen minutes (perhaps at :07, :22, :37, :52)
 H/15 * * * *
 # every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
 H(0-29)/10 * * * *
 # once every two hours every weekday (perhaps at 9:38 AM, 11:38 AM, 1:38 PM, 3:38 PM)
 H 9-16/2 * * 1-5
 # once a day on the 1st and 15th of every month except December
 H H 1,15 1-11 *