aseb
aseb

Reputation: 352

YAML Variables, can you reference variables within variables?

I am using a variables.yml file as a template to store different variables, and I was curious if I am able to reference variables within the yml file itself to essentially nest them.

For example:

#variables.yml file
variables:
  food1:        'pineapple'
  food2:        'pizza'
  favoriteFood: '$(food1) $(food2)'

So that when I eventually call upon this variable "favoriteFood", I can just use ${{ variables.favoriteFood }} and the value should be "pineapple pizza" Example:

#mainPipeline.yml file
variables: 
-  template: 'variables.yml'
steps: 
-  script: echo My favorite food is ${{ variables.favoriteFood }}.

Am I on the right track here? I can't seem to google to any examples of if this is possible.

Upvotes: 12

Views: 17996

Answers (2)

tmaj
tmaj

Reputation: 35135

Here's an example for your scenario from the official
Learn/Azure/Azure DevOps/Azure Pipelines/Expressions:

variables:
  staticVar: 'my value' # static variable
  compileVar: ${{ variables.staticVar }} # compile time expression
  isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')] # runtime expression

steps:
  - script: |
      echo ${{variables.staticVar}} # outputs my value
      echo $(compileVar) # outputs my value
      echo $(isMain) # outputs True

Upvotes: 2

aseb
aseb

Reputation: 352

Yes! It is in fact possible, just follow the syntax outlined above. Don't forget spacing is critical in YML files.

Upvotes: 4

Related Questions