jenkinsyamljenkins-pipelinejenkins-pluginsjcasc

Build Periodically Syntax for Jenkins Configuration as Code Plug-In (JCasC)


I'm trying to use the configuration as code (JCasC) plug-in to create a pipeline job that builds periodically, but I can't find the syntax for this anywhere online. I'm writing the configuration in YAML.

The "Build Periodically" field is under Build Triggers in the pipeline jobs and has a text field called Schedule. My schedule is 0 6-19 * * *

Is this even possible to do?

This is the yaml file that I am trying to edit:

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' {}
          }
        }
      }

Solution

  • If using JCasC to configure your build/pipeline configuration:

    To build periodically, regardless of SCM changes, you can add this block:

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

    To build periodically, only if there were SCM changes, you can use this block:

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

    To view this answer in context, here is a code snippet example:

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

    Snippet taken and adjusted from: https://github.com/jenkinsci/configuration-as-code-plugin/issues/876