0

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?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
ASH
  • 573
  • 2
  • 7
  • 20

2 Answers2

0

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:
GeralexGR
  • 2,973
  • 6
  • 24
  • 33
  • While this would work, it doesn't solve my requirement to have a single "switch" where I could failover all pipelines to use private rather hosted agent pools. What I don't understand is why I can't use values from a variable group to do what you've illustrated above... – ASH Feb 23 '22 at 15:23
  • You can use variables groups normally. Try to switch to `${{ if eq($(variableNAMEfromGROUP), 'true') }}:` – GeralexGR Feb 23 '22 at 15:25
  • That doesn't appear to be valid syntax – ASH Feb 24 '22 at 10:40
0

I think it's just the syntax error, can you try this?

variables:
  - group: my-variable-group

stages:
  - stage: deploy
    pool:
      ${{ if eq(variables.UseHostedAgents, true) }}:
         vmImage: ubuntu-latest
      ${{ else }}:
         name: private-pool
    jobs:
     ...
4hbane
  • 139
  • 1
  • 10