YAML parser yq modifying file in place with filter

Viewed 3110

background

I prepare a manifest file of Kubernetes, build a docker image from circleci in GitOps operation, push it to ECR, and at the same time, send PR of image tag change from circleci to GitHub

At that time, I tried to edit the manifest file in the circleci job as well

Problems, unknown points

I tried to change the yaml file using yq command.

yq -y -i  '.images |= map(select(.name=="XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend-admin").newTag |= '$CIRCLE_SHA1')'  k8s/overlays/dev/bases/kustomization.yaml

manifest file

namespace: production

bases:
  - ../../../bases/admin

patchesStrategicMerge:
  - patch-admin.yaml

images:
  - name: nginx
    newTag: 1.15.2
  - name: XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend
    newName: XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend
    newTag: backend-tag
  - name: XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/frontend
    newName: XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/frontend
    newTag: latest

but error occur

jq: error: a99cce09747507feb74799e1e1c9459aba43d4a6/0 is not defined at <top-level>, line 1:
.images |= map(select(.name=="XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend-admin").newTag |= a99cce09747507feb74799e1e1c9459aba43d4a6)

I think that the cause is that it is included up to I saw various articles, but none of them led to a solution.

I want to know if anyone knows something

2 Answers

The filter generated is right, but the update operator |= on the RHS takes either a string type or a proper object type to be present. What you have is not interpreted as a literal string. Put the tag around quotes ".."

yq -y '.images |= map(select(.name=="XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend").newTag |= "a99cce09747507feb74799e1e1c9459aba43d4a6")'

Also yq like its predecessor jq supports passing argument fields to be used within the filter, so you can do

yq -y --arg tag "${CIRCLE_SHA1}" '.images |= map(select(.name=="XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/backend").newTag |= $tag )'

Variables are not expanded inside single quotes. Please go outside.

'$CIRCLE_SHA1' 

to

"'$CIRCLE_SHA1'"

Related