簡體   English   中英

使用 yq 更新帶有 artifacthub.io 注釋的 yaml 文件

[英]Using yq to update a yaml file with artifacthub.io annotations

我有一個 bash 腳本,它正在使用artifacthub.io 注釋更新 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}"

為了使 yq 查詢盡可能可讀,我盡量避免使用 escaping 雙引號。 通過在 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM