Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Conditional Variable Assignment be Done in Azure Pipelines?

Azure Pipelines has Expressions and Conditions, but I can find no way to assign one of two values to a variable, based on a condition.

Is there any way to accomplish what this pseudo-code would?

    ${{ if endsWith( variables['Build.SourceBranchName'], '/master' ) }}: 
      buildVersion: variables['mavenVersion']
    ${{ else }}: 
      buildVersion: variables['Build.SourceBranchName']
like image 298
Mike Murray Avatar asked Aug 16 '19 23:08

Mike Murray


People also ask

How do you pass variables in Azure pipelines Yml 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.

How do you use variables in Azure DevOps release pipeline?

You define and manage these variables in the Variables tab in a release pipeline. In the Pipeline Variables page, open the Scope drop-down list and select "Release". By default, when you add a variable, it is set to Release scope. Share values across all of the tasks within one specific stage by using stage variables.


3 Answers

As an extension to @Mike Murray's answer, if you are using variable groups you must define additional variables as name value pairs. To use conditional variable assignment in this case would be as follows:

variables:
- group: 'my-variable-group'
- name: myfirstadditionalvariable
  value: 100
- name: myconditionalvariable
  ${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: masterBranchValue
  ${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}: 
    value: featureBranchValue
like image 123
Evan Citulsky Avatar answered Oct 17 '22 16:10

Evan Citulsky


I was closer than I thought. This is not pretty, but it worked. (with more yaml context)

variables:
  ${{ if eq( variables['Build.SourceBranchName'], 'master' ) }}: 
    buildVersion: ${{ variables['mavenVersion'] }}
  ${{ if ne( variables['Build.SourceBranchName'], 'master' ) }}: 
    buildVersion: ${{ variables['Build.SourceBranchName'] }}

  buildKey: ${{ format('{0}_{1}', variables['supportReleaseNumber'], variables['buildVersion']) }}
  buildNum: $[counter(variables['buildKey'], 1)]  # same as $(Rev:r), but more widely usable 

name: $(buildKey)_$(buildNum)  # build run name
like image 38
Mike Murray Avatar answered Oct 17 '22 17:10

Mike Murray


This should do the trick....

BuildVersion is initialised as $(Build.SourceBranch) if it's the master branch you change that to the $(mavenVersion) else no change.

variables:
  mavenVersion: '1.0'
  buildVersion: $(Build.SourceBranch)

pool:
  vmImage: 'ubuntu-latest'

steps:

- script: echo '##vso[task.setvariable variable=buildVersion]$(mavenVersion)'
  displayName: "Set the buildVersion as mavenVersion if the Build.SourceBranch = 'refs/heads/master' "
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')

- script: echo $(buildVersion)
  displayName: 'Printing the variable'

non-master branches prints 'refs/heads/branch_name' which is mavenVersion non-master branches prints 'refs/heads/branch_name' which is mavenVersion

master branch prints 1.0 which is mavenVersion master branch prints 1.0 which is mavenVersion

like image 11
Venura Athukorala Avatar answered Oct 17 '22 16:10

Venura Athukorala