Home > Enterprise >  How to achive nested parallel in Jenkins declarative pipeline?
How to achive nested parallel in Jenkins declarative pipeline?

Time:07-16

I have declarative pipeline as below and it runs 2* and 3* stages in parallel, pasted the blue ocean diagram below.

pipeline {
    agent { label 'my_node' }
    options {
        timestamps()
        parallelsAlwaysFailFast()
    }
    stages {
        stage('1') {
            steps {
                script {
                    step([$class: 'WsCleanup'])
                }
            }
        }
        stage('2') {
            parallel {
                stage("2.1") {
                    steps {
                        script {
                            sh 'echo hi 2.1'
                        }
                    }
                }
                stage("2p") {
                    steps {
                        script {
                            sh 'echo hi 2p'
                        }
                    }
                }
            }
        }
        stage('3') {
            parallel {
                stage('3.1') {
                    steps {
                        script {
                            sh """
                               echo hi 3.1
                            """
                        }
                    }
                }
                stage('3.2') {
                    steps {
                        script {
                            sh """
                                echo "hi 3.2"
                            """
                        }
                    }
                }
            }
        }
        stage('4') {
            steps {
                script {
                    sh "echo end"
                }
            }
        }
    }
}

enter image description here

But I am looking to run 2p in parallel to 2* and 3*, like shown below, is there a way? enter image description here

I tried to use paralle under parallel, to start 2p in parallel to 2 and 3, and nested parallel to run 3.1. and 3.2 underneath, but declarative pipeline is not allowing nested parallel.

CodePudding user response:

You can't do this only with Declarative syntax. But you can achieve this with a combination of Scripted and Declarative syntax. One thing to note is, AFAIK there is no visualization support for nested parallel stages as of now. There is a feature request for this here.

Following is a sample pipeline you can use as a reference for your use case.

pipeline {
    agent any
    options {
        timestamps()
        parallelsAlwaysFailFast()
    }
    stages {
        stage('1') {
            steps {
                script {
                    step([$class: 'WsCleanup'])
                }
            }
        }
        stage('2 AND 3') {
        steps {
            script {
                parallel getWrappedStages()
            }
        }
        }
        stage('4') {
            steps {
                script {
                    sh "echo end"
                }
            }
        }
    }
}

def getWrappedStages() {
    stages = [:]
    stages["Step2.1"] = { stage('2.1') {
        sh """
           echo hi 2.1
        """
        } 
        parallel parallel3xstages()
    }
    stages["Step2.p"] = { stage('2.p') {
        sh """
           echo hi 2.p
        """
        } 
    }
    
    return stages
}

def parallel3xstages() {
  stages = [:]
    stages["Step3.1"] = { stage('3.1') {
        sh """
           echo hi 3.1
        """
        }
    }

    stages["Step3.2"] = { stage('3.2') {
        sh """
           echo hi 3.2
        """
        }
    }
  return stages
}
  • Related