【问题标题】:yq - inserting JSON as a raw stringyq - 将 JSON 作为原始字符串插入
【发布时间】:2022-08-14 08:38:57
【问题描述】:

我正在编写一个 GitHub Action 来制作一些 CD,它使用 yq 将环境变量插入到 yaml 文件中以进行部署。

我正在尝试从 GH 机密中读取 JSON,该机密最终将从 env 中读取并加载到 python 中,其中所述字符串将被评估为字典。

在终端中运行它,例如:

yq -i \'.value=\"{\\\"web\\\": \\\"test\\\"}\"\' test.yaml

给我:

value: \'{\"web\": \"test\"}\'

但是在 Github Action 中,我正在这样做:

env:
  JSON=\"{\\\"web\\\": \\\"test\\\"}\"
...
  - name: test
    run : |
        yq -i \'
          .value=strenv(JSON)
        \' deployment.yaml

给我:

Error: Bad expression, please check expression syntax

对该字符串进行其他变体,例如\'{\\\"web\\\": \\\"test\\\"}\'\'\\\"{\\\"web\\\": \\\"test\\\"}\\\"\' 等也给了我同样的错误。

我尝试在 yq 存储库上搜索并查阅文档,但似乎找不到我要查找的内容。

总而言之,我的问题是我想读取一个 JSON 字符串作为字符串当它由 yq 评估时。

  • 一些错别字和我需要指出的另一件事:带有 env-var 的行应该是 JSON: ${{ secrets.JSON }} ——但是其中的字符串值是我运行操作时评估的值。
  • 将变量定义为 JSON=\'{\"web\": \"test\"}\' 并将 yq 运行为 .value=strenv(JSON)
  • 根据我如何引用引号,它似乎要么创建一个带有不匹配文本的错误表达式,要么 yq 将其评估为 yml。
  • 对我来说,这看起来像是正确的 yq 命令——你使用的是哪个版本的 yq?你能回显变量吗?免责声明:我写了 yq

标签: json github-actions yq


【解决方案1】:

yq 的一位用户最近为 yq 的 github 操作文档做出了贡献,内容涉及在 github 操作中使用 env 变量 - 它可能在这里有所帮助:

  - name: Get an entry with a variable that might contain dots or spaces
    id: get_username
    uses: mikefarah/yq@master
    with:
      cmd: yq '.all.children.["${{ matrix.ip_address }}"].username' ops/inventories/production.yml
  - name: Reuse a variable obtained in another step
    run: echo ${{ steps.get_username.outputs.result }}

请参阅https://mikefarah.gitbook.io/yq/usage/github-action 了解更多信息。

免责声明:我写了 yq

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2021-08-13
    • 2016-06-30
    • 1970-01-01
    相关资源
    最近更新 更多