I'm running Jenkins 2 with the Pipeline plugin. I have setup a Multi-branch Pipeline project where each branch (master, develop, etc.) has a Jenkinsfile in the root. Setting this up was simple. However, I'm at a loss for how to have each branch run periodically (not the branch indexing), even when the code does not change. What do I need to put in my Jenkinsfile to enable periodic builds?
If you use a declarative style Pipeline and only want to trigger the build on a specific branch you can do something like this:
String cron_string = BRANCH_NAME == "master" ? "@hourly" : ""
pipeline {
agent none
triggers { cron(cron_string) }
stages {
// do something
}
}
Found on Jenkins Jira
-
11This works, but note that committing and pushing the Jenkinsfile is not enough for the trigger to be picked up; the job has to be run once manually afterwards as well. – Cameron Nov 29 '17 at 14:46
-
1Can the trigger be inside of a stage block? I.e. stage('Deploy to production') { triggers { cron(MASTER_TRIGGER) } – Learner Feb 14 '18 at 23:54
-
2Will this work in a declarative jenkins file or only a scripted jenkins file? – Learner Feb 15 '18 at 0:00
-
Should work with a declarative Jenkinsfile. The trigger only works inside the options tag iirc. If you want to only deploy the master branch you can put a script{ if (BRANCH_NAME == „Master“) { //deploy}} inside the deploy block. (I’m currently on mobile so the syntax might be wrong) – Julian Veerkamp Feb 17 '18 at 12:53
-
If you are using a declarative style Jenkinsfile then you use the triggers directive.
pipeline {
agent any
triggers {
cron('H 4/* 0 0 1-5')
}
stages {
stage('Example') {
steps {
echo 'Hello World'
}
}
}
}
-
13Any way to make cron trigger only on
master
branch? To give some context: when team mates create a new feature branch and commit there Jenkins file should still trigger (via poll or push) but what I do not want is the cron trigger fire on these feature branches. – foobarto Jun 9 '17 at 10:33
I was able to find an example illustrating this an discarding old builds, which is also something I wanted.
Jenkinsfile
in jenkins-infra/jenkins.io:
properties(
[
[
$class: 'BuildDiscarderProperty',
strategy: [$class: 'LogRotator', numToKeepStr: '10']
],
pipelineTriggers([cron('H/30 * * * *')]),
]
)
-
1BTW for the first property you can use the
buildDiscarder
symbol to simplify syntax, as Pipeline Syntax should show. – Jesse Glick Jan 27 '17 at 17:53 -
1It does not work in scripted pipelines under jenkins 2.79 (java.lang.UnsupportedOperationException: Undefined symbol ‘pipelineTriggers’) – gileri Sep 22 '17 at 15:11
-
2Eric, just try with this for scripted pipelines:
pipelineTriggers([[$class: "TimerTrigger", spec: "H 1 * * *"]])
– nradev Jan 30 '18 at 17:04
This is working for me:
triggers {
cron(env.BRANCH_NAME == 'development' ? 'H */12 * * *' : '')
}
See more in this article
For Paramertized periodic runs or scheduled triggers, one could use as follows.
triggers{
parameterizedCron env.BRANCH_NAME == "develop" ? '''H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=vbox;VERSION=10.5.0.0
H 03 * * * % buildSlave=vm1;testSlave=vm2;HYPERVISOR=workstation;VERSION=10.5.0.0''' : ""
}