在 Jenkins 中使用多分支管道“定期构建”

我用管道插件运行 Jenkins 2。我已经设置了一个多分支管道项目,其中每个分支(master、 development 等)在根目录中都有一个 Jenkins 文件。设置这个很简单。然而,我不知道如何让每个分支定期运行(而不是分支索引) ,即使代码没有改变。我需要在 Jenkins 文件中放入什么来启用定期构建?

77406 次浏览

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 * * * *')]),
]
)

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

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

I hit issues with the above solutions.
I'm not a Jenkins wizard so not sure if I am using an old format/syntax or something, but the following is working for me.

#!/usr/bin/env groovy
properties(
[
pipelineTriggers([
[
$class: 'TimerTrigger',
spec: 'H 7,19 * * *'
]
])
]
)

Determined from: https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/triggers/TimerTrigger.java