Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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  
like image 778
kevmando Avatar asked Nov 12 '19 16:11

kevmando


People also ask

How do you access variables from variable group in Azure DevOps?

To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.

How do you add multiple conditions in Azure pipeline?

You could add two same tasks in the pipeline, one with the condition ((Var1==A || Var1==B || Var1==C) && (Var2==2)) and another with condition ((Var1==A) &&(Var2==1)) , this should be work.

How do you pass variables in Azure Pipelines YAML tasks?

Passing variables between tasks in the same jobSet the value with the command echo "##vso[task. setvariable variable=FOO]some value" In subsequent tasks, you can use the $(FOO) syntax to have Azure Pipelines replace the variable with some value.


1 Answers

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 }} 
like image 85
vi7a Avatar answered Sep 29 '22 07:09

vi7a