Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djfdyuruiry/f8357498aed61ee53c8482e6c1763694 to your computer and use it in GitHub Desktop.
Save djfdyuruiry/f8357498aed61ee53c8482e6c1763694 to your computer and use it in GitHub Desktop.

A custom step for Jenkins pipelines that allows scaling a task n times in parallel, specifying parameters for each thread

// vars/customStep.groovy - see https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps
/*
This step allows executing a custom step with trace header and footer.
*/
def call(String name, Closure step) {
echo "[Pipeline] $name" + "\n" + '[Pipeline] {'
try {
step()
} finally {
echo "[Pipeline] // $name\n" + '[Pipeline] }'
}
}
// vars/dynamicParallel.groovy - see https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps
/*
This step allows calling a stage multiple times in parallel with different
parameters for each invocation.
Example:
dynamicParallel(name: "Echo Stuff", paramsForStages: [
[number: 5, direction: 'north'],
[number: 6, direction: 'south'],
[number: 2, direction: 'west']
]) { stageParams ->
echo "number: ${stageParams.number} | direction: ${stageParams.direction}"
}
*/
def call(Map params, Closure stage) {
customStep('dynamicParallel') {
def stages = [:]
for (def _stageId = 0; _stageId < params.paramsForStages.size(); _stageId++) {
def stageId = _stageId + 1
def stageName = "${params.name}-$stageId"
def stageParams = params.paramsForStages[_stageId]
stages[stageName] = {
script {
stage(stageParams, stageId, stageName)
}
}
}
parallel stages
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment