2

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
  • You could load the manifest in as a Map from the YAML, modify the Map, and then write it back out to YAML. However, it would be easier to input the value of the JP parameter as a value via something like Helm. – Matthew Schuchard May 12 '21 at 12:35
  • Whereabouts would I load in the yaml and edit it? In the agent block? – Thomas Teece May 12 '21 at 15:56

1 Answers1

1

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'
    )
  }
}
csanchez
  • 1,623
  • 11
  • 20