Skip to content

Instantly share code, notes, and snippets.

@tknerr
Created January 18, 2018 10:21
Show Gist options
  • Save tknerr/c79a514db4bdbfb4956aaf0ee53836c8 to your computer and use it in GitHub Desktop.
Save tknerr/c79a514db4bdbfb4956aaf0ee53836c8 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)
}
}
}
}
@therewillbeblood
Copy link

disableConcurrentBuilds

@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... :)

I tried this approach but it does not work for me. It indeed will block build number n+1 even after build number n is finished.
What disableConcurrentBuilds does is that only one build at a time is running.
I was unsuccessful in trying to use it properly in job DSL 1.77. Any hint appreciated

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