Skip to content

Instantly share code, notes, and snippets.

@legege
Forked from stuartstevenson/jenkins-job-dsl.groovy
Last active September 25, 2018 16:53
Show Gist options
  • Save legege/f557233667b98a50bd04 to your computer and use it in GitHub Desktop.
Save legege/f557233667b98a50bd04 to your computer and use it in GitHub Desktop.
Jenkins Job DSL with distributed job config files on GitLab. The seed.groovy file scans GitLab for all your projects for .jenkins-ci.groovy files and runs them centrally.
mavenJob("${name}") {
goals('clean install findbugs:findbugs pmd:pmd cobertura:cobertura')
logRotator {
numToKeep(5)
artifactNumToKeep(1)
}
scm {
git {
remote {
url ("${repoUrl}")
branch (branchName)
credentials ("jenkins")
}
}
}
triggers {
snapshotDependencies (true)
}
publishers {
mailer('', true, true)
findbugs('**/findbugsXml.xml', true)
pmd('**/*.pmd')
cobertura('**/target/site/cobertura/coverage.xml')
}
}
import groovy.json.JsonSlurper
def gitlab = 'https://gitlab.com'
def token = '<YOUR_TOKEN>'
def job_filename = 'jenkins-job.groovy'
def projects = []
def page = 1
while (true) {
println "Fetching projects (page ${page})..."
def p = new JsonSlurper().parse(new URL("${gitlab}/api/v3/projects/all?page=${page}&per_page=100&private_token=${token}").newReader())
if (p.size() > 0) {
projects = projects + p
page++
} else {
break
}
}
projects.each {
def id = it.id
def projectName = it.path_with_namespace
def repoUrl = it.ssh_url_to_repo
println "Checking project ${projectName}..."
def branches = new JsonSlurper().parse(new URL("${gitlab}/api/v3/projects/${id}/repository/branches?private_token=${token}").newReader())
branches.each {
def branchName = it.name
println "Checking branch ${branchName}..."
def fileRefUrl = new URL("${gitlab}/api/v3/projects/${id}/repository/files?private_token=${token}&file_path=${job_filename}&ref=${branchName}")
def urlConnection = (HttpURLConnection) fileRefUrl.openConnection()
def status = urlConnection.getResponseCode()
if (status == 200) {
def jenkinsJob = new JsonSlurper().parse(fileRefUrl.newReader())
def jobName = projectName.replaceAll('/','_') + "_" + branchName.replaceAll('/|@','-')
println "Found a job descriptor for ${projectName}:${branchName} - Creating job ${jobName}"
def blob_id = jenkinsJob.blob_id
def jobFile = new URL("${gitlab}/api/v3/projects/${id}/repository/raw_blobs/${blob_id}?private_token=${token}").getText()
def jobCode = "{->" + jobFile + "}"
def binding = new Binding();
binding.setVariable('name', "${jobName}")
binding.setVariable('branchName', "${branchName}")
binding.setVariable('repoUrl', "${repoUrl}")
def shell = new GroovyShell(binding);
def closure = shell.evaluate(jobCode)
closure.delegate = this
closure()
} else {
println "No job descriptor found for ${projectName}:${branchName}"
}
}
}
def groups = new JsonSlurper().parse(new URL("${gitlab}/api/v3/groups?private_token=${token}").newReader())
groups.each {
def group_name = it.name
def group_path = it.path
def group_description = it.description
println "Creating view ${group_name}"
categorizedJobsView(group_name) {
description(group_description)
jobs {
regex(group_path + "_.*")
}
categorizationCriteria {
regexGroupingRule(group_path + "_([^_]+).*")
}
columns {
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
lastBuildConsole()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment