Configuring a Jenkins Multibranch Pipeline to Use an External Script with Job DSL

I've recently started working with the Jenkins Job DSL plugin to manage all my Jenkins jobs' configuration as code.

For this move to Job DSL, I wanted to get it set up to allow me to configure a Multibranch pipeline for a given Git repository, but to use Jenkins pipeline configured elsewhere, so I could reduce some repeated code.

Aside: Job DSL is really awesome, and I'll blog about how to get using it at some point in the future.

So the problem that has prompted this blog post is that while it is possible to configure in the Jenkins UI, it doesn't seem to be possible through the bindings for Job DSL's multibranchPipelineJob, at least as of v1.76.

Fortunately, Job DSL has the configure block, which allows the tweaking of the generated XML, so although there aren't native bindings, we're able to set it ourselves:

multibranchPipelineJob('example') {
  branchSources {
    git {
      // ...
    }
  }
  configure {
    def factory = it / factory(class: 'com.cloudbees.workflow.multibranch.CustomBranchProjectFactory')
    factory << definition(class:'org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition') {
      scm(class: 'hudson.plugins.git.GitSCM') {
        userRemoteConfigs {
          'hudson.plugins.git.UserRemoteConfig' {
            url ( 'https://git.host/pipelines.git')
          }
        }
        branches {
          'hudson.plugins.git.BranchSpec' {
            name('master')
          }
        }
      }
      scriptPath('pipelines/maven.groovy')
    }
  }
}

When the job is then seeded, we get our Multibranch pipeline configured to run a script from an external repo - awesome!

Note that I was able to determine the above by configuring the changes manually in the Jenkins UI, and then viewing the job's config as XML to then reverse-engineer the above configure block.

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.

Related Posts

Other posts you may be interested in:

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.