<h2>Setup</h2>
<ul>
<li>Kubernetes v1.27 (EKS)</li>
<li>Jenkins installed from <a href="https://github.com/jenkinsci/helm-charts" rel="nofollow noreferrer">helm chart</a></li>
<li>Jenkins configured from code (CaaC) with <a href="https://www.jenkins.io/doc/book/pipeline/shared-libraries/" rel="nofollow noreferrer">shared libraries</a></li>
</ul>
<h2>Jenkins shared library configuration</h2>
<p>All files are located in <code>var</code> directory</p>
<p><code>pipelineTemplate.groovy</code></p>
<pre><code>#!/usr/bin/env groovy

def call() {
  pipeline {
    agent {
      kubernetes {
         yaml kaniko()
      }
     }

    stages {
      stage ('1. Build') {
        steps {
          kanikoBuild()
        }
      }
    }
  }
}
</code></pre>
<p><code>kaniko.groovy</code></p>
<pre><code>#!/usr/bin/env groovy

def call(){
agent =  &quot;&quot;&quot;
  apiVersion: v1
  kind: Pod
  metadata:
    labels:
      role: agent
    name: jenkins-build
    namespace: jenkins
  spec:
    serviceAccountName: jenkins-agent
    imagePullSecrets:
    - name: kaniko-docker
    containers:
    - name: kaniko
      image: gcr.io/kaniko-project/executor:v1.10.0-debug
      imagePullPolicy: Always
      command:
      - /busybox/cat
      tty: true
      volumeMounts:
      - name: kaniko-docker
        mountPath: /kaniko/.docker
      resources:
        requests:
          cpu: 3
          memory: 5Gi
    restartPolicy: Never
    volumes:
    - name: kaniko-docker
      projected:
        sources:
        - secret:
            name: kaniko-docker
            items:
              - key: .dockerconfigjson
                path: config.json
&quot;&quot;&quot;
return agent
}
</code></pre>
<p><code>kanikoBuild.groovy</code></p>
<pre><code>#!/usr/bin/env groovy

def generateStage(job) {
  return {
    stage(&quot;Build ${job}&quot;) {
      sh &quot;&quot;&quot;
        executor \
        -f `pwd`/Dockerfile-${job} \
        -c `pwd` \
        --cache=true \
        --cache-repo=mydocker/${job} \
        --destination=mydocker/${job}:${env.GIT_COMMIT}
      &quot;&quot;&quot;
    }
  }
}

def call() {
  container('kaniko') {
    def stagesList = [:]
    def parallelStagesMap = ''
  
    stagesList = [
      &quot;app1&quot;,
      &quot;app2&quot;
    ]

    parallelStagesMap = stagesList.collectEntries {[&quot;${it}&quot; : generateStage(it)]}
                
    parallelStagesMap.failFast = false
    parallel parallelStagesMap
  }
}
</code></pre>
<h2>What is working</h2>
<p>When I add new item to <code>stagesList</code> it create new stage dynamically which is great since I can update only one place and don't need to bother with copy-past code for <code>kaniko</code> executor</p>
<h2>What is not working</h2>
<p>Since <code>kaniko</code> is design to run only single executor in one pod my pipeline is failing since parallel stages are run on the same pod which is <code>kaniko</code>. This is also problem since I would like to add different resource allocation for some applications like 5CPU for <code>app1</code> and 2CPU for <code>app2</code>.</p>
<h2>What I would like to achieve</h2>
<p>I would like to run each dynamically generated stage of my pipeline on different executor which means that each application define is <code>stagesList</code> should start separate pod with <code>kaniko</code> and build the application.</p>
<p>I was trying various things like adding pod definition to <code>generateStage</code> function but without any luck.</p>
<p>Any help will be highly appreciated.</p>

Michał Lewndowski
<p>You could try using the <code>podTemplate()</code> step.</p>
<p>Example :</p>
<pre><code>def generateStage(job) {
  return {
    stage(&quot;Build ${job}&quot;) {
      podTemplate(yaml: kaniko()) {
        node(POD_LABEL) {
          checkout scm
          container('kaniko') {
            sh &quot;&quot;&quot;
              executor \
              -f `pwd`/Dockerfile-${job} \
              -c `pwd` \
              --cache=true \
              --cache-repo=mydocker/${job} \
              --destination=mydocker/${job}:${env.GIT_COMMIT}
            &quot;&quot;&quot;
          }
        }
      }
    }
  }
}
</code></pre>
<p>See <a href="https://plugins.jenkins.io/kubernetes/#plugin-content-using-the-pipeline-step" rel="nofollow noreferrer">the documentation</a> for more information.</p>
