'How to use variable group as a runtime parameter in azure devops yml

I would like to pass the variable group as a runtime parameter so that whenever I run the pipeline, it should allow me to provide the input variable group name, and based on the input value for the variable group during runtime my pipeline should proceed.

I want to achieve this when we click on the run button, then there's a variable section also. So, I want you to accept the variable group names from there.

Pipeline.yml:

stages:
  - stage: VMBackupandValidate
    displayName: 'VM Backup and Validate using RSV'
    jobs:
      - job: VMBackupValidate
        displayName: 'Azure VM Backup'
        steps:
          - task: AzurePowerShell@5
            inputs:
              azureSubscription: $(azure_sc)
              ScriptType: 'FilePath'
              ScriptPath: 'pipelines/automation/scripts/vmbackup.ps1'
              ScriptArguments: '-ResourceGroupName $(ResourceGroupName) -Storagetype $(Storagetype) -SourceVMname $(SourceVMname) -RSVaultname $(RSVaultname) -Location $(Location) -WorkLoadType $(WorkLoadType) -Policyname $(Policyname) -Verbose'
              azurePowerShellVersion: 'LatestVersion'
              pwsh: true


Solution 1:[1]

Based on comments communication with OP.

I suggest using a parameter with a default value. It will ask you for input if want other values, before you hit run then make a condition to select the right variable based on input.

Here is a minified sample of the pipeline:

parameters:
- name: environment
  displayName: Deploy Environment
  type: string
  default: TEST
  values:
  - TEST
  - PROD

trigger:
- 'none'

variables:
- name: environment
  ${{ if contains(parameters.environment, 'TEST') }}:
    value: TEST
  ${{ if contains(parameters.environment, 'PROD') }}:
    value: PROD

stages: 
- stage: TEST
  displayName: Build
  condition: ${{ eq(variables.environment, 'TEST') }}
  jobs:
  - job:
    pool:
      vmImage: 'ubuntu-20.04'
    steps:
    - script: |
        echo ${{ variables.environment}}
      displayName: 'Print environment info'

You can extend the logic, or replace it with other values and consume it in code later. You can create multiple stages with conditions as well as shown.

Solution 2:[2]

Lets say you have two variable groups with names prod and test. You could use the below pipeline:

  trigger:
- main

parameters:
  - name: environment
    displayName: Where to deploy?
    type: string
    default: test
    values:
    - prod
    - test


pool:
  vmImage: ubuntu-latest

variables:
 - group: ${{parameters.environment}}

steps:

- script: |
    echo $(ENV)
    echo $(VERSION)
  displayName: Step 1 - print version and environment

- script: pwd ENV ${{parameters.environment}}
  displayName: Step 2 - print parameter

You should define ENV, VERSION values on both variable groups.

Your stage should stay as is. In your case you will delete the steps I provided and use only the first part of the pipeline

Adding a reference article.

https://blog.geralexgr.com/azure/deploy-between-different-environments-with-variable-groups-azure-devops?msclkid=002b01eab02f11ec8dffa95dc3a34094

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 GeralexGR