Reputation: 691
How can a build pipeline be scheduled to execute at a certain time of the night just like a regular job can be?
Upvotes: 67
Views: 143021
Reputation: 59
This code is part of a shared library. For example, it could be defined in this file: my-shared-library-repository/vars/myPipeline.groovy
In a different repository, let's call it, my-repository
, we can have a Jenkinsfile
that will use this pipeline.
For example: /my-repository/Jenkinsfile
:
@Library('[email protected]') _
myPipeline('master')
Once we already have this code merged in the master
branch of my-repository
if we run the job for the master
branch in Jenkins, this cron job will be configured with the values for the cron job implemented in the master
branch due to the value of BRANCH_NAME is master
Check triggers - cron
This cron job will re-run periodically the current job, that means, the job for the master
branch.
Another example:
We can call this pipeline like this:
/my-repository/Jenkinsfile
@Library('[email protected]') _
myPipeline('devel')
If we run the job for the master
branch the cron job won't be configured, however, if we run the job for the devel
branch, the cron job will be set, because the value of BRANCH_NAME is devel
. We will have a cron task running the job for the devel
branch periodically.
Upvotes: -1
Reputation: 59
If you want to run the job periodically for a specific branch using a multibranch pipeline you can do this in your Jenkinsfile:
def call(String cronBranch = 'master') {
// Cron job to be run from Monday to Friday at 10.00h (UTC)
String cronString = BRANCH_NAME == cronBranch ? "0 10 * * 1-5" : ""
pipeline {
agent any
triggers {
cron(cronString)
}
stages {
stage('My Stage') {
//Do something
}
}
}
}
You need to run this job manually the first time in order to be added.
Upvotes: 6
Reputation: 2970
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'
}
}
}
}
Upvotes: 30
Reputation: 2672
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
Upvotes: 59
Reputation: 371
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.
Upvotes: 37
Reputation: 804
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 *
Upvotes: 10