1
stages {
    stage('Setup') {
    }
    stage('Parallel Stage') {
        parallel {
            stage('Executor 1') {
            }
            stage('Executor 2') {
            } 
            stage('Executor 3') {
            } 
            stage('Executor 4') {
            }                                          
        }
    }
}

Above is a skeleton of my Jenkins pipeline that has a setup stage and then a parallel stage that does the same thing four times for faster execution time.

Is there a way to define a stage as a variable to reduce the 4x code repetition and to reduce the number of edits I would have to make?

np2807
  • 1,050
  • 15
  • 30
atg
  • 127
  • 2
  • 13

1 Answers1

4

Yes, best way is to defined a function which generates stage and can be called in parallel.

Presuming that you are executing the stages into 1 agent in parallel.

In below sample pipeline generateStage is a function which replaces nested stages with function.

def jobs = ["Executor1", "Executor2", "Executor3"]
 
def parallelStagesMap = jobs.collectEntries {
    ["${it}" : generateStage(it)]
}
 
def generateStage(job) {
    return {
        stage("${job}") {
                echo "Running stage ${job}."
        }
    }
}
 
pipeline {
    agent any
 
    stages {
        stage('setup') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
 
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }
    }
}

Output of the pipeline is as below:

enter image description here

For more details please see my answer LINK

Only drawback is that you can not execute this pipeline arrangement directly after stages thats why parallelStageMap is called inside the script.

np2807
  • 1,050
  • 15
  • 30
  • When place the three variables after, Jenkins complains about paralleStagesMap not being defined. When placing the three variables above pipeline as seen in your example, I get `WorkflowScript: 64: Expected a stage @ line 64, column 13. stage('Setup') { ^ WorkflowScript: 84: Expected a stage @ line 84, column 13. stage('Parallel Stage') { ^ WorkflowScript: 63: No stages specified @ line 63, column 9. stages – atg Apr 15 '21 at 22:14
  • 1
    @atg I have updated my snippet and output of pipeline. – np2807 Apr 16 '21 at 07:06
  • thank you! i'll try running it and I'll let you know how it goes. I feel like this is really close. it's probably an issue with the actual commands I'm trying to run but I'll see. – atg Apr 16 '21 at 17:53
  • Thank you @np2807 I managed to get it working. starting over and gradually adding steps helped. – atg Apr 27 '21 at 21:36