英文:
How to run a job in an Azure DevOps pipeline multiple times with different variable groups?
问题
我有一个在Azure DevOps管道中的工作,其中有一些步骤,我想要多次运行,每次使用不同的变量组。以下是一个最小示例:
```yml
strategy:
matrix:
Customer:
variableGroupName: 'Customer variables'
Test:
variableGroupName: 'Test variables'
pool:
vmImage: 'windows-latest'
variables:
- group: $(variableGroupName)
steps:
- script: echo $(MyVariable)
然而,当尝试运行管道时,会出现以下错误:
加载YAML构建管道时发生错误。未找到变量组或未经授权使用变量组。有关授权详细信息,请参阅 https://aka.ms/yamlauthz。
如何更改配置以使变量组名称在每次运行时不同?
<details>
<summary>英文:</summary>
I have a job with some steps in an Azure DevOps pipeline that I want to run multiple times, each time with a different variable group. Here is a minimal example:
```yml
strategy:
matrix:
Customer:
variableGroupName: 'Customer variables'
Test:
variableGroupName: 'Test variables'
pool:
vmImage: 'windows-latest'
variables:
- group: $(variableGroupName)
steps:
- script: echo $(MyVariable)
However, it yields this error when trying to run the pipeline:
> An error occurred while loading the YAML build pipeline. Variable group was not found or is not authorized for use. For authorization details, refer to https://aka.ms/yamlauthz.
How can I change the configuration so that the variable group name gets set differently each run?
答案1
得分: 1
以下是翻译好的内容:
在这些情况下我发现有用的是对我拥有的项目进行一次循环,对于你的情况是变量组的集合。
这里是更新后的代码片段:
parameters:
# 定义要使用的变量组
- name: variableGroups
type: object
default:
- name: "Customer"
variableGroupName: "Customer variables"
- name: "Test"
variableGroupName: "Test variables"
pool:
vmImage: 'windows-latest'
stages:
- stage: "test_stage"
jobs:
# 为每个变量组运行作业
- ${{ each variableGroup in parameters.variableGroups }}:
- job: "run_for_each_${{ variableGroup.name }}_variable_group_job"
displayName: "使用 ${{ upper(variableGroup.name) }} 变量组"
variables:
- group: ${{ variableGroup.variableGroupName }}
steps:
- script: echo $(MyVariable)
英文:
What I find useful in these scenarios is a for each loop over the items I have, in your case a collection of variable groups.
Here's an updated code snippet:
parameters:
# Define the variable groups to use
- name: variableGroups
type: object
default:
- name: "Customer"
variableGroupName: "Customer variables"
- name: "Test"
variableGroupName: "Test variables"
pool:
vmImage: 'windows-latest'
stages:
- stage: "test_stage"
jobs:
# Run the job for each variable group
- ${{ each variableGroup in parameters.variableGroups }}:
- job: "run_for_each_${{ variableGroup.name }}_variable_group_job"
displayName: "Using ${{ upper(variableGroup.name) }} variable group"
variables:
- group: ${{ variableGroup.variableGroupName }}
steps:
- script: echo $(MyVariable)
答案2
得分: 1
根据Robert的写法,我得出了以下解决方案:
parameters:
- name: variableGroups
type: object
default:
- name: 'Azure'
variableGroupName: 'MyApplication Azure'
displayName: 'Azure'
- name: 'Customer_Test'
variableGroupName: 'MyApplication Customer Test'
displayName: 'Customer Test'
- name: 'Customer_Prod'
variableGroupName: 'MyApplication Customer Prod'
displayName: 'Customer Prod'
pool:
vmImage: 'windows-latest'
stages:
- stage: 'Build'
jobs:
- ${{ each variableGroup in parameters.variableGroups }}:
- job: '${{ variableGroup.name }}'
displayName: '${{ variableGroup.displayName }}'
variables:
- group: ${{ variableGroup.variableGroupName }}
steps:
- script: echo $(MyVariable)
我将他的回答标记为接受的答案。但为了完整起见,我的代码也在这里。我更改了缩进,为variableGroups
添加了displayName
属性,并且我认为${{ environment.name }}
对我没有起作用,所以我更改了作业的名称。
英文:
Based on what Robert wrote, I ended up with this solution:
parameters:
- name: variableGroups
type: object
default:
- name: 'Azure'
variableGroupName: 'MyApplication Azure'
displayName: 'Azure'
- name: 'Customer_Test'
variableGroupName: 'MyApplication Customer Test'
displayName: 'Customer Test'
- name: 'Customer_Prod'
variableGroupName: 'MyApplication Customer Prod'
displayName: 'Customer Prod'
pool:
vmImage: 'windows-latest'
stages:
- stage: 'Build'
jobs:
- ${{ each variableGroup in parameters.variableGroups }}:
- job: '${{ variableGroup.name }}'
displayName: '${{ variableGroup.displayName }}'
variables:
- group: ${{ variableGroup.variableGroupName }}
steps:
- script: echo $(MyVariable)
I'm marking his answer as the accepted answer. But just for the sake of completeness, my code is here as well. I changed the indentation, added a displayName
property to the variableGroups
, and I don't think ${{ environment.name }}
worked for me so I changed the name of the jobs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论