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

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

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

问题

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

期望如下:

替换之前:

{
    "externalapp": ""
}

替换之后:

{
    "externalapp": {
        "url": "https://external.com",
        "cleintid": "cid",
        "secret": "client secret"
    }
}

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

以下是实际输出:

"externalapp":
    "{
    \"url\":\"https://external.com\",
    \"clientid\": \"cid\",
    \"secret\":\"client secret\"
    }"

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

请查找工作流程代码:

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

提前感谢!

英文:

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

Below is the expectation:

Before replacement:

{    
    "externalapp": ""    
}

After replacement:

{
    "externalapp": {
        "url":"https://external.com",
        "cleintid": "cid",
        "secret":"client secret"
    }
}

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:

"externalapp":
    "{\n
    \"url\":\"https://external.com", \n
    \"clientid\": \"cid", \n
    \"secret\":\"client secret" \n
    \n}"

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

Please find the workflow code:

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

Thanks in advance!

答案1

得分: 3

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

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

jobs:
  foo:
    runs-on: ubuntu-latest
    steps:
      - shell: bash
        run: |
                    echo '{ "externalapp": ""  }' > appsettings.json
      - name: 替换密钥
        shell: bash
        run: |
                      cat appsettings.json | jq --argjson my_secret $MY_SECRET '.externalapp = $my_secret' > newfile.json
        env: 
          # 在您的情况下,应该是:${{ secrets.EXTERNAL_APP }}
          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:

jobs:
  foo:
    runs-on: ubuntu-latest
    steps:
      - shell: bash
        run: |
          echo '{ "externalapp": ""  }' > appsettings.json
      - name: Replace secret
        shell: bash
        run: |
           cat appsettings.json | jq --argjson my_secret $MY_SECRET '.externalapp = $my_secret' > newfile.json
        env: 
          # In your case it would be:  ${{ secrets.EXTERNAL_APP }}
          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:

确定