In my values.yaml I have:
isLocal: false
localEnv:
url: abcd
prodEnv:
url: defg
Then I have a service.yaml:
{{- if .Values.isLocal }}
{{- $env := .Values.localEnv }}
{{- else}}
{{- $env := .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}
Unfortunately I got a 'undefined variable "$env"' error, does anyone have idea how to achieve this? Thanks!
One more way would be to create the variable before starting the if/else block. For example:
{{- $env := .Values.prodEnv -}}
{{ if .Values.isLocal }}
{{- $env = .Values.localEnv -}}
{{ end}}
Notice that I've not used the := operator inside the if block as the variable is already created.
and then
url: {{ $env.url}}
You actually can define the variable $env outside the if block and assign its value using = with if block.
{{- $env := "" }}
{{- if .Values.isLocal }}
{{- $env = .Values.localEnv }}
{{- else}}
{{- $env = .Values.prodEnv }}
{{- end}}
url: {{ $env.url}}
Please note:
:=will declare the variable while assigning the value. On the other hand,=will assign the value only. If you use:=inif, it will declare a new local variable $env which will not impact the value of $env declared outside theifblock.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With