'How to use variable from variable group in conditional insertion expression in Azure DevOps pipeline

My aim is to use a variable group to hold global configuration settings which apply to all pipelines. Specifically, I want the ability to flip a switch in a variable value to switch from using hosted build agents to using private build agents instead.

I have a variable group named my-variable-group which contains a variable named UseHostedAgents. I can toggle its value between true and false.

The pipeline:

variables:
  - group: my-variable-group

stages:
  - stage: deploy
    pool:
      ${{ if eq(variables['UseHostedAgents'], 'true') }}:
        vmImage: ubuntu-latest
      ${{ else }}:
        name: private-pool
    jobs:
     ...

I can't figure out how to get this to work. It seems as though the variable group variable values aren't available in the conditional insertion expression. I've tried everything I can think of to no avail. Any ideas?



Solution 1:[1]

You could use a variable/parameter to store whether you want to run on custom agent pool or shared and use it.

Example with parameters:

parameters:
  - name: environment
    type: string
    default: private-pool

variables:
  ${{ if eq(parameters.environment, 'private-pool') }}: 
    buildPoolName: private-pool
  ${{ else }}:
    buildPoolName: ubuntu-latest

And then on your job you should use the buildPoolName

- job: 'Job 1'
  displayName: 'Build'
  pool:
    name: $(buildPoolName)
  steps:

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 GeralexGR