jenkins-pipelinejenkins-groovy

convert jenkinsfile agent label switch to multiple embeded podSpec


Team, I have this function to pick podtemplate based on login and it works but I need to convert it to not use template but use pod spec for dynamic agent scheduling. any hint how can I achieve it? with one agent am able to do it but need to do for three so am stuck.

def buildAgent() {
    agent = 'artifacts'
    if (params.Cuda) {
        agent =  'artifacts-gpu'
    }
    if (params.'Image Targets'.contains('drive')) {
        agent = 'artifacts-drive'
    }
    return agent
}

i have three specs for each agent above. how can i replace them?

artifacts podSpec

apiVersion: v1
kind: Pod
metadata:
spec:
  containers:
  - name: argocd
    image: image.com/third_party/argocd-jenkins-agent:2.2
    command:
    - cat
    tty: true
    volumeMounts:
      - mountPath: "/home/jenkins/agent"
        name: "workspace-volume"
        readOnly: false
    hostNetwork: false

artifacts-gpu podSpec

apiVersion: v1
kind: Pod
metadata:
spec:
  .
  .

artifacts-drive podSpec

apiVersion: v1
kind: Pod
metadata:
spec:
  .
  .

single agent pipeline works like below but am not sure how to do for multiple agents.

pipeline {
  agent {
    kubernetes {
      cloud 'ipp-stuff'
      yaml '''
apiVersion: v1
kind: Pod
metadata:
spec:
'''
  }
 }
}

Solution

  • created a variable podYaml and used it as below

    podYaml= """
    apiVersion: v1
    kind: Pod
    spec:
      imagePullSecrets:
      - name: repo-ecr
      containers:
      - name: main
        image: test-verify:20220328135044
        resources:
          requests:
    
        volumeMounts:
          - mountPath: "/home/jenkins/agent"
            name: "workspace-volume"
            readOnly: false
        hostNetwork: false
    """
    pipeline {
      agent {
        kubernetes {
            cloud 'sc-prod'
            yaml podYaml
          }
      }