Created
October 6, 2017 10:00
-
-
Save tknerr/e7793bf8127c4f1bee56f6761220245a to your computer and use it in GitHub Desktop.
JobDSL example for setting up master / release branch builds + PR builds via bitbucket-branch-source-plugin (using the generated JobDSL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define the bitbucket project + repos we want to build | |
def bitbucket_project = 'awesome' | |
def bitbucket_repos = ['foo','bar','baz'] | |
// create a pipeline job for each of the repos and for each feature branch. | |
for (bitbucket_repo in bitbucket_repos) | |
{ | |
multibranchPipelineJob("${bitbucket_repo}-ci") { | |
// configure the branch / PR sources | |
branchSources { | |
branchSource { | |
source { | |
bitbucket { | |
credentialsId("f345a6b-b850-some-creds-5ffd45aedaf3") | |
repoOwner("${bitbucket_project.toUpperCase()}") | |
repository("${bitbucket_repo}") | |
serverUrl("https://bitbucket.yourcompany.com/") | |
traits { | |
// discover PRs | |
originPullRequestDiscoveryTrait { | |
strategyId(3) //build both the head and merge refs | |
} | |
// match only long-lived branches, and PRs instead of feature branches | |
headWildcardFilter { | |
includes("master release/* PR-*") | |
excludes("") | |
} | |
} | |
} | |
} | |
} | |
} | |
// discover Branches (workaround due to JENKINS-46202) | |
configure { | |
def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits | |
traits << 'com.cloudbees.jenkins.plugins.bitbucket.BranchDiscoveryTrait' { | |
strategyId(3) // detect all branches | |
} | |
} | |
// check every minute for scm changes as well as new / deleted branches | |
triggers { | |
periodic(1) | |
} | |
// don't keep build jobs for deleted branches | |
orphanedItemStrategy { | |
discardOldItems { | |
numToKeep(0) | |
} | |
} | |
// automatically queue the job after the initial creation | |
if (!jenkins.model.Jenkins.instance.getItemByFullName("${bitbucket_repo}-ci")) { | |
queue("${bitbucket_repo}-ci") | |
} | |
} | |
} |
One for future wayfarers. This is how its used at time of writing:
traits {
bitbucketBranchDiscovery{
strategyId(1)
}
bitbucketPullRequestDiscovery{
strategyId(2)
}
}
I couldn't find any good docs on the bitbucket branch-source plugin for its DSL, and had to go hoking in the source code. The Symbol annotation seems to be the key:
https://github.com/jenkinsci/bitbucket-branch-source-plugin/blob/master/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/OriginPullRequestDiscoveryTrait.java#LL132C14-L132C43
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not work though for my version of Jenkins (~2.62):
Instead, I've added one more trait for OriginPullRequestDiscovery: