Skip to content

Instantly share code, notes, and snippets.

@atward
Created July 11, 2018 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atward/ca414f25627ceddc62de77532e8270f2 to your computer and use it in GitHub Desktop.
Save atward/ca414f25627ceddc62de77532e8270f2 to your computer and use it in GitHub Desktop.
Jenkins scan config & generate stages
def jobs = ["JobA", "JobB", "JobC"]
def parallelStagesMap = jobs.collectEntries {
["${it}" : generateStage(it)]
}
def generateStage(job) {
return {
stage("stage: ${job}") {
echo "This is ${job}."
sh script: "sleep 15"
}
}
}
pipeline {
agent any
stages {
stage('non-parallel stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
def jq_script_get_config_files = """.provisioners[] |
if .type == "file" then .source
elif .type == "shell" then .script
else null
end
| strings"""
// get list of files used by each config
used_files_per_config = [:]
def configs = findFiles(glob: 'config/*.json')
configs.each {
def pk_config = "${it}"
def jq_get_used = ["jq", "-r", jq_script_get_config_files, pk_config].execute()
jq_get_used.waitFor()
def used_files = [ pk_config, *jq_get_used.in.text.split("\n") ]
//println(used_files)
used_files_by_config[pk_config] = used_files
}
// get list of files changed
def cmd = "git diff --name-only ${LAST_KNOWN_GOOD_COMMIT} ${GIT_COMMIT}".execute()
cmd.waitFor()
def git_changed_files = cmd.in.text.split("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment