我如何使用 cron 语法从 jenkinsfile 触发构建?

How i can trigger build from jenkinsfile using cron syntax?

我使用多分支 jenkins 风格,每个分支都有自己的 jenkinsfile,我在 jenkinsfile 中添加了触发器,但它没有在指定时间(晚上 8 点)触发任何东西,我不确定我是否遗漏了什么

 agent {
    node {
      label 'master'
    }
  }

  triggers {
           cron(env.APP_NAME == 'DICTIONARY' ? '00 20 * * *' : '')
  }

  stages {
    stage('SCM Checkout') {
      steps {
        git(branch: 'test', url: 'https://gitlab.testral.ba/amramework.git', poll: true, credentialsId: 'GitlabCred')
      }
    }

要捕获 cron 中的更改,您需要 运行 在正确的分支 中手动 您的 Jenkinsfile 一次。之后,检查 "View Configuration" 您的 cron 是否成功(应该检查 "Build periodically" 并包含计划)。

如果没有,可能是在评估触发器时,您的 env.APP_NAME'DICTIONARY' 不同。

要调试 env,您可以添加以下内容:

println "env.APP_NAME is ${env.APP_NAME}" // will run before pipeline

pipeline { 
 agent {
    node {

作为旁注,建议使用 H 而不是分钟,因此并非所有每小时构建都恰好在小时内:

triggers {
           cron(env.APP_NAME == 'DICTIONARY' ? 'H 20 * * *' : '')
  }