Forked from stuartstevenson/jenkins-job-dsl.groovy
Created
December 21, 2015 12:09
-
-
Save aryak007/0006f0529071e99ef6db to your computer and use it in GitHub Desktop.
Jenkins job dsl with distributed job config files. The seed.groovy file scans stash repo branches for jenkins-job-dsl.groovy files and runs them centrally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Closure job = { | |
// Job Name | |
name "${stashProjectKey}-${projectName}-${branchSimpleName}" | |
// Where should jenkins run the job | |
label ('master') | |
// Where should Jenkins get the source code from | |
scm { | |
git { | |
remote { | |
url ("<STASH URL>/git/${stashProjectKey}/${projectName}.git") | |
branch (branchName) | |
credentials (jenkinsGitCredential) | |
} | |
} | |
} | |
// How often should the job run | |
triggers { | |
scm ('H/10 * * * *') | |
} | |
// Gradle build steps to execute | |
steps { | |
def gradleTask = '<GRADLE BUILD STEPS>' | |
gradle (gradleTask, null, true) { | |
def makeExecutable = it / 'makeExecutable' | |
makeExecutable.setValue (true) | |
} | |
} | |
// Additional Report Settings | |
publishers { | |
jacocoCodeCoverage { | |
minimumLineCoverage '60' | |
maximumLineCoverage '90' | |
execPattern '**/build/**/*.exec' | |
} | |
archiveJunit ("**/build/test-results/*.xml", true, true) | |
} | |
} | |
return job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
def jenkinsGitCredential = 'JENKINS GIT CREDNETIALS GO HERE' | |
def jenkinsJobDslFileName = 'jenkins-job-dsl.groovy' | |
def projectsApi = new URL("<STASH URL>/rest/api/1.0/projects") | |
def projects = new JsonSlurper().parse(projectsApi.newReader()) | |
projects.get("values").each { | |
def stashProjectKey = it.get("key") | |
if (stashProjectKey) { | |
println "INFO - Searching for repos in project $stashProjectKey" | |
def reposApi = new URL("<STASH URL>/rest/api/1.0/projects/${stashProjectKey}/repos") | |
def repos = new JsonSlurper().parse(reposApi.newReader()) | |
if (repos) { | |
def repoList = repos.get("values") | |
println "INFO - Found ${repoList.size} repos for project ${stashProjectKey}." | |
repoList.each { repo -> | |
processRepo(repo, stashProjectKey, jenkinsGitCredential, jenkinsJobDslFileName) | |
} | |
} | |
} | |
} | |
private void processRepo(repo, stashProjectKey, jenkinsGitCredential, jenkinsJobDslFileName) { | |
def repoSlug = repo.get("slug") | |
if (repoSlug) { | |
println "INFO - Searching for branches in repo $repoSlug" | |
def branchApi = new URL("<STASH URL>/rest/api/1.0/projects/${stashProjectKey}/repos/${repoSlug}/branches") | |
def branches = new JsonSlurper().parse(branchApi.newReader()) | |
branches.get("values").each { branch -> | |
processBranch(jenkinsGitCredential, branch, stashProjectKey, repoSlug, jenkinsJobDslFileName) | |
} | |
} | |
} | |
private void processBranch(jenkinsGitCredential, branch, stashProjectKey, repoSlug, jenkinsJobDslFileName) { | |
def branchName = branch.get("displayId") | |
def branchSimpleName = branchName.replace("/", "-") | |
def jobDslUrl = new URL("<STASH URL>/rest/api/1.0/projects/${stashProjectKey}/repos/${repoSlug}/browse/${jenkinsJobDslFileName}?raw&at=${branchName}") | |
println "INFO - Found branch ${repoSlug} - ${branchName}" | |
println "INFO - Looking for job dsl ${jobDslUrl}" | |
def urlConnection = (HttpURLConnection) jobDslUrl.openConnection() | |
def status = urlConnection.getResponseCode() | |
def repoAndBranchName = "${repoSlug} - ${branchName}" | |
if (status == 200) { | |
println "INFO - Found job dsl for " + repoAndBranchName | |
def dslJson = new JsonSlurper().parse(jobDslUrl.newReader()) | |
if (dslJson.get("lines") != null) { | |
try { | |
println "INFO - Executing job dsl for $repoAndBranchName" | |
Binding binding = getJobDslBinding( | |
stashProjectKey, | |
repoSlug, | |
branchSimpleName, | |
branchName, | |
jenkinsGitCredential) | |
executeJobDsl(binding, dslJson) | |
} catch (Exception e) { | |
println "ERROR - Couldn't run job dsl for ${repoAndBranchName}: ${e.getMessage()} \n ${e.printStackTrace()}" | |
} | |
} | |
} else { | |
println "WARN - No jenkins job dsl found for $repoAndBranchName" | |
} | |
} | |
private void executeJobDsl(Binding binding, dslJson) { | |
def jobDsl = []; | |
dslJson.get("lines").each { | |
jobDsl.add(it.text) | |
} | |
def jobDslString = jobDsl.join("\n") | |
GroovyShell shell = new GroovyShell(binding); | |
Object value = shell.parse(jobDslString); | |
job value.run() | |
} | |
private Binding getJobDslBinding(stashProjectKey, repoSlug, branchSimpleName, branchName, jenkinsGitCredential) { | |
Binding binding = new Binding(); | |
binding.setVariable('stashProjectKey', stashProjectKey) | |
binding.setVariable('projectName', repoSlug) | |
binding.setVariable('branchSimpleName', branchSimpleName) | |
binding.setVariable('branchName', branchName) | |
binding.setVariable('jenkinsGitCredential', jenkinsGitCredential) | |
binding | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment