2

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' {}
          }
        }
      }
Mark Han
  • 2,785
  • 2
  • 16
  • 31
Katie
  • 23
  • 5
  • Would you mind posting a screenshot of where you are putting this syntax? This is definitely possible -- if you hit the (?) and are configuring from the GUI it should give you more hints on the syntax. – Mark Han Sep 23 '19 at 16:57
  • Hi @MarkHan I added a code block to the original question. I did look under the (?) in the GUI but it looked like it was more for cron syntax rather than for CASC. Thanks! – Katie Sep 24 '19 at 12:32
  • Ahh I see, good call. I think I may have found what you're looking for. Check my answer. @Katie – Mark Han Sep 24 '19 at 18:48
  • @MarkHan I tried that, but it sets the "Poll SCM" trigger rather than the "Build Periodically" one. – Katie Sep 24 '19 at 19:41
  • Can you try `triggers { cron('0 6-19 * * *) }` ? @Katie – Mark Han Sep 25 '19 at 17:54
  • Also, it's possible that you may want to reserve CASC plugin for configuring Jenkins, and then for configuration of Jobs to use scripted / declarative pipelines within your Jenkinsfile. If you do, then see: https://stackoverflow.com/questions/44113834/trigger-hourly-build-from-scripted-jenkinsfile for Scripted Pipeline, or https://stackoverflow.com/questions/39168861/build-periodically-with-a-multi-branch-pipeline-in-jenkins for Declarative Pipeline. In your case, it may be beneficial if you posted the contents of the referenced Jenkinsfile. We may be able to achieve chron job within that. – Mark Han Sep 25 '19 at 18:01
  • I used 'triggers { cron('0 6-19 * * *) }' and that worked! Thank you!! @MarkHan and thanks, I'll look in configuring the jobs within my Jenkinsfile – Katie Sep 27 '19 at 14:13
  • Awesome! That's great to here. I edited my answer to make sure future viewers can also digest this info. – Mark Han Sep 30 '19 at 14:15

1 Answers1

3

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

Mark Han
  • 2,785
  • 2
  • 16
  • 31