简体   繁体   English

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

[英]How i can trigger build from jenkinsfile using cron syntax?

im using multibranch jenkins style each branch has its own jenkinsfile, i have added triggers in jenkinsfile but it didnt trigger anything on the specified time (8pm), im not sure if im missing something我使用多分支 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')
      }
    }

For the change in cron to catch, you need to run your Jenkinsfile once manually in the correct branch .要捕获cron中的更改,您需要在正确的分支中手动运行一次 Jenkinsfile。 After that, check in "View Configuration" that your cron succeeded ("Build periodically" should be checked and contain the schedule).之后,在“查看配置”中检查您的 cron 是否成功(应检查“定期构建”并包含计划)。

If it has not, it could be that, at the time when triggers are evaluated, your env.APP_NAME differs from 'DICTIONARY' .如果没有,则可能是在评估触发器时,您的env.APP_NAME'DICTIONARY'不同。

To debug the env , you may add the following:要调试env ,您可以添加以下内容:

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

pipeline { 
 agent {
    node {

As a side-note, it's recommended using H instead of minutes, so not all hourly builds fall exactly on the hour:附带说明一下,建议使用H而不是分钟,因此并非所有每小时构建都完全按小时计算:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM