"Build Periodically" with a Multi-branch Pipeline in Jenkins
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'
}
}
}
}
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
This is working for me:
triggers {
cron(env.BRANCH_NAME == 'development' ? 'H */12 * * *' : '')
}
See more in this article
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 * * * *')]),
]
)