Nested JSON appsetting substitution not working in microsoft/variable-substitution

huangapple go评论107阅读模式
英文:

Nested JSON appsetting substitution not working in microsoft/variable-substitution

问题

我正在尝试替换应用程序设置中的嵌套JSON对象。

期望如下:

替换之前:

  1. {
  2. "externalapp": ""
  3. }

替换之后:

  1. {
  2. "externalapp": {
  3. "url": "https://external.com",
  4. "cleintid": "cid",
  5. "secret": "client secret"
  6. }
  7. }

externalapp JSON({..} 包括大括号)存储为GitHub环境中的机密,并在工作流运行期间引用。
但问题是替换后的JSON显示为字符串。

以下是实际输出:

  1. "externalapp":
  2. "{
  3. \"url\":\"https://external.com\",
  4. \"clientid\": \"cid\",
  5. \"secret\":\"client secret\"
  6. }"

有没有办法去掉这些额外的字符(\n\")?

请查找工作流程代码:

  1. - name: 更新动态URL
  2. uses: microsoft/variable-substitution@v1
  3. with:
  4. files: 'appsettings.json'
  5. env:
  6. externalapp: ${{ secrets.EXTERNAL_APP }}

提前感谢!

英文:

I am trying to replace a nested JSON object in the app setting.

Below is the expectation:

Before replacement:

  1. {
  2. "externalapp": ""
  3. }

After replacement:

  1. {
  2. "externalapp": {
  3. "url":"https://external.com",
  4. "cleintid": "cid",
  5. "secret":"client secret"
  6. }
  7. }

externalapp JSON ({..} including braces) is stored as a secret in GitHub environments and referenced during the workflow run.
But the problem is that the replaced JSON is appearing as a string.

Below is the actual output:

  1. "externalapp":
  2. "{\n
  3. \"url\":\"https://external.com", \n
  4. \"clientid\": \"cid", \n
  5. \"secret\":\"client secret" \n
  6. \n}"

Any idea how to get rid of these additional characters (\n, \,")?

Please find the workflow code:

  1. - name: Update Dynamics URL
  2. uses: microsoft/variable-substitution@v1
  3. with:
  4. files: 'appsettings.json'
  5. env:
  6. externalapp: ${{ secrets.EXTERNAL_APP }}

Thanks in advance!

答案1

得分: 3

问题在于jobs.<job_id>.steps\[*\].env将您的密钥加载为JSON字符串,这将用\n替换您的换行符,并将所有JSON键/值的引号转义为\"

如果您没有另一个使用microsoft/variable-substitution的原因,只需使用jq来更新您的对象。例如:

  1. jobs:
  2. foo:
  3. runs-on: ubuntu-latest
  4. steps:
  5. - shell: bash
  6. run: |
  7. echo '{ "externalapp": "" }' > appsettings.json
  8. - name: 替换密钥
  9. shell: bash
  10. run: |
  11. cat appsettings.json | jq --argjson my_secret $MY_SECRET '.externalapp = $my_secret' > newfile.json
  12. env:
  13. # 在您的情况下,应该是:${{ secrets.EXTERNAL_APP }}
  14. MY_SECRET: '{ "url":"https://external.com", "cleintid": "cid", "secret":"client secret" }'
英文:

The issue is that jobs.<job_id>.steps\[*\].env loads your secret as a JSON string, which will replace your line breaks with \n and escape all your JSON key/values quotes with \".

If you don't have another reason to use microsoft/variable-substitution, just use jq to update your object. For example:

  1. jobs:
  2. foo:
  3. runs-on: ubuntu-latest
  4. steps:
  5. - shell: bash
  6. run: |
  7. echo '{ "externalapp": "" }' > appsettings.json
  8. - name: Replace secret
  9. shell: bash
  10. run: |
  11. cat appsettings.json | jq --argjson my_secret $MY_SECRET '.externalapp = $my_secret' > newfile.json
  12. env:
  13. # In your case it would be: ${{ secrets.EXTERNAL_APP }}
  14. MY_SECRET: '{ "url":"https://external.com", "cleintid": "cid", "secret":"client secret" }'

huangapple
  • 本文由 发表于 2023年3月12日 17:47:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定