Use first non-empty value from a list in templates


Context

When customizing the values to be passed down to helm from helmfile, there are scenarios where it is desirable to accept potentially different values, depending on whether these are present or not.

For example, to specify the tag value for a docker image, we could pick its final value based on different environment variables, as follows:

  1. If COMMIT_SHA is present, use it
  2. Else, if BRANCH_NAME is set, use it
  3. Finally, if none of the previous environment variables are set, use latest

Implementation

Instead of using an if/else condition, which could be cumbersome and may not work, we can leverage the coalesce function, which will pick the first non-empty value from a given list.

tag: {{ coalesce (env "COMMIT_SHA") (env "BRANCH_NAME") "latest" }}

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.