title: How to create dynamic jenkins pipeline with separate container for every stages?
body: <p>I have static working jenkins pipelines:</p>
  <pre><code>pipeline {
    agent none
    options {
      buildDiscarder(logRotator(daysToKeepStr: '7'))
      timeout(time: 90, unit: 'MINUTES')
    }
    stages {
      stage ('1. Prepare') {
        agent {
          kubernetes {
            yaml agentPod()
            defaultContainer 'agent'
          }
        }
        steps {
          script {
            echo &quot;Prepare pipeline&quot;
          }
        }
      }
      stage ('2. Check') {
        parallel {
          stage('Runs on master') {
            agent {
              kubernetes {
                yaml agentPod(&quot;500m&quot;, &quot;500Mi&quot;, &quot;100m&quot;, &quot;200Mi&quot;)
                defaultContainer 'agent'
              }
            }
            steps {
              echo &quot;Running on master&quot;
            }
          }
          stage('Runs on agent1') {
            agent {
              kubernetes {
                yaml agentPod(&quot;500m&quot;, &quot;500Mi&quot;, &quot;100m&quot;, &quot;200Mi&quot;)
                defaultContainer 'agent'
              }
            }
            steps {
              echo &quot;Running on agent1&quot;
            }
          }
          stage('Runs on agent2') {
            agent {
              kubernetes {
                yaml agentPod(&quot;500m&quot;, &quot;500Mi&quot;, &quot;100m&quot;, &quot;200Mi&quot;)
                defaultContainer 'agent'
              }
            }
            steps {
              echo &quot;Running on agent2&quot;
            }
          }
        }
      }
    }
  }
  </code></pre>
  <p>but I need to create the same pipeline but use a dynamic function. I try to use several ideas, but every time it doesn't work. Very important is that for every stage I need create a separate kubernetes pod (with kaniko image to build new image). I try to use something like this but it desn't work.</p>
  <pre><code>def generateStage(podLabel) {
    return {
      agent {
        kubernetes {
          yaml agentPod(&quot;500m&quot;, &quot;500Mi&quot;, &quot;100m&quot;, &quot;200Mi&quot;)
          defaultContainer 'agent'
        }
      }
      steps {
        echo &quot;Running on ${podLabel}&quot;
      }
    }
  }
  
  def parallelStagesMap = [:]
  
  pipeline {
    agent none
    options {
      buildDiscarder(logRotator(daysToKeepStr: '7'))
      timeout(time: 90, unit: 'MINUTES')
    }
    stages {
      stage ('1. Prepare') {
        agent {
          kubernetes {
            yaml agentPod()
            defaultContainer 'agent'
          }
        }
        steps {
          script {
            def agents = ['master', 'agent1', 'agent2']
            parallelStagesMap = agents.collectEntries {
              [&quot;Runs on ${it}&quot; : generateStage(it)]
            }
            echo &quot;Prepare pipeline&quot;
          }
        }
      }
      stage ('2. Check') {
        parallel parallelStagesMap 
      }
    }
  }
  </code></pre>
  <p>because after I execute this code I see this error:</p>
  <pre><code>org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
  
  /var/jenkins_home/jobs/app/jobs/pipeline/branches/PR-14476/builds/24/libs/e647be597f45b6129772d69874a82199dfce9ad821d01ce80c7a153b2c310c04/vars/dashboardPipelineTemplate.groovy: 46: Expected a block for parallel @ line 46, column 7.
           parallel parallelStagesMap 
           ^
  
  /var/jenkins_home/jobs/app/jobs/pipeline/branches/PR-14476/builds/24/libs/e647be597f45b6129772d69874a82199dfce9ad821d01ce80c7a153b2c310c04/vars/dashboardPipelineTemplate.groovy: 46: No stages specified @ line 46, column 7.
           parallel parallelStagesMap 
           ^
  
  /var/jenkins_home/jobs/app/jobs/pipeline/branches/PR-14476/builds/24/libs/e647be597f45b6129772d69874a82199dfce9ad821d01ce80c7a153b2c310c04/vars/dashboardPipelineTemplate.groovy: 46: No stages specified @ line 46, column 7.
           parallel parallelStagesMap 
           ^
  </code></pre>
  <p>Does anyone have any idea how to build something like this correctly?</p>