【问题标题】:Using yq to update a yaml file with artifacthub.io annotations使用 yq 更新带有 artifacthub.io 注释的 yaml 文件
【发布时间】:2021-05-03 01:04:04
【问题描述】:

我有一个 bash 脚本,它使用 artifacthub.io annotations 更新 helm yaml 文件。但是,我的脚本正在使用变量,我相信这些变量需要命令使用双引号而不是单引号。此外,artifacthub.io 会导致 artifact 和 io 分离的问题。我可以使用哪个yq 命令来更新changesimages 注释?我也尝试过使用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}"

【问题讨论】:

  • nameimage 字段不是单独的记录,而是多行字符串的一部分 - YAML 世界中的“块”文字。见 - yaml-multiline.info
  • 您将无法直接操作这些字段,而是使用您所需的值形成一个多行字符串。跑yq e '.. style="double"' yaml自己看看

标签: bash sed yaml yq


【解决方案1】:

为了使 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

【讨论】:

  • 感谢您发布此信息。我会测试一下,看看效果如何。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2020-09-17
  • 1970-01-01
  • 2011-11-07
  • 2019-08-03
  • 2022-01-25
  • 1970-01-01
相关资源
最近更新 更多