Jenkins构建管道预定触发器

use*_*783 29 jenkins

如何构建管道可以安排在夜间的某个特定时间执行,就像常规工作一样?

小智 27

您可以使用以下语法设置作业参数:

properties([pipelineTriggers([cron('H 23 * * *')])])
Run Code Online (Sandbox Code Playgroud)

将此行添加到构建脚本或Jenkinsfile将配置作业每晚11点运行.


Bar*_*oży 26

声明性管道有triggers指令,一个像这样使用它:

triggers { cron('H 4/* 0 0 1-5') }
Run Code Online (Sandbox Code Playgroud)

我从Pipeline Syntax docs中获取了它

  • 虽然尚不支持条件触发器,但存在一些解决方法,请参阅:https://issues.jenkins-ci.org/browse/JENKINS-42643。基本上,您必须在声明性管道之外定义一个变量 `String cron_string = BRANCH_NAME == "master" ?"@hourly" : ""` 然后将 `cron_string` 传递给 `cron` 触发器。为我工作! (5认同)
  • 仅供参考:这将在其"Jenkins文件"中构建包含此行的每个分支; 也就是说,如果你在`master`或`develop`分支和所有正在进行的功能分支中都有这个,它们都会按照这个时间表触发. (4认同)
  • @dwj,在大多数情况下不是想要的! (3认同)

Sat*_*ave 9

完整示例(摘自文档)参考:https : //jenkins.io/doc/book/pipeline/syntax/#triggers

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

  • 您需要触发一次构建才能添加触发器。我花了一些时间才意识到 - https://issues.jenkins-ci.org/browse/JENKINS-47539?focusedCommentId=317448&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-317448 (2认同)

joh*_*nic 7

进入管道的主要作业配置(第一个),设置"定期构建"复选框,并指定所需的计划.

按照语法指示.

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 *
Run Code Online (Sandbox Code Playgroud)

  • 如果使用多分支管道,您将如何为特定分支执行此操作? (2认同)

小智 6

如果您想使用多分支管道定期为特定分支运行作业,您可以在 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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您需要第一次手动运行此作业才能添加。