5

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?

Community
  • 1
  • 1
Bino
  • 320
  • 3
  • 14

3 Answers3

9

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
4

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.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • It doesn't work for me. What could I do wrong? ============================================================================== Generating script. Script contents: echo $(groupname) ========================== Starting Command Output =========================== /bin/bash --noprofile --norc /home/vsts/work/_temp/f1c09eb7-1ef2-43ab-b0e6-7a4905c67b8d.sh /home/vsts/work/_temp/f1c09eb7-1ef2-43ab-b0e6-7a4905c67b8d.sh: line 1: groupname: command not found Finishing: Bash – Krzysztof Madej Apr 02 '20 at 08:50
  • @Krzysztof, does **QA** and **PROD** variable groups has been created in your **Library**? – Mengdi Liang Apr 02 '20 at 08:54
  • Ok. So groupname is a variable inside a group, right? It is not a built in variable :) All is ok. I made mistake assuming that groupname is a built in variable containing used groups. – Krzysztof Madej Apr 02 '20 at 08:56
  • @Krzysztof Nope,QA and PROD are just variable group names, they are not stored in any group. I mean you need make this variable group has been created like this: https://imgur.com/a/rr7z1sX – Mengdi Liang Apr 02 '20 at 08:59
  • @ Merlin Liang - MSFT yes I know. My mistake was that I didn't create a groupname variable inside a groups QA and PROD. All is fine. Thanks a lot! – Krzysztof Madej Apr 02 '20 at 09:01
  • I managed to go with @Krzysztof answer, for the team it was better to use the parameters. Thank you for the comment! – Bino Apr 03 '20 at 13:03
  • @Bino It’s okay. It would be much better of you could accept Krzysztof’s answer:-) – Mengdi Liang Apr 03 '20 at 13:16
  • Is it possible to achieve the same thing with classic (non-YAML) pipeline ? – LiohAu Sep 09 '21 at 08:15
1

I did something similar at the global level to dynamically set the variable group.

variables:
    variables:
    - ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/release') }}:
      - group: Staging
    - ${{ elseif eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
      - group: Production
    - ${{ else }}:
      - group: Development

also added a variable in the variable group to identify which one was being set, then displayed using the below script:

- script: |
    echo $(VARIABLE_GROUP_NAME)
  displayName: 'Selected Variable Group
Davia
  • 73
  • 10