如何解决使用 yq 更新带有 artifacthub.io 注释的 yaml 文件
我有一个 bash 脚本,它正在使用 artifacthub.io annotations 更新 helm yaml 文件。但是,我的脚本使用的变量,我相信,需要命令使用双引号而不是单引号。此外,artifacthub.io
会导致 artifact 和 io 分离的问题。我可以使用哪个 yq
命令更新 changes
和 images
注释?我也试过使用 sed
无济于事。
annotations:
artifacthub.io/changes: |
- Fixed linting issues.
artifacthub.io/images: |
- name: transmission
image: ghcr.io/linuxserver/transmission:3.00-r0-ls75
我尝试过类似下面的方法,但没有成功。
image=foo
yq e ".annotations."artifacthub.io/images"=\"${image}\"" -i "${chart_file_path}"
解决方法
为了尽可能保持 yq 查询的可读性,我尽量避免转义双引号。通过在 yq 查询周围使用单引号,不必转义双引号。此外,单引号可以关闭并重新打开以将 bash 变量连接到查询。
对于带有特殊字符的键,您需要将它们括在双引号和方括号中:.annotations.["artifacthub.io/images"]
给定文件:
# file.yml
annotations:
artifacthub.io/changes: |
- Fixed linting issues.
artifacthub.io/images: |
- name: transmission
image: ghcr.io/linuxserver/transmission:3.00-r0-ls75
执行这个脚本:
image="foo"
yq eval '.annotations.["artifacthub.io/images"] = "'${image}'"' file.yml
# | | | || |||
# | | | || ||└> (a.1) close yq query
# | | | || |└> (c) end string value
# | | | || └> (a.2) open yq query (end concatenation)
# | | | |└> (a.2) close yq query (start concatenation)
# | | | └> (c) start string value
# | | └> (b) end key w/ special chars
# | └> (b) start key w/ special chars
# └> (a.1) open yq query
生成此输出:
annotations:
artifacthub.io/changes: |
- Fixed linting issues.
artifacthub.io/images: |-
foo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。