Author : MD TAREQ HASSAN | Updated : 2021/10/26
Ways to Define Variables
- In yaml file (``)
- In pipeline settings UI
Define variable in yaml file
variables:
foo: xxx
bar: yyy
# ... ... ...
Define variable in pipeline settings UI
Variable Types
- User-defined variables: created in yaml file or in UI settings
- System-defined (pre-defined) variables: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#system-variables
Injection of Variables as Environment Variable
- Variables and “variables in Variable Group” will be injected as environment variables into the pipeline tasks/scripts
- Automatically inserted into the process environment
- Translation:
- The name is upper-cased, and the “
.
” is replaced with the “_
” - Example:
- “
foo.bar
” will be “FOO_BAR
” - In PowerShell Script task, use:
$env:FOO_BAR
- “
- The name is upper-cased, and the “
Variable Syntax
Defining variables
- Syntax 1: variables keyword is followed by a list of key-value pairs
- Syntax 2: the variables keyword takes a list of variable specifiers
- useful when you want to use variable templates or variable groups
- this syntax should be used at the root level of a pipeline
- name for a regular variable, group for a variable group, and template to include a variable template
Syntax 1 (define variable)
variables:
foo: xxx
bar: yyy
# ... ... ...
Syntax 2 (define variable)
variables:
- name: myvariable # a regular variable
value: myvalue
- group: myvariablegroup # a variable group
- template: myvariabletemplate.yml # a reference to a variable template
# ... ... ...
Usage of Variables
$(var)
- Called “macro syntax”
- Macro syntax is designed to interpolate variable values into task inputs and into other variables
$[variables.var]
- Called “Runtime expression syntax”
- Runtime expressions are designed to be used in the conditions of jobs, to support conditional execution of jobs, or whole stages
$
- Called “Template expression syntax”
- Template variables are processed at compile time, and are replaced before runtime starts
- Template expressions are designed for reusing parts of YAML as templates.
- Deatils: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#runtime-expression-syntax
variables:
- group: demo-vg # has varibales: foo-one & foo-two
jobs:
- job: DemoJob
displayName: Demo Job
continueOnError: false
steps:
- task: DemoTask@1
inputs:
# ... ... ...
env:
FOO_ONE: $(foo-one)
FOO_TWO: $(foo-two)
# ... ... ...
Variable Scope
- Variables can be defined at different scope i.e. at root level (pipeline/yaml file), stage level, job level etc.
- Variable at lower level will override variable with same name in higher level
# Variable at yaml file / pipeline scope
variables:
- name: fooVar
value: Foo
stages
- stage: BuildStage
displayName: Demo Build Stage
jobs
- job: DemoJob
displayName: Demo Job
variables: # Variable at job scope
- name: barVar
value: Bar
Expansion of Variables
Links
- Define variables: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml
- Predefined variables: https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
- Classic release and artifacts variables: https://docs.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch