Using Parameters in yamlFile of kubernetes agent in jenkins decorative pipeline

1.3k views Asked by At

I have a shared yaml file for multiple pipelines and I would like to parameterize the tag of one of the images in the yaml file.

What would be the simplest way to do this? At the moment I am maintaining multiple KubernetesPods.yaml, such as KubernetesPods-1.5.0.yaml and interpolating the parameter into the name ( yamlFile "KubernetesPods-${params.AGENT_POD_SPEC}.yaml"), but this does not scale well.

Can I get parameters into the yaml without having to have the yaml written out in every pipeline?

Example pipeline:

pipeline {
  agent {
    kubernetes {
      yamlFile 'KubernetesPods.yaml'
    }
  }
  parameters {
    choice(
      name: 'AGENT_POD_SPEC',
      choices: ['1.5.0','1.3.0','1.2.0','1.4.0'],
      description: 'Agent pod configuration'
    )
  }
}

Example KubernetesPods.yaml:

kind: Pod
spec:
  containers:
  - name: example-image
    image: example/image:<IMAGE-TAG-I-WANT-TO-PARAMETERIZE>
    imagePullPolicy: Always
    command:
    - cat
1

There are 1 answers

0
csanchez On

You can do it using yaml instead of yamlFile

pipeline {
  agent {
    kubernetes {
      yaml """
kind: Pod
spec:
  containers:
  - name: example-image
    image: example/image:${params.AGENT_POD_SPEC}
    imagePullPolicy: Always
    command:
    - cat
"""
    }
  }
  parameters {
    choice(
      name: 'AGENT_POD_SPEC',
      choices: ['1.5.0','1.3.0','1.2.0','1.4.0'],
      description: 'Agent pod configuration'
    )
  }
}