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

wtzytmuj  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(124)

我在Azure DevOps管道中有一个包含一些步骤的作业,我想多次运行,每次使用不同的变量组。下面是一个简单的例子:

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
如何更改配置以使每次运行时变量组名的设置不同?

tuwxkamq

tuwxkamq1#

我发现在这些场景中有用的是对我所拥有的项进行for each循环,在您的示例中是变量组的集合。
下面是一个更新的代码片段:

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)
cfh9epnr

cfh9epnr2#

基于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 }}不适合我,所以我改变了作业的名称。

相关问题