首页 > 解决方案 > 为 Jenkins 配置作为代码插件 (JCasC) 定期构建语法

问题描述

我正在尝试使用配置即代码 (JCasC) 插件来创建一个定期构建的管道作业,但我在网上的任何地方都找不到此语法。我正在用 YAML 编写配置。

“定期构建”字段位于管道作业中的“构建触发器”下,并有一个名为“计划”的文本字段。我的日程是0 6-19 * * *

这甚至可能吗?

这是我要编辑的 yaml 文件:

jobs:
  - script: >
      folder('test1'){
        pipelineJob('test1/seedJobTest') {
          description 'seedJobTest'
          logRotator {
            daysToKeep 10
          }
          definition {
            cpsScm {
              scm {
                git {
                  remote {
                    credentials "xxx"
                    url 'xxx'
                  }
                  branches 'refs/head/master'
                  scriptPath 'Jenkinsfile'
                  extensions { }
                }
              }
            }
          }
          configure { project ->
            project / 'properties' / 'EnvInjectJobProperty' {
              'on'('true')
              'info' {
                'propertiesContent'('BRANCH=master')
              }
            }
            project / 'properties' / 'org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty' {}
          }
        }
      }

标签: jenkinsyamljenkins-pipelinejenkins-pluginsjcasc

解决方案


如果使用 JCasC 来配置您的构建/管道配置:

要定期构建,无论 SCM 更改如何,您都可以添加此块:

triggers { 
    cron('0 6-19 * * *')
}

要定期构建,只有在 SCM 发生更改时,您才能使用此块:

triggers {
    scm('0 6-19 * * *')
}

要在上下文中查看此答案,这是一个代码片段示例

jobs:
  - script: |
    job('PROJ-unit-tests') {
        scm {
            git(gitUrl)
        }
        triggers { 
            cron('0 6-19 * * *')
        }
        steps {
            maven('-e clean test')
        }
    }

摘自和调整的片段:https ://github.com/jenkinsci/configuration-as-code-plugin/issues/876


推荐阅读