skip to Main Content

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:

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?

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search