Skip to content

Instantly share code, notes, and snippets.

@tknerr
Created January 18, 2018 10:21
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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)
}
}
}
}
@tknerr
Copy link
Author

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
Copy link
Author

tknerr commented Jan 30, 2020

@timblaktu
Copy link

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?

@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