Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-53991

cron job trigger for multibranchPipelineJob DSL

      Hi,

       

      I'm using multiBranchPipeline DSL to create a jobs like that:

      multibranchPipelineJob('${jobName}') {
          branchSources {
              git {
                  remote('${repo}')
                  credentialsId('${credentialsId}')
                  includes('${branches}')
              }
          }
          orphanedItemStrategy {
              discardOldItems {
                  numToKeep(20)
              }
          }
          configure {
              it / factory(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory') {
                  owner(class: 'org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject', reference: '../..')
                  scriptPath('${jenkinsfilePath}')
              }
          }
      }

      Everything works fine, but now I want to trigger job automatically by cron.

      According to docs I can use https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob-triggers-cron

      But it doesnt work for me.  I'm adding this to DSL snippet:

      triggers {    cron('@daily')}

      But Build Triggers section for the job is completely virgin.

      Am I missing something there? How can I debug this problem?

       

      UPDATE
      In job's config.xml I found this section:

      <triggers>
          <hudson.triggers.TimerTrigger>
              <spec>@daily</spec>
          </hudson.triggers.TimerTrigger>
      </triggers>

          [JENKINS-53991] cron job trigger for multibranchPipelineJob DSL

          Loren Alatan added a comment -

          Do we have any solutions on this issue?

          Loren Alatan added a comment - Do we have any solutions on this issue?

          Tim Jacomb added a comment -

          Tim Jacomb added a comment - cc jamietanna

          Jamie Tanna added a comment -

          The solution I came up with to do this is to have an upstream job that triggers it:

          // Apache 2.0 licensed
          factory.pipelineJob('Pipelines/_Schedule_Multibranch') {
            definition {
              cpsScm {
                triggers {
                  cron('@weekly')
                }
                scm {
                  git {
                    remote {
                      url '...'
                    }
                  }
                  scriptPath 'jobs/scheduled.Jenkinsfile'
                }
              }
            }
          }
          
          // Apache 2.0 licensed
          build job: 'Multibranch/main', wait: false
          try {
            build job: 'Multibranch/develop', wait: false
          } catch (Exception e) {
            // allow failure, as it could not exist
          }
          

          (See https://www.jvt.me/posts/2021/04/26/jenkins-schedule-multibranch/ for more details)

          The reason for this is that because it's a multibranch, the presumption is that you won't want to trigger each and every branch?

          Jamie Tanna added a comment - The solution I came up with to do this is to have an upstream job that triggers it: // Apache 2.0 licensed factory.pipelineJob( 'Pipelines/_Schedule_Multibranch' ) { definition { cpsScm { triggers { cron( '@weekly' ) } scm { git { remote { url '...' } } scriptPath 'jobs/scheduled.Jenkinsfile' } } } } // Apache 2.0 licensed build job: 'Multibranch/main' , wait: false try { build job: 'Multibranch/develop' , wait: false } catch (Exception e) { // allow failure, as it could not exist } (See https://www.jvt.me/posts/2021/04/26/jenkins-schedule-multibranch/ for more details) The reason for this is that because it's a multibranch, the presumption is that you won't want to trigger each and every branch ?

            Unassigned Unassigned
            spugachev Stanislav Pugachov
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: