Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djfdyuruiry/e3c891c6204bea602e770f9bf7a0cb1c to your computer and use it in GitHub Desktop.
Save djfdyuruiry/e3c891c6204bea602e770f9bf7a0cb1c to your computer and use it in GitHub Desktop.
Jenkins Job DSL for a Multi-Branch Pipeline that includes Branch Source Strategy & custom Jenkinsfile script path
// A new UUID must be generated for the first run and re-used for your Job DSL, the plugin updates jobs based on ID
UUID uuid = UUID.fromString("dd847135-8391-4f66-a54c-7f8781dc3119") // generate one @ https://www.uuidgenerator.net
multibranchPipelineJob("my_awesome_job") {
displayName "my awesome job"
description "multi-branch pipeline job thingy"
configure {
it / sources / 'data' / 'jenkins.branch.BranchSource' << {
source(class: 'jenkins.plugins.git.GitSCMSource') {
id(uuid)
remote("git@gitlab:root/repo.git")
credentialsId("ssh_key")
includes('*')
excludes('')
ignoreOnPushNotifications('false')
traits {
'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
}
}
// default strategy when sourcing from a branch
strategy(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy") {
defaultProperties(class: "java.util.Arrays\$ArrayList") {
a(class: "jenkins.branch.BranchProperty-array") {
// don't trigger builds
"jenkins.branch.NoTriggerBranchProperty"()
}
}
// exceptions to the default strategy
namedExceptions(class: "java.util.Arrays\$ArrayList") {
a(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy\$Named-array") {
"jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named"() {
// for the brach named `master` trigger builds
// (this is default behaviour if no branch properties are specified)
props(class: "empty-list")
name("master")
}
}
}
}
}
// customise the branch project factory
it / factory(class: "org.jenkinsci.plugins.workflow.multibranch.WorkflowBranchProjectFactory") << {
// pipeline jobs will have their script path set to `pipelines/customPipeline.groovy`
scriptPath("pipelines/customPipeline.groovy")
}
}
}
@mjungsbluth
Copy link

mjungsbluth commented Apr 9, 2018

Using id(random uuid) with a multi branch pipeline will lead to errors as soon as the job DSL script is re-executed. The multi branch pipeline won't be able to find the latest tip of the branch (at least that is the error message). Using a fixed id for the repository fixes that problem.

@djfdyuruiry
Copy link
Author

@mjungsbluth Good spot, corrected with comment!

@babatundebusari
Copy link

where is section for scriptPath to your Jenkinsfile?

@coolguy7779
Copy link

where is section for scriptPath to your Jenkinsfile?
I would like to know how to give Jenkinsfile option. Please help.

@froblesmartin
Copy link

where is section for scriptPath to your Jenkinsfile?
I would like to know how to give Jenkinsfile option. Please help.

@coolguy7779 have a look: https://stackoverflow.com/questions/48284589/jenkins-jobdsl-multibranchpipelinejob-change-script-path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment