<p>Sure, you can do this with Scripted Pipelines.  I've never tried to do it with Declarative so I'm not sure whether it's possible to do with Declarative or, if it is, if it's easy or straightforward.</p>

<p>Here's an example of what this might look like with a Scripted Pipeline.  I haven't tested it so there may be syntax errors or other problems.</p>

<pre><code>def parallelTopLevelSteps = [:]

parallelTopLevelSteps['A and B'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step A'] = { echo('A') }
  parallelNestedSteps['step B'] = { echo('B') }

  parallel(parallelNestedSteps)
}

parallelTopLevelSteps['C and D'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step C'] = { echo('C') }
  parallelNestedSteps['step D'] = { echo('D') }

  parallel(parallelNestedSteps)
}

parallel(parallelTopLevelSteps)
</code></pre>

CC BY-SA 4.0
2018-10-24T07:39:03.823Z
need to add stages and steps, but it is the right and elegant way to do it.
amichib
CC BY-SA 4.0
2018-10-23T22:25:53.533Z
2018-10-23T22:25:53.533Z
jayhendren
<p>Jenkins pipeline, declarative.</p>
<p>Simple question, how do I create a parallel stages inside a stage which is parallel by itself?</p>
<pre><code>pipeline {
agent { label 'master' }
options {
    timestamps()
    timeout(time: 15, unit: 'MINUTES')
    ansiColor('xterm')
}

stages {
        stage('Main parallel') {
            parallel {
                stage('Start containers') {
                agent { label 'my-agent' }
                    stages {
                        stage('Start Linux containers') {
                            steps {
                                script {
                                    dir (&quot;&quot;){
                                        println &quot;running&quot;
                                    }
                                }   
                            }
                        }
                        
                    }
                }
                stage('Build UI') {
                    stages {
                        stage('Cleanup main workspace'){
                            steps {
                                    dir(&quot;&quot;) {
                                        cleanWs()
                                    }
                            }
                        }
                        stage('Git checkout') {
                            steps {
                                dir(&quot;&quot;) {
                                        checkout()
                                }
                            }
                        }
                        
                        stages {
                            stage('Gradle build') {
                                parallel{
                                    stage('gradle1') {
                                        steps {
                                            dir(&quot;&quot;) {
                                                script {
                                                            buildInfo = rtGradle.run switches: gradleParams , tasks: 'clean deploy'
                                                            
                                                        }
                                                }
                                            }
                                        }
                                    }
                                    stage('gradle2') {
                                        steps {
                                            dir(&quot;&quot;) {
                                                script {
                                                        buildInfo2 = rtGradle2.run switches: gradleParams , tasks: 'unitTests'
                                                    
                                                    }
                                                }
                                            }   
                                        }
                                    }   
                            }
                        }
                }
            }
        }
    }
}
</code></pre>
<p>I am sure it is possible, but how?</p>

CC BY-SA 4.0
2020-05-25T17:29:53.543Z
Pipeline script told me: `"matrix" or "parallel" cannot be nested inside another "matrix" or "parallel"`
- so I'm guessing it's not possible.
Rick Moritz
<jenkins-pipeline>
Jenkins pipeline nested parallel
amichib
# Jenkins pipeline nested parallel

(Asked by: username_0 on 2018-10-23)

Jenkins pipeline, declarative.
Simple question, how do I create a parallel stages inside a stage which is parallel by itself?
pipeline {
agent { label 'master' }
options {
    timestamps()
    timeout(time: 15, unit: 'MINUTES')
    ansiColor('xterm')
}

stages {
        stage('Main parallel') {
            parallel {
                stage('Start containers') {
                agent { label 'my-agent' }
                    stages {
                        stage('Start Linux containers') {
                            steps {
                                script {
                                    dir (""){
                                        println "running"
                                    }
                                }   
                            }
                        }
                        
                    }
                }
                stage('Build UI') {
                    stages {
                        stage('Cleanup main workspace'){
                            steps {
                                    dir("") {
                                        cleanWs()
                                    }
                            }
                        }
                        stage('Git checkout') {
                            steps {
                                dir("") {
                                        checkout()
                                }
                            }
                        }
                        
                        stages {
                            stage('Gradle build') {
                                parallel{
                                    stage('gradle1') {
                                        steps {
                                            dir("") {
                                                script {
                                                            buildInfo = rtGradle.run switches: gradleParams , tasks: 'clean deploy'
                                                            
                                                        }
                                                }
                                            }
                                        }
                                    }
                                    stage('gradle2') {
                                        steps {
                                            dir("") {
                                                script {
                                                        buildInfo2 = rtGradle2.run switches: gradleParams , tasks: 'unitTests'
                                                    
                                                    }
                                                }
                                            }   
                                        }
                                    }   
                            }
                        }
                }
            }
        }
    }
}

I am sure it is possible, but how?

**Tags:** jenkins-pipeline

> **Comments:**
> **username_1**: Pipeline script told me: `"matrix" or "parallel" cannot be nested inside another "matrix" or "parallel"` - so I'm guessing it's not possible.  

---

## 1 answers

### Answer by username_2 on 2018-10-23. Score: 5 | Accepted answer

Sure, you can do this with Scripted Pipelines.  I've never tried to do it with Declarative so I'm not sure whether it's possible to do with Declarative or, if it is, if it's easy or straightforward.
Here's an example of what this might look like with a Scripted Pipeline.  I haven't tested it so there may be syntax errors or other problems.
def parallelTopLevelSteps = [:]

parallelTopLevelSteps['A and B'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step A'] = { echo('A') }
  parallelNestedSteps['step B'] = { echo('B') }

  parallel(parallelNestedSteps)
}

parallelTopLevelSteps['C and D'] = {
  def parallelNestedSteps = [:]

  parallelNestedSteps['step C'] = { echo('C') }
  parallelNestedSteps['step D'] = { echo('D') }

  parallel(parallelNestedSteps)
}

parallel(parallelTopLevelSteps)

> **Comments:**
> **username_0**: need to add stages and steps, but it is the right and elegant way to do it.  

---

