Building a Multibranch Pipeline on a Schedule

Featured image for sharing metadata for article

There are cases you want to rebuild your Multibranch projects on a schedule, for instance if there are regular updates to the pipeline, but not to the project itself, so you want to make sure that the projects still work, or to ensure that you're constantly rebuilding and running compliance checks against the latest version of the code, especially if there's no active development.

I'd looked at the options, and although there are solutions by putting it into the Multibranch's Jenkinsfile, I found that this wasn't the best solution, because the job's configuration only becomes active if that branch has been built, and can look a bit awkward.

I found a more elegant solution was to create a separate job that runs on a scheduled and triggers the branches we want, which allows us to control this more easily, as well as easily disabling any scheduling across the board.

Let's say that we want to build the main branch on our repo, and the develop branch if one exists, and that the Multibranch pipeline is located at Pipelines/Multibranch.

If we were using Job DSL, we would use the following definition for the schedule job:

factory.pipelineJob('Pipelines/_Schedule_Multibranch') {
  definition {
    cpsScm {
      triggers {
        cron('@weekly')
      }
      scm {
        git {
          remote {
            url '...'
          }
        }
        scriptPath 'jobs/scheduled.Jenkinsfile'
      }
    }
  }
}

Then jobs/scheduled.Jenkinsfile has the definition:

build job: 'Multibranch/main', wait: false
try {
  build job: 'Multibranch/develop', wait: false
} catch (Exception e) {
  // allow failure, as it could not exist
}

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #jenkins #job-dsl.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.