Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jenkins Seed job for Gitlab projects
// jenkins job dsl plugin is required!
// This job will be run through the gitlab projects (except user owned ones) and generate jenkins jobs with maven goal. if there should be no job generated, then the label 'nojob' must be set in repository config.
// reads projects from gitlab and creates jobs
def gitUrl = '$URL'
def privateToken = '$TOKEN'
def allowedNamespaces = ['product', 'innovation']
def reposToIgnoreTag = 'nojob'
def jobNames = []
// Reading projects from GitLab REST API
def projectList = new URL("${gitUrl}/api/v4/projects?page=1&per_page=500&private_token=${privateToken}")
def projects = new groovy.json.JsonSlurper().parse(projectList.newReader())
projects.each {
try {
def id = it.id
def projectName = it.name
def projectNameWithNamespace = it.path_with_namespace
def repoUrl = it.http_url_to_repo
def description = it.description
//only create job if project is allowed
if (allowedNamespaces.contains(projectNameWithNamespace.split('/')[0])) {
if (!it.tag_list.contains(reposToIgnoreTag)) {
println "Creating job for project: ${projectName}"
job(projectName) {
//description(description)
wrappers {
configFiles {
globalMavenSettings('0cd0fe22-09da-4d9e-bfcf-45cdbcbf5ba5') {
targetLocation('settings.xml')
variable('GLOBAL_CONFIG_FILE')
}
}
}
scm {
git {
remote {
url(repoUrl)
branch('master')
credentials('dbcd9493-5363-442d-b55d-1d1ac96734d8')
}
}
}
triggers {
gitlabPush {
buildOnPushEvents(true)
buildOnMergeRequestEvents(true)
}
}
steps {
maven('-e -B -s $GLOBAL_CONFIG_FILE clean deploy')
// dsl {
//external('jobs/*.groovy')
//}
}
jobNames.add(projectName)
}
}
}
} catch (Exception ex) {
println("job creation failed for project: ${projectName}")
}
}
listView('JOBVIEW') {
jobs {
jobNames.each {
name(it)
}
}
columns {
status()
weather()
name()
lastSuccess()
lastFailure()
lastDuration()
buildButton()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment