Helm, create env var with image tag value

5/28/2020

I'm fairly new to helm as I use charts created by other ppl for our applications and I'm trying to do something that I suppose is kinda easy but couldn't manage to find how to. Basically I want to pass application version to my react app. Based on the few information I found, here is what I came up with

image:
    tag: 0.2.6
extraEnv:
  - name: REACT_APP_APP_VERSION
    value: {image.tag}

thx in advance

-- Maxime Couture
environment-variables
kubernetes
kubernetes-helm

2 Answers

5/28/2020

I assume the code you sent is your values.yaml. Then, the first part is correct.

image:
    tag: 0.2.6

Now, you don't specify variables passed to pod in values.yaml, but in your templates/* files. For example, to pass an variable to your pod, you can use the following code:

env:
  - name: REACT_APP_APP_VERSION
    value: "{{ .Values.image.tag }}"

Check this for the complete example.

Note that you cannot use values from values.yaml inside values.yaml. So the code you sent won't work. That is because, the values.yaml file itself is not evaluated.

-- RafaƂ Leszko
Source: StackOverflow

5/29/2020

From Documentation

The tpl function allows developers to evaluate strings as templates inside a template. This is useful to pass a template string as a value to a chart or render external configuration files. Syntax: {{ tpl TEMPLATE_STRING VALUES }}

You will have something similar to

values.yaml

image:
  repository: k8s.gcr.io/busybox
  tag: "latest"

extraEnv: "{{ .Values.image.tag }}"

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test
spec:
  containers:
  - name: test
    image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
    env:
    - name: REACT_APP_APP_VERSION
      value: {{ tpl .Values.extraEnv. }}
-- edbighead
Source: StackOverflow