【问题标题】:Github Action: How to edit a JSON objects with Github repository secretsGithub 操作:如何使用 Github 存储库机密编辑 JSON 对象
【发布时间】:2023-02-24 03:13:08
【问题描述】:

Github 操作:如何使用 Github 存储库机密编辑 JSON 对象在我的工作流程中,我的工作是编辑 json 文件中的空 JSON 值,并将它们替换为我的 github 秘密。问题是当我运行 cat 'test.json' 命令时,我的更新值没有出现:

JSON 文件:

{
"secret": "",
"name": "test"
}

工作流程:

steps:
- name: Edit Json
      shell: bash
      run: |
        echo "`jq '.secret="${{ secrets.PRIVATE_KEY }}"'test.json`" > test.json
    - name: display
      run: |
        cat 'test.json'

输出:

{
"secret": "",
"name": "test"
}

预期输出:

{
"secret": "****************",
"name": "test"
}

【问题讨论】:

  • 尝试:jq '.secret = "${{ secrets.PRIVATE_KEY }}"' test.json > test.out.json,然后是cat test.out.json
  • 请考虑使用现有的 github 操作来更新该值,例如 github.com/jossef/action-set-json-field
  • 这个秘密被编辑了,它永远不会被打印出来。要进行测试,您可以以某种方式修改它,例如,对其进行 base64 编码,然后查看该值是否看起来正确。

标签: json github github-actions


【解决方案1】:

您可以使用 jq 和 --arg 来实现

这个例子对我有用:

steps:
  - name: Create test file
    run: |
      cat > test.json << EOL
      {
          "secret": "",
          "name": "test"
      }
      EOL
  - name: Edit Json
    shell: bash
    run: |
      echo $(jq --arg a "${{ secrets.MY_SECRET }}" '.secret = ($a)' test.json) > test.json   
  - name: display
    run: |
      cat test.json
  - uses: actions/upload-artifact@v3
    with:
      name: my-test-file
      path: ./test.json

我使用了actions/upload-artifact,所以我将我的test.json 作为工件上传,然后作为解决方法在我的机器上读取它,因为 GitHub 隐藏了输出。

是的,如果您在控制台中看到 *** 并不意味着该文件没有更改,它只是在工作流的控制台和日志中被替换了。

【讨论】:

    猜你喜欢
    • 2021-08-12
    • 2020-10-10
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2021-10-03
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多