Azure Devops: How to set group variable with if statement

Viewed 5563

I'm trying to set the variable group according to one varible that exists in the pipeline. The yaml looks like this:

enter image description here

But i'm getting the following error when i'm running the pipeline:

enter image description here

If i remove the "- group : QA" or "- group : PROD" the pipeline runs without any problem. What am I doing wrong?

2 Answers

This is slightly different solution but you may achieve your goal - which is if I understood conditional selection of variable group.

You can use runtime parameters:

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

stages:
- stage:
  displayName: 'Build and Restore'
  variables:
  - group: ${{ parameters.environment }}
  jobs:
    - job:
      steps:
      - script: echo $(name)

than running a build you can select your envrionment:

Running a build

Note: I have defined two variable groups QA and PROD with variable name in both groups.

Try with below schema:

variables:
  isProd: true

stages:
- stage: Test
  displayName: Build and restore
  variables:
  - ${{ if eq(variables['isProd'], 'false') }}:
    - group: QA
  - ${{ if eq(variables['isProd'], 'true') }}:
    - group: PROD
  jobs:
  - job: A
    steps:
    - bash: echo $(groupname)

enter image description here

Note: You may receive some red warning when you defining above YAML scipt. Ignore that confused warning and continue to run it.

Related