how can I use IF ELSE in variables of azure DevOps yaml pipeline with variable group?

kevmando picture kevmando · Nov 12, 2019 · Viewed 25.3k times · Source

I'm trying to assign one of 2 values to a variable in addition to variable group and can't find the reference that how to use IF ELSE.

Basically I need to convert this jerkins logic to azure DevOps.

Jenkins

if (branch = 'master') { 
   env = 'a'
} else if (branch = 'dev'){
    env ='b'
}

I found 1 reference from the following one, but this one seems to work if the variables section doesn't have variable groups.

https://stackoverflow.com/a/57532526/5862540

But in my pipeline, I already have a variable group for secrets, so I have to use name/value convention and the example doesn't work with the errors like expected a mapping or A mapping was not expected or Unexpected value 'env'

variables:
- group: my-global
- name: env
  value:
    ${{ if eq(variables['Build.SourceBranchName'], 'master') }}: 
      env: a
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}: 
      env: b

or

variables:
- group: my-global
- name: env
  value:
    ${{ if eq(variables['Build.SourceBranchName'], 'master') }}: a
    ${{ if eq(variables['Build.SourceBranchName'], 'dev') }}: b

Answer

vi7a picture vi7a · Sep 22, 2020

This code works.
I'm doing similar with parameters.

variables:
  - name: var1
    ${{ if eq(parameters.var1, 'custom') }}:
      value: $(var1.manual.custom)
    ${{ if ne(parameters.var1, 'custom') }}:
      value: ${{ parameters.var1 }}