Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-56586

Parallel stages or branches cannot be nested ("can only be included in a top-level stage")

      When I try to use nested parallel statements in a declarative pipeline as such:

       pipeline {
          agent none
          stages {
              stage('Cancel Previous Builds') { steps { echo "Cancel Previous Builds" } }
              stage('Pre-build') { steps { echo "Pre-build" } }
              stage("Build and Test") {
                  parallel {
                      stage('Build and Test') {
                          stages {
                              stage('Build Platform 1') { steps { echo "Build Platform 1" } }
                              stage('Test Platform 1') { 
                                  parallel {
                                      stage('Test 1') { steps { echo "Test 1" } }
                                      stage('Test 2') { steps { echo "Test 2" } }
                                      stage('Test 3') { steps { echo "Test 3" } }
                                  }
                              }
                          }
                      }
                      stage('Build Platform 2') { steps { echo "Build Platform 2" } }
                      stage('Build Platform 3') { steps { echo "Build Platform 3" } }
                  }
              }
          }
      }

      I get an error:

      WorkflowScript: 25: Parallel stages or branches can only be included in a top-level stage. @ line 25, column 33.
                                         stage('Test 1') { steps { echo "Test 1" } }
                                         ^
       

      A single level of parallel steps is really quite limiting.  I don't think my desired workflow is really all that much "out there".

      From researching this I seem to find a lot of questions about multiple levels of parallel steps but not many answers.

      Is this something that will be supported with declarative at some point?

      To be clear, I don't care about Blue Ocean rendering and visualisation of such nested parallel stages, certainly never any time before JENKINS-39203 since that bug makes Blue Ocean visualisation pretty useless (IMHO) anyway.

      But Blue Ocean aside, it would still be nice to have pipelines that could execute multiple levels of parallel stages.

          [JENKINS-56586] Parallel stages or branches cannot be nested ("can only be included in a top-level stage")

          This issue is affecting me too.  Even when using the example pipeline from the documentation, jenkins throws an error.

           

          https://jenkins.io/doc/book/pipeline/syntax/#sequential-stages

          John Scaglione added a comment - This issue is affecting me too.  Even when using the example pipeline from the documentation, jenkins throws an error.   https://jenkins.io/doc/book/pipeline/syntax/#sequential-stages

          Dan Jordan added a comment -

          Same issue, as soon as I attempt parallel stages at a lower level I get the error:

          WorkflowScript: 25: Parallel stages or branches can only be included in a top-level stage

          Dan Jordan added a comment - Same issue, as soon as I attempt parallel stages at a lower level I get the error: WorkflowScript: 25: Parallel stages or branches can only be included in a top-level stage

          Any progress on this? This is a real problem for us. 

          Guillaume Egles added a comment - Any progress on this? This is a real problem for us. 

          Liam Newman added a comment -

          Th error message could be better, but the lack of parallel in parallel is intentional at this time.  I haven't looked into the reasoning. 

          Liam Newman added a comment - Th error message could be better, but the lack of parallel in parallel is intentional at this time.  I haven't looked into the reasoning. 

          the lack of parallel in parallel is intentional at this time

          Indeed. But it's quite limiting, unfortunately.

          I wouldn't image a scenario of wanting to do multiple tests in parallel across multiple distros, in parallel of course, would be all that uncommon.

          Brian J Murrell added a comment - the lack of parallel in parallel is intentional at this time Indeed. But it's quite limiting, unfortunately. I wouldn't image a scenario of wanting to do multiple tests in parallel across multiple distros, in parallel of course, would be all that uncommon.

          Sergio Mira added a comment - - edited

          You can use a script and run the parallel in there as a workaround in declarative pipelines:

           pipeline {
              agent none
              stages {
                  stage('Cancel Previous Builds') { steps { echo "Cancel Previous Builds" } }
                  stage('Pre-build') { steps { echo "Pre-build" } }
                  stage("Build and Test") {
                      parallel {
                          stage('Build and Test') {
                              stages {
                                  stage('Build Platform 1') { steps { echo "Build Platform 1" } }
                                  stage('Test Platform 1') {
                                      script {
                                        parallel([
                                            'Test 1': { echo "Test 1" },
                                            'Test 2': { echo "Test 2" },
                                            'Test 3') { echo "Test 3" }
                                        ])
                                      }
                                  }
                              }
                          }
                          stage('Build Platform 2') { steps { echo "Build Platform 2" } }
                          stage('Build Platform 3') { steps { echo "Build Platform 3" } }
                      }
                  }
              }
          }

          I

          Sergio Mira added a comment - - edited You can use a script and run the parallel in there as a workaround in declarative pipelines: pipeline { agent none stages { stage('Cancel Previous Builds') { steps { echo "Cancel Previous Builds" } } stage('Pre-build') { steps { echo "Pre-build" } } stage("Build and Test") { parallel { stage('Build and Test') { stages { stage('Build Platform 1') { steps { echo "Build Platform 1" } } stage('Test Platform 1') { script { parallel([ 'Test 1': { echo "Test 1" }, 'Test 2': { echo "Test 2" }, 'Test 3') { echo "Test 3" } ]) } } } } stage('Build Platform 2') { steps { echo "Build Platform 2" } } stage('Build Platform 3') { steps { echo "Build Platform 3" } } } } } } I

          Dan Jordan added a comment -

          As someone that is trying to upgrade my complex project from the deprecated multi-job plugin, I find this issue to be quite relevant. That plugin's deprecation page states:

          > since most of Jenkins usage now is based on Jenkins Pipeline, and the 'parallel' step along with 'BlueOcean' can basically do whatever this plugin does, it is no longer in Tikal's radar. Also, we published the parallelPhase library class that can do in pipelines what the plugin does

          I notice no traffic on this in nearly 5 years - is it safe to assume we'll never get this feature in declarative pipelines? Can anyone help me understand what the "parallelPhase library class" means in that quote/link above? Does that correspond directly to the parallel() notation in the declarative pipelines? Or is there some plugin.

          Finally, is the script {}  workaround described by sergiomira still the best way to go if I want this capability? Thanks!

          Dan Jordan added a comment - As someone that is trying to upgrade my complex project from the deprecated multi-job plugin , I find this issue to be quite relevant. That plugin's deprecation page states: > since most of Jenkins usage now is based on Jenkins Pipeline, and the 'parallel' step along with 'BlueOcean' can basically do whatever this plugin does, it is no longer in Tikal's radar . Also, we published the parallelPhase library class that can do in pipelines what the plugin does I notice no traffic on this in nearly 5 years - is it safe to assume we'll never get this feature in declarative pipelines? Can anyone help me understand what the "parallelPhase library class" means in that quote/link above? Does that correspond directly to the parallel() notation in the declarative pipelines? Or is there some plugin. Finally, is the script { }  workaround described by sergiomira still the best way to go if I want this capability? Thanks!

          Mark Waite added a comment -

          ddj116 if you need parallel in parallel, you'll need to use a scripted Pipeline.

          Mark Waite added a comment - ddj116 if you need parallel in parallel, you'll need to use a scripted Pipeline.

            Unassigned Unassigned
            brianjmurrell Brian J Murrell
            Votes:
            24 Vote for this issue
            Watchers:
            29 Start watching this issue

              Created:
              Updated: