如何在Azure DevOps管道中多次运行作业,并使用不同的变量组?

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

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: &#39;Customer variables&#39;
    Test:
      variableGroupName: &#39;Test variables&#39;

pool:
  vmImage: &#39;windows-latest&#39;

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: &quot;Customer&quot;
        variableGroupName: &quot;Customer variables&quot;
      - name: &quot;Test&quot;
        variableGroupName: &quot;Test variables&quot;

pool:
  vmImage: &#39;windows-latest&#39;

stages:
  - stage: &quot;test_stage&quot;
    jobs:
      # Run the job for each variable group
      - ${{ each variableGroup in parameters.variableGroups }}:
          - job: &quot;run_for_each_${{ variableGroup.name }}_variable_group_job&quot;
            displayName: &quot;Using ${{ upper(variableGroup.name) }} variable group&quot;
            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: &#39;Azure&#39;
    variableGroupName: &#39;MyApplication Azure&#39;
    displayName: &#39;Azure&#39;
  - name: &#39;Customer_Test&#39;
    variableGroupName: &#39;MyApplication Customer Test&#39;
    displayName: &#39;Customer Test&#39;
  - name: &#39;Customer_Prod&#39;
    variableGroupName: &#39;MyApplication Customer Prod&#39;
    displayName: &#39;Customer Prod&#39;

pool:
  vmImage: &#39;windows-latest&#39;

stages:
- stage: &#39;Build&#39;
  jobs:
  - ${{ each variableGroup in parameters.variableGroups }}:
    - job: &#39;${{ variableGroup.name }}&#39;
      displayName: &#39;${{ variableGroup.displayName }}&#39;
      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.

huangapple
  • 本文由 发表于 2023年6月13日 00:19:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458541.html
匿名

发表评论

匿名网友

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

确定