1

I'm trying to change the variable group name in azure devops pipeline using the code below. I have tried different expressions ${{ variables.env}}, $(env), $[eq(variables['env']. None seem to work.

enter image description here

build.yaml:

variables:
- ${{ if eq('$(env)', 'Production') }}:
  - group: Production
- ${{ if eq('$(env)', 'Staging') }}:
  - group: Staging
- ${{ if eq('$(env)', 'Test') }}:
  - group: Test
- ${{ if eq('$(env)', 'Development') }}:
  - group: Development

Could you please point me in the right direction.

4
  • where are you defining the env? in pipeline variables? Commented Mar 6, 2023 at 19:11
  • Hi @Sibtain Yes, Pipeline variable. Commented Mar 6, 2023 at 22:57
  • Ok, but the question is why are you using pipeline variables and not runtime parameters? Commented Mar 7, 2023 at 11:58
  • Also you might want to share your complete YAML so we can get the context of why exactly you want to achieve this and what can be workaround Commented Mar 7, 2023 at 12:04

2 Answers 2

2

In my case I wanted to determine proper variable group based on a value of variable defined for specific pipeline. The goal was to avoid the need to have separate yml file for each environment. First, I tried following syntax:

variables:
- ${{ if eq(variables['EnvName'], 'prod') }}:
  - group: prod

but if expression seems to be evaluated compile time and at that moment values of the variables are not yet set so it didn't work.

Instead I used one of the "build variables" which luckily are already set when if expression is processed. For me DefinitionName variable which returns the name of the build pipeline was a good candidate as I have environment specified in my pipelines' names:

enter image description here

This is what I used in the yml:

variables:
- ${{ if contains(variables['Build.DefinitionName'], 'testing') }}:
  - group: testing-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'staging') }}:
  - group: staging-env-group
- ${{ elseif contains(variables['Build.DefinitionName'], 'production') }}:
  - group: prod-env-group
Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned in this answer, you don't need the conditions to evaluate the variable group. Below is a complete working YAML that uses variables instead of parameters

trigger: none
 
variables:
- name: env
  value: 'Staging'
- group: ${{ variables.env }}

pool:
  vmImage: ubuntu-latest

jobs:
- job: Build
  steps:
  - script: echo Environment is $(env)

Demo Run:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.