Skip to content

Instantly share code, notes, and snippets.

@pariekshit
Forked from tknerr/ci_jobs.groovy
Created October 13, 2018 01:17
Show Gist options
  • Save pariekshit/6fdeb45b8a623df538bfdcf6fd9c69cb to your computer and use it in GitHub Desktop.
Save pariekshit/6fdeb45b8a623df538bfdcf6fd9c69cb 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)
// 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")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment