Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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