Answers: <p>The Scripted version of <code>parallel</code> and the Declarative version of <code>parallel</code> are different functions.  The Scripted version takes a Map as an argument, whereas the Declarative version expects a block with calls to the <code>stage</code> function within that block.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-10-21T13:31:00.167Z
Answers: After 2 days of struggling against it i was starting to assume this was the case... Is this documented anywhere?
Answers: iwantmyhatback
Answers: CC BY-SA 4.0
Answers: 2022-10-20T21:30:07.403Z
Answers: 2022-10-20T21:30:07.403Z
Answers: jayhendren
Body: <p>I am trying to get a Jenkins (2.204.2) declarative pipeline to run <strong>parallel stages generated into a map</strong> on different machines. I am aware it can be done by mixing in the <code>script</code> block and I have done it that way in the past, but from documentation and other Stack____ questions I cannot figure out why this format does not work.</p>
  <p>I have stripped everything down as far as possible and I am trying to just create the map statically outside the pipeline to figure out the syntax I need.</p>
  <pre><code>#!/usr/bin/env groovy
  
  def map = [:]
  map['Passed Stage'] = {
      stage(&quot;Passed Map Inner Stage&quot;){
          agent{ label &quot;nodeLabel&quot; }
          steps{
              echo &quot;PASSED MAP STEP&quot;
          }
      }   
  }
  
  pipeline {
      agent { label &quot;nodeLabel&quot; }
      stages {
          stage(&quot;Non map stage&quot;){
              agent{ label &quot;nodeLabel&quot; }
              steps{
                  echo &quot;NON MAP STEP&quot;
              }
          }
          stage(&quot;Direct Map Outer Parallel Stage&quot;){
              parallel{ direct : 
                  stage(&quot;Direct Map Inner Stage&quot;){
                      agent{ label &quot;nodeLabel&quot; }
                      steps{
                          echo &quot;DIRECT MAP STEP&quot;
                      }
                  }
              }
          }
          stage(&quot;Passed Map Outer Parallel Stage&quot;){
              parallel{ map }
          }
      }
  }
  </code></pre>
  <p>The first two stage methods work if I comment out the mapped one but &quot;Passed Map Outer Parallel Stage&quot; always fails with:</p>
  <pre><code>Running in Durability level: MAX_SURVIVABILITY
  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  WorkflowScript: 33: Expected a stage @ line 33, column 23.
                 parallel{ map }
                           ^
  
  WorkflowScript: 33: No stages specified @ line 33, column 13.
                 parallel{ map }
                 ^
  
  WorkflowScript: 33: No stages specified @ line 33, column 13.
                 parallel{ map }
                 ^
  
  3 errors
  
      at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
      at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
      at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
      at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
      at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
      at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
      at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
      at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
      at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
      at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
      at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:327)
      at hudson.model.ResourceController.execute(ResourceController.java:97)
      at hudson.model.Executor.run(Executor.java:427)
  Finished: FAILURE
  </code></pre>
  <p>The stage format seems fine from everything I've spent reading all day, and passing the same stage directly to <code>parallel</code> outside the map works fine.</p>
  <p>What am I missing here? why won't <code>parallel</code> accept my map? Does declarative <code>parallel</code> only accept stages statically passed? Is my Jenkins version too low?</p>
  
Tags: <jenkins><jenkins-pipeline><pipeline>
Title: Stage Parallelization in Jenkins declarative pipelines
UserDisplayName: iwantmyhatback
ThreadText: # Stage Parallelization in Jenkins declarative pipelines
  
  (Asked by: username_0 on 2022-10-20)
  
  I am trying to get a Jenkins (2.204.2) declarative pipeline to run **parallel stages generated into a map** on different machines. I am aware it can be done by mixing in the `script` block and I have done it that way in the past, but from documentation and other Stack\_\_\_\_ questions I cannot figure out why this format does not work.
  
  I have stripped everything down as far as possible and I am trying to just create the map statically outside the pipeline to figure out the syntax I need.
  
  ```
  #!/usr/bin/env groovy
  
  def map = [:]
  map['Passed Stage'] = {
      stage("Passed Map Inner Stage"){
          agent{ label "nodeLabel" }
          steps{
              echo "PASSED MAP STEP"
          }
      }   
  }
  
  pipeline {
      agent { label "nodeLabel" }
      stages {
          stage("Non map stage"){
              agent{ label "nodeLabel" }
              steps{
                  echo "NON MAP STEP"
              }
          }
          stage("Direct Map Outer Parallel Stage"){
              parallel{ direct : 
                  stage("Direct Map Inner Stage"){
                      agent{ label "nodeLabel" }
                      steps{
                          echo "DIRECT MAP STEP"
                      }
                  }
              }
          }
          stage("Passed Map Outer Parallel Stage"){
              parallel{ map }
          }
      }
  }
  
  ```
  
  The first two stage methods work if I comment out the mapped one but "Passed Map Outer Parallel Stage" always fails with:
  
  ```
  Running in Durability level: MAX_SURVIVABILITY
  org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
  WorkflowScript: 33: Expected a stage @ line 33, column 23.
                 parallel{ map }
                           ^
  
  WorkflowScript: 33: No stages specified @ line 33, column 13.
                 parallel{ map }
                 ^
  
  WorkflowScript: 33: No stages specified @ line 33, column 13.
                 parallel{ map }
                 ^
  
  3 errors
  
      at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
      at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
      at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
      at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
      at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
      at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
      at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
      at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
      at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
      at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
      at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
      at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:561)
      at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:522)
      at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:327)
      at hudson.model.ResourceController.execute(ResourceController.java:97)
      at hudson.model.Executor.run(Executor.java:427)
  Finished: FAILURE
  
  ```
  
  The stage format seems fine from everything I've spent reading all day, and passing the same stage directly to `parallel` outside the map works fine.
  
  What am I missing here? why won't `parallel` accept my map? Does declarative `parallel` only accept stages statically passed? Is my Jenkins version too low?
  
  **Tags:** jenkins, jenkins-pipeline, pipeline
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-10-20. Score: 0
  
  The Scripted version of `parallel` and the Declarative version of `parallel` are different functions. The Scripted version takes a Map as an argument, whereas the Declarative version expects a block with calls to the `stage` function within that block.
  
  > **Comments:**
  > **username_0**: After 2 days of struggling against it i was starting to assume this was the case... Is this documented anywhere?  
  
  ---
  
  