Azure DevOps 管道动态列出变量组的成员
Azure DevOps Pipeline Dynamically List Members of Variable Group
我正在尝试让 YAML 管道构建一个 JSON 文件以用作 Cypress.env.json
。一些值需要来自变量组(最终绑定到 Azure Key Vault)。我希望它是动态构建的,因此如果将秘密添加到 Key Vault 并通过变量组公开,它将作为 JSON 文件的新 属性 写出,而无需 YAML 管道被修改。我希望我在 中找到了一个技巧,它描述了一个名为 convertToJson
的未记录函数,它允许将管道对象作为 JSON 字符串传递给脚本。所以我试了一下:
- job:
variables:
- group: DevVariableGroup
steps:
- bash: echo "${{ convertToJson(variables) }}"
displayName: 'Get an Object - Bash'
只要我得到一个包含编译时可用的所有变量的 JSON 字符串,它就可以工作。不幸的是,变量组提供的变量不在其中。我只能使用模板语法使 convertToJson
函数正常工作,而不能使用宏或运行时语法。
我想不出另一种方法来动态访问变量组提供的变量。有什么建议吗?
我想我会回到这个问题并分享我们找到的解决方案,以防它对其他人有帮助。这些秘密保存在 Key Vault 中,并使用 AzureKeyVault@1 task. That downloads the values to variables, so there isn't a need to rely on a Variable Group to expose the Key Vault's contents. Once they are variables, we can use the replacetokens@3 task 检索。因此,我们更改了要填充值的 JSON 文件,以便它可以处理该任务。
两个任务让我们得到了我们需要的东西。
- task: AzureKeyVault@1
displayName: 'Read vault'
inputs:
azureSubscription: ${{ parameters.azureSubscription }}
KeyVaultName: $(keyVaultName)
SecretsFilter: '*'
RunAsPreJob: false
- task: replacetokens@3
inputs:
targetFiles: '**/cypress.json'
encoding: 'auto'
writeBOM: true
actionOnMissing: 'warn'
keepToken: false
tokenPrefix: '#{'
tokenSuffix: '}#'
useLegacyPattern: false
enableTransforms: false
enableTelemetry: true
我正在尝试让 YAML 管道构建一个 JSON 文件以用作 Cypress.env.json
。一些值需要来自变量组(最终绑定到 Azure Key Vault)。我希望它是动态构建的,因此如果将秘密添加到 Key Vault 并通过变量组公开,它将作为 JSON 文件的新 属性 写出,而无需 YAML 管道被修改。我希望我在 convertToJson
的未记录函数,它允许将管道对象作为 JSON 字符串传递给脚本。所以我试了一下:
- job:
variables:
- group: DevVariableGroup
steps:
- bash: echo "${{ convertToJson(variables) }}"
displayName: 'Get an Object - Bash'
只要我得到一个包含编译时可用的所有变量的 JSON 字符串,它就可以工作。不幸的是,变量组提供的变量不在其中。我只能使用模板语法使 convertToJson
函数正常工作,而不能使用宏或运行时语法。
我想不出另一种方法来动态访问变量组提供的变量。有什么建议吗?
我想我会回到这个问题并分享我们找到的解决方案,以防它对其他人有帮助。这些秘密保存在 Key Vault 中,并使用 AzureKeyVault@1 task. That downloads the values to variables, so there isn't a need to rely on a Variable Group to expose the Key Vault's contents. Once they are variables, we can use the replacetokens@3 task 检索。因此,我们更改了要填充值的 JSON 文件,以便它可以处理该任务。
两个任务让我们得到了我们需要的东西。
- task: AzureKeyVault@1
displayName: 'Read vault'
inputs:
azureSubscription: ${{ parameters.azureSubscription }}
KeyVaultName: $(keyVaultName)
SecretsFilter: '*'
RunAsPreJob: false
- task: replacetokens@3
inputs:
targetFiles: '**/cypress.json'
encoding: 'auto'
writeBOM: true
actionOnMissing: 'warn'
keepToken: false
tokenPrefix: '#{'
tokenSuffix: '}#'
useLegacyPattern: false
enableTransforms: false
enableTelemetry: true