Replacing a value as a string with yq

Viewed 694

I have the following map of strings and I would like to change the value of "image.tag" key . I tried the following but it does not work as I expected. The problem here is that image.tag is a string but I am not sure how to express that. Thanks

yq eval --inplace ".spec.chart.values.\"image.tag\": \"$TAG\"" values.yaml


spec:
  chart:
    values:
      image.tag: master
  
1 Answers

You don't have to use double quotes for reading variables from shell. mikefarah/yq provides a method strenv to load variables (also environment) form the shell.

Also by using single quotes, you can just wrap image.tag under double quotes to let it be treated as a single word.

Use the style method to set quotes for the updated value style="double" reflects the updated tag value to be treated as a string.

newtag="foo" yq e --inplace '.spec.chart.values."image.tag" |= strenv(newtag) | ..style="double"' values.yaml

or if the new tag is defined as a shell variable say TAG

newtag="$TAG" yq e --inplace '.spec.chart.values."image.tag" |= strenv(newtag) | ..style="double"' values.yaml

Note that, if you are using yq version above 4.18.1, the eval action e is the default one and can be skipped altogether.

Related