Skip to content

Instantly share code, notes, and snippets.

@alipandidan
Forked from tknerr/ci_jobs.groovy
Created May 16, 2020 20:47
Show Gist options
  • Save alipandidan/bf3f3aeec63a4a5989fbfbbedac8fc43 to your computer and use it in GitHub Desktop.
Save alipandidan/bf3f3aeec63a4a5989fbfbbedac8fc43 to your computer and use it in GitHub Desktop.
Example JobDSL for a multibranchPipelineJob which keeps only the last 10 builds
// define the bitbucket project + repos we want to build
def bitbucket_project = 'myproj'
def bitbucket_repos = ['myrepo1', 'myrepo2']
// 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("top-secret-1234-some-guid")
repoOwner("${bitbucket_project.toUpperCase()}")
repository("${bitbucket_repo}")
serverUrl("https://bitbucket.acme.com/")
traits {
headWildcardFilter {
includes("master release/* feature/* bugfix/*")
excludes("")
}
}
}
}
strategy {
defaultBranchPropertyStrategy {
props {
// keep only the last 10 builds
buildRetentionBranchProperty {
buildDiscarder {
logRotator {
daysToKeepStr("-1")
numToKeepStr("10")
artifactDaysToKeepStr("-1")
artifactNumToKeepStr("-1")
}
}
}
}
}
}
}
}
// 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(-1)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment