Jenkins pipeline with parallel

You need to upgrade the Declarative Pipeline plugin on your Jenkins to Version 1.2 (Sept 21, 2017) or above


In declarative pipeline, in case if you want to add stage, inside steps, this nesting is also possible.

If the steps are same in that case you can declare step as a function.

def steps  = ['first', 'second'] 
def generateSteps(stepLabel) {
    return {
        step("${stepLabel}") {
                echo "Running on ${stepLabel}"
        }
    }
}
def parallelStepMap = steps.collectEntries {
    ["${it}" : generateSteps(it)]
}
pipeline {
    agent any
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
 
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStepMap
                }
            }
        }       
    }
}

General need is to execute different stages into different nodes. in such case above function can be modified to execute on different agent nodes as follows.

def agents  = ['master', 'agent1', 'agent2']
 
 
def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
                echo "Running on ${nodeLabel}"
            }
        }
    }
}
def parallelStagesMap = agents.collectEntries {
    ["${it}" : generateStage(it)]
}
pipeline {
    agent none
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
 
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }       
    }
}

With nodelabel defined inside the function and agent none during start of pipeline, the stages will be executed into different nodes.


You need to add a steps block after your stage declaration.

    pipeline {
    agent any
    stages {
        stage('Example Stage 1') {
            steps {
                parallel(
                        "step 1": { echo "hello" },
                        "step 2": { echo "world" },
                        "step 3": { echo "world" }
                )
            }
        }
        stage('Example Stage 2') {
            steps {
                parallel(
                        "step 1": { echo "hello" },
                        "step 2": { echo "world" },
                        "step 3": { echo "world" }
                )
            }
        }
    }
}

Blue Ocean Parallel Steps

To Make your Stages Parallel use this, both solutions show up very similar in Blue Ocean :

    pipeline {
    agent any
    stages {
        stage('Example Stage') {
            parallel {
                stage('Stage 1') {
                    steps { sh 'echo stage 1 passed' }
                }
                stage('Stage 2') {
                    steps { sh 'echo stage 2 passed' }
                }
                stage('Stage 3') {
                    steps { sh 'echo stage 3 passed' }
                }
            }
        }
    }
}

Blue Ocean Parallel Stages