You can define variables and their scope in Azure DevOps pipeline. Variables give you a convenient way to get key bits of data into various parts of the pipeline. The most common use of variables is to define a value that you can then use in your pipeline. All variables are stored as strings and are mutable. The value of a variable can change from run to run or job to job of your pipeline.
The following pipeline can be used to understand the scope of variables defined at root, stage and job level.
In the YAML file, you can set a variable at various scopes:
- At the root level, to make it available to all jobs in the pipeline.
- At the stage level, to make it available only to a specific stage.
- At the job level, to make it available only to a specific job.
When a variable is defined at the top of a YAML, it will be available to all jobs and stages in the pipeline and is a global variable. Global variables defined in a YAML are not visible in the pipeline settings UI.
Variables at the job level override variables at the root and stage level. Variables at the stage level override variables at the root level. A variable set in the pipeline root level will override a variable set in the Pipeline settings UI.
trigger: - master pool: name: Self-Hosted-Agent variables: - name: globalVariable value: "This is root level variable" - name: overrideGlobalVariable value: "ROOT" - name: overrideGlobalandStageVariable value: "Global" stages: - stage: displayName: "Stage 1" variables: stageVariable: "This is Stage 1 level variable" overrideGlobalVariable: "STAGE 1" overrideGlobalandStageVariable: "Stage 1" jobs: - job: BuildAndTest variables: - name: jobVariable value: "This is Job level variable in Stage 1" - name: sameNameVariable value: "This overrides pipeline variable" - name: overrideGlobalandStageVariable value: "Job" workspace: clean: all steps: - task: Bash@3 inputs: targetType: 'inline' script: | # Write your commands here echo ${{ variables.sameNameVariable }} echo "globalVariable :" $(globalVariable) echo "stageVariable :" $(stageVariable) echo "jobVariable :" $(jobVariable) echo "overrideGlobalVariable in Stage :" $(overrideGlobalVariable) echo "overrideGlobalandStageVariable in Job :" $(overrideGlobalandStageVariable) echo "Override pipelineVariable :" $(sameNameVariable) - stage: displayName: "Stage 2" variables: stageVariable: "This is Stage 2 level variable" overrideGlobalVariable: "STAGE 2" overrideGlobalandStageVariable: "Stage 2" jobs: - job: BuildAndTest variables: - name: jobVariable value: "This is Job level variable in Stage 2" - name: overrideGlobalandStageVariable value: "Stage2 -Job" workspace: clean: all steps: - task: Bash@3 inputs: targetType: 'inline' script: | # Write your commands here echo ${{ variables.sameNameVariable }} echo "globalVariable :" $(globalVariable) echo "stageVariable :" $(stageVariable) echo "jobVariable :" $(jobVariable) echo "overrideGlobalVariable in Stage :" $(overrideGlobalVariable) echo "overrideGlobalandStageVariable in Job :" $(overrideGlobalandStageVariable) echo "Get pipelineVariable as is:" $(sameNameVariable)
In this YAML pipeline, Root level variables are
variables: - name: globalVariable value: "This is root level variable" - name: overrideGlobalVariable value: "ROOT" - name: overrideGlobalandStageVariable value: "Global"
Stage level variables are
variables: stageVariable: "This is Stage 1 level variable" overrideGlobalVariable: "STAGE 1" overrideGlobalandStageVariable: "Stage 1"
and
variables: stageVariable: "This is Stage 2 level variable" overrideGlobalVariable: "STAGE 2" overrideGlobalandStageVariable: "Stage 2"
Job level variables are
variables: - name: jobVariable value: "This is Job level variable in Stage 1" - name: sameNameVariable value: "This overrides pipeline variable" - name: overrideGlobalandStageVariable value: "Job"
and
variables: - name: jobVariable value: "This is Job level variable in Stage 2" - name: overrideGlobalandStageVariable value: "Stage2 -Job"
variable set in the Pipeline settings UI
variables: - name: sameNameVariable value: "This overrides pipeline variable"
Reference:
https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch