Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
}
}
}
}
@timblaktu

This comment has been minimized.

Copy link

@timblaktu timblaktu commented Jan 30, 2020

Thorben, I wonder if you would be willing to expand on how this approach could be used to configure a multibranch pipeline to disable concurrent builds globally for all its branch jobs? (So you don't have to go into each branch Jenkinsfile and set disableConcurrentBuilds option).

@tknerr

This comment has been minimized.

Copy link
Owner Author

@tknerr tknerr commented Jan 30, 2020

@timblaktu honestly I haven't done that yet, but you could try with the rateLimitBranchProperty , e.g.:

multibranchPipelineJob("your-multibranch-pipeline") {
  branchSources {
    branchSource {
      source {
        ...
      }
      strategy {
        defaultBranchPropertyStrategy {
          props {
            rateLimitBranchProperty {
              count(1)
              durationName("year")
            }
          }
        }
      }
    }
  }
}

See also my comment here:
http://disq.us/p/270gwm6

Let me know if that works... :)

@tknerr

This comment has been minimized.

Copy link
Owner Author

@tknerr tknerr commented Jan 30, 2020

@timblaktu

This comment has been minimized.

Copy link

@timblaktu timblaktu commented Nov 20, 2020

Thanks Torben. Rate-limiting aside, I'm now considering using your jobdsl multibranch pipeline factory approach here to workaround Jenkins' limitation one Jenkinsfile per repo. Do you think this would work?

Basically, I'd use jobdsl like your above too iterate over a number of paths-to-Jenkinsfiles within a single repo, and create a multibranch pipeline job for each. While it seems that this would work for job creation, I'm concerned that all the SCM polling and event detection would not work properly since Jenkins doesn't support the notion of more than a single "Pipeline" per repo. Thoughts?

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