Home > database >  Can I add more than one variable to an existing variable group in Azure DevOps?
Can I add more than one variable to an existing variable group in Azure DevOps?

Time:12-22

I have a starter pipeline with one task which is using the Azure CLI: az pipelines variable-group variable create. My whole script looks like this:

steps:
  - bash: |
      az pipelines variable-group variable create \
        --group-id 113 \
        --name envName \
        --value ${{parameters.envName}} \
        --org $(System.CollectionUri) \
        --project $(System.TeamProject)
    displayName: 'Add variables to group'
    env:
      AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

After running this command, the variable now exists in my variable group. However, I was wondering if there was a way to add more than one variable at a time? For example:

steps:
  - bash: |
      az pipelines variable-group variable create \
        --group-id 113 \
        --name location \ # First variable
        --value ${{parameters.location}} \
        --name envName \ # Second variable
        --value ${{parameters.envName}} \
        --org $(System.CollectionUri) \
        --project $(System.TeamProject)
    displayName: 'Add variables to group'
    env:
      AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

When I run this script with the additional variable, the first variable is overwritten by the next variable. I'm looking into creating a loop that will iterate over all of my parameters to then be passed into my script. Something on the lines of:

steps:
  ${{each parameter in parameters}}:
    - bash: |
        az pipelines variable-group variable create \
          --group-id 113 \
          --name ${{parameter.Key}} \
          --value ${{parameters.Value}} \
          --org $(System.CollectionUri) \
          --project $(System.TeamProject)
      displayName: 'Add variables to group'
      env:
        AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

This last script gives me A mapping was not expected.

Is there a way to use the script to add more than one variable to an already existing variable group?

CodePudding user response:

Your each is fine, I just fixed small syntax and it works:

parameters:
  - name: test1
    displayName: test1
    type: string
    default: "Test-1"
  - name: test2
    displayName: test2
    type: string
    default: "Test-2"
    
jobs:
- job: VG
  steps: 
  - ${{ each parameter in parameters }}:
    - bash: |
        az pipelines variable-group variable create \
          --group-id 113 \
          --name ${{parameter.Key}} \
          --value ${{parameter.Value}} \
          --org $(System.CollectionUri) \
          --project $(System.TeamProject)
      displayName: 'Add variable ${{parameter.Key}} to group'
      env:
        AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)

Results:

enter image description here

enter image description here

  • Related