How to update Helm Chart.yaml from command line

5/6/2019

I'm looking to dynamically update my Chart.yaml file specifically the version when I run a make helm build command.

For example Chart.yaml contains

apiVersion: v1
appVersion "1.0"
description: A helm chart for so and so
name: my app
version: 0.2

I'm looking for a way to run make helm build version=0.3 and when that build is complete see the updated version number in that builds Chart.yaml

It's my understanding I can't pass variables to .yaml files so not sure if this is possible?

-- Time Keeper
docker
kubernetes-helm
linux
yaml

1 Answer

5/7/2019

First of all your Chart.yaml is not valid YAML, you need to insert a value separator (:) before "1.0" on the second line.

Assuming your Makefile looks like:

helm:
        python3 updateversion.py Chart.yaml ${version}
        cat Chart.yaml

, ruamel.yaml is installed for your Python3 and your updateversion.py:

import sys
from pathlib import Path
import ruamel.yaml

yaml_file = Path(sys.argv[1])

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
# uncomment and adapt next line in case defaults don't match your indentation
# yaml.indent(mapping=4, sequence=4, offset=2)

data = yaml.load(yaml_file)
version = sys.argv[2]
if isinstance(data['version'], float):
    version = float(version)
data['version'] = version

yaml.dump(data, yaml_file)

you can run make helm version=0.3 to get output:

apiVersion: v1
appVersion: "1.0"
description: A helm chart for so and so
name: my app
version: 0.3

The trick with testing value for version being a float is necessary as 0.2 is a float when loading YAML, but 0.2.1 is a string. And what you get from the commandline using sys.argv[2] is always a string.

You can replace the cat Chart.yaml line for the target helm with whatever you need to run with the updated file.

-- Anthon
Source: StackOverflow