Author : MD TAREQ HASSAN | Updated : 2021/10/26

Ways to Define Variables

Define variable in yaml file


variables:
  foo: xxx
  bar: yyy
  
# ... ... ...

Define variable in pipeline settings UI

Azure DevOps - Define variable in pipeline settings UI

Variable Types

Injection of Variables as Environment Variable

Variable Syntax

Defining variables

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

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

# 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

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#expansion-of-variables