简体   繁体   English

YAML 解析器 yq 使用过滤器修改文件

[英]YAML parser yq modifying file in place with filter

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我准备了一个Kubernetes的manifest文件,在GitOps操作中从circleci构建docker镜像,push到ECR,同时将circleci的image tag变化的PR发送到GitHub

At that time, I tried to edit the manifest file in the circleci job as well当时,我也尝试编辑 circleci 作业中的清单文件

Problems, unknown points问题,未知点

I tried to change the yaml file using yq command.我尝试使用 yq 命令更改 yaml 文件。

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我想知道有没有人知道

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.生成的过滤器是正确的,但 RHS 上的更新运算符|=需要出现字符串类型或适当的对象类型。 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像它的前身jq支持传递要在过滤器中使用的参数字段,所以你可以这样做

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'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM