6

Am working on Azure DevOps templates lately. And came across this double curly braces syntax. Just want to know how to use double curly braces in Azure DevOps.

Have seen some posts regarding the same.

  1. Assign conditional value to variables in Azure DevOps

  2. Curly braces in Yaml files

  3. If else in Azure DevOps

  4. Conditional Variable Assignment in Azure DevOps

So lets say, I have a variable defined in a group as follows.

Defining Variable Group in Azure DevOps

Also we can define variables as follows in a yaml file.

variables:
- name: BuildConfiguration
  value: 'Release'

- name: finalBuildArtifactName
  value: 'TempFolderName'

When should we use the double curly brace syntax?

I have idea about and using the following ways to reference variables.

  1. variables['MyVar']
  2. variables.MyVar

What can we accomplish with double curly braces in contrast to the above two?

Its very difficult to get things working in a yaml pipeline. Do the change, checkin, run the pipeline, see the result and circle back again. This is highly time consuming, and not smooth to say the least.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
VivekDev
  • 20,868
  • 27
  • 132
  • 202

1 Answers1

6

${{}} syntax is used for expressions. What's more there is another $[ <expression> ]. And here is the difference:

# Two examples of expressions used to define variables
# The first one, a, is evaluated when the YAML file is compiled into a plan.
# The second one, b, is evaluated at runtime.
# Note the syntax ${{}} for compile time and $[] for runtime expressions.
variables:
  a: ${{ <expression> }}
  b: $[ <expression> ]

Please check documentation here.

And as you may see here

steps:
- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)'
    ${{ if eq(variables['Build.SourceBranchName'], 'main') }}:
      artifact: 'prod'
    ${{ else }}:
      artifact: 'dev'
    publishLocation: 'pipeline'

you can use variables['Build.SourceBranchName'] syntax for accessing variables. But,

variables:
  - name: foo
    value: fabrikam # triggers else condition

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: echo "start"
- ${{ if eq(variables.foo, 'adaptum') }}:
  - script: echo "this is adaptum"
- ${{ elseif eq(variables.foo, 'contoso') }}:
  - script: echo "this is contoso"
- ${{ else }}:
  - script: echo "the value is not adaptum or contoso"

You may also use variables.foo.

Expressons are often used for conditional evaluation, dynamic steps/jobs/stages configuration etc.

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