Search code examples
gitlab-cijqyq

update YAML file using YQ in GitlabCI


So basically, I have this command that runs in Gitlab CI to update a field in YAML configuration before packaging and pushing a Helm chart.

yq -i -y ".pod.image.imageTag="${CI_COMMIT_SHORT_SHA}"" deployment/values.yaml

values.yaml

pod:
  image:
    repository: my.private.repo/my-project
    imageTag: 'latest'
  nodegroupName: "nessie-nodegroup"

But I keep getting this error.

jq: error: syntax error, unexpected IDENT, expecting $end (Unix shell quoting issues?)
.pod.image.imageTag=4c0118bf  

The variable is actually read but it looks like I'm doing something wrong in the yq command. Any ideas where that error is coming from ? Trying with only one quote doesn't read the environment variable obviously. I already tried it.

Update:

Trying with :

yq -i -y '.pod.image.imageTag="${CI_COMMIT_SHORT_SHA}"' deployment/values.yaml

and

yq -i -y .pod.image.imageTag="${CI_COMMIT_SHORT_SHA}" deployment/values.yaml

didn't work either.


Solution

  • With the -y option I assume you are using the kislyuk/yq implementation.

    Use jq's --arg option to introduce values from shell:

    yq -i -y --arg tag "${CI_COMMIT_SHORT_SHA}" '.pod.image.imageTag=$tag' deployment/values.yaml