Issue
We have a fairly sizeable Jenkins pipeline that we are trying to par down using dynamic stages. Unfortunately the stages we're replacing use the options {}
construct for the stage which doesn't appear to be valid in this dynamic model. Here's an example that if you remove the options it will run fine:
def list = ["Stage-1","Stage-2","Stage-3","Stage-4","Stage-5","Stage-6","Stage-7","Stage-8","Stage-9","Stage-10","Stage-11","Stage-12","Stage-13","Stage-14","Stage-15"];
pipeline {
agent any
stages {
stage('Dynamic Stages') {
steps {
script {
parallel dynamicStages(list)
}
}
}
}
}
def dynamicStages(stageList) {
def stages = [:];
for(int i=0; i < stageList.size(); i++) {
def sname = stageList[i];
stages[sname] = {
option {
timeout(time: 1, unit: 'MINUTES')
}
stage(sname) {
echo "Element: $sname";
sh 'pwd; sleep 5; echo "Hello World"'
}
}
}
return stages;
}
Is there a way to use options for dynamic stages, and if not can the timeout be implemented a different way?
Solution
def dynamicStages(stageList) {
def stages = [:].asSynchronized() // don't ask
for (def sname : stageList) {
def final_name = sname // don't ask
stages[final_name] = {
stage(final_name) {
node("some_node") { // what agent should be running this?
timeout(time: 1, unit: 'MINUTES') {
echo "Element: ${final_name}"
sh 'pwd; sleep 5; echo "Hello World"'
}
}
}
}
}
return stages
}
Answered By - MaratC
Answer Checked By - Willingham (JavaFixing Volunteer)