4

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
  • 51
  • 1
  • 3
  • What does your current `Makefile` do? Is it specifically important for the version number to be in the `Chart.yaml` file, as opposed to just an injected value? – David Maze May 06 '19 at 22:49
  • Is there anything against updating the file before you call helm in the `helm` rule in your makefile? That can be trivially done using a few lines of Python. – Anthon May 07 '19 at 05:17
  • @DavidMaze It's doing quite a few things but in regards to strictly helm it's building(making a directory then tar the file) and pushing(creating a repo in S3 and pushing to it) I think injection could work as well, if I can leave the Chart.yaml file with no version and it will take something from the command line? – Time Keeper May 07 '19 at 13:24
  • @Anthon not necessarily just the idea here is that I wanted to create one VERSION variable so when I do `make build VERSION=someversion` or `make helm-build VERSION=someversion` it's just one command line arguement to keep the docker container version and helm chart version the same. (For our usage these values must be the same) – Time Keeper May 07 '19 at 13:25
  • I feel like modifying make is the way to go but can't get it exactly how I want.. i'm trying helm --set version=$(VERSION) within my make file but not sure how I can get that to apply directly to the Charts.yaml file – Time Keeper May 07 '19 at 14:21

2 Answers2

1

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
  • 69,918
  • 32
  • 186
  • 246
  • The default indentation is 2 for both mappings and items (which leaves no space to offset the item indicator (`-`), so it has to be zero). There are ways to do some detection on that if the file varies often, but here you update the same file. Comments, tags, anchor names etc in your YAML file will be preserved (i.e. things that other YAML loader-dumpers usually don't preserve) – Anthon May 07 '19 at 15:16
  • The author of this answer mentioned `disclaimer: I am the author of that package` in a separate [answer](https://stackoverflow.com/a/49767944/7365866). That's quite useful to have here too :) – Ben Butterworth Aug 16 '22 at 14:18
0

Helm package has a --version argument

Run helm package --version 1.2.3 .helm/package_name

  • This won't help you modify the Chart.yaml, it creates a Chart archive file instead.
  • For more information, take a look at helm package --help.
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167