A custom step for Jenkins pipelines that allows scaling a task n times in parallel, specifying parameters for each thread
Last active
May 20, 2018 22:00
-
-
Save djfdyuruiry/f8357498aed61ee53c8482e6c1763694 to your computer and use it in GitHub Desktop.
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
// 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] }' | |
} | |
} |
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
// 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