Last active
April 18, 2025 14:49
-
-
Save rudfoss/1df3e721f732f958688c076674f81aee to your computer and use it in GitHub Desktop.
Cheat-sheet for setting and retrieving variables in Azure DevOps yaml pipelines across different steps, jobs and stages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cheat-sheet for using dynamically defined variables between steps, jobs and stages in Azure DevOps Yaml Pipelines | |
# Documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch | |
trigger: none | |
pr: none | |
stages: | |
- stage: DefineVarStage | |
jobs: | |
- job: DefineVarJob | |
pool: | |
vmImage: "ubuntu-latest" | |
steps: | |
- script: "echo outputVar: $(DefineVar.outputVar)" | |
name: ValueBeforeDef | |
# Define the variable using vso tasks. isOutput=true must be present | |
- powershell: echo "##vso[task.setvariable variable=outputVar;isOutput=true]Value defined in DefineVarStage.DefineVarJob.DefineVar" | |
name: DefineVar # Name the step so that it can later be retrieved | |
# Usage in the same job requires $([name of step].[name of variable]) | |
- script: "echo outputVar: $(DefineVar.outputVar)" | |
name: RefVarInSameStep | |
- job: UseVarJob | |
dependsOn: DefineVarJob # Depend on the job where the variable is defined. (implicit dependency on previous job) | |
pool: | |
vmImage: "ubuntu-latest" | |
variables: | |
# Look up the job in the "dependencies" set and find the appropriate output variable | |
outputVar: $[dependencies.DefineVarJob.outputs['DefineVar.outputVar']] | |
steps: | |
# Use the local variable as any other | |
- script: "echo DefineVarJob.DefineVar.outputVar: $(outputVar)" | |
name: RefVarInOtherJob | |
- stage: UseVarStage | |
dependsOn: DefineVarStage # Depend on the stage with the job with the step that defines the variable. | |
variables: | |
# Look up the stage in the "stageDependencies" set, find the job and finally the output variable. | |
outputVar: $[stageDependencies.DefineVarStage.DefineVarJob.outputs['DefineVar.outputVar']] | |
jobs: | |
- job: UseVarStageJob | |
pool: | |
vmImage: "ubuntu-latest" | |
steps: | |
# Use the local variable as any other | |
- script: "echo DefineVarStage.DefineVarJob.DefineVar.outputVar: $(outputVar)" | |
name: RefVarInOtherStage | |
# Same as above but this time using deployments | |
- stage: DefineVarDeploymentStage | |
dependsOn: DefineVarStage | |
jobs: | |
- deployment: DefineVarDeploymentJob | |
environment: dev # For deployments the environment is required | |
pool: | |
vmImage: "ubuntu-latest" | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- script: "echo outputVar: $(DefineVar.outputVar)" | |
name: ValueBeforeDef | |
# Define the variable using vso tasks. isOutput=true must be present | |
- powershell: echo "##vso[task.setvariable variable=outputVar2;isOutput=true]Value defined in DefineVarDeploymentStage.DefineVarDeploymentJob.DefineVar2" | |
name: DefineVar2 # Name the step so that it can later be retrieved | |
# Usage in the same job requires $([name of step].[name of variable]) | |
- script: "echo outputVar: $(DefineVar2.outputVar2)" | |
name: RefVarInSameStep | |
- deployment: UseVarDeploymentJob | |
environment: dev # For deployments the environment is required | |
dependsOn: DefineVarDeploymentJob # Depend on the job where the variable is defined. (implicit dependency on previous job) | |
pool: | |
vmImage: "ubuntu-latest" | |
variables: | |
# Look up the job in the "dependencies" set and find the appropriate output variable | |
# Note that because this is a deployment, we must also prefix the dependency output with the name of the job. | |
outputVar2: $[dependencies.DefineVarDeploymentJob.outputs['DefineVarDeploymentJob.DefineVar2.outputVar2']] | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
# Use the local variable as any other | |
- script: "echo DefineVarDeploymentJob.DefineVar2.outputVar2: $(outputVar2)" | |
name: RefVarInOtherJob | |
- stage: UseVarDeploymentStage | |
dependsOn: | |
- DefineVarStage | |
- DefineVarDeploymentStage # Depend on the stage with the job with the step that defines the variable. | |
variables: | |
# Look up the stage in the "stageDependencies" set, find the job and finally the output variable. | |
# Note that because the job that defined the variables was a deployment, we must also prefix the dependency output with the name of the job. | |
outputVar: $[stageDependencies.DefineVarStage.DefineVarJob.outputs['DefineVar.outputVar']] | |
outputVar2: $[stageDependencies.DefineVarDeploymentStage.DefineVarDeploymentJob.outputs['DefineVarDeploymentJob.DefineVar2.outputVar2']] | |
jobs: | |
- deployment: UseVarDeploymentJob | |
environment: dev # For deployments the environment is required | |
pool: | |
vmImage: "ubuntu-latest" | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
# Use the local variable as any other | |
- script: "echo DefineVarStage.DefineVarJob.DefineVar.outputVar: $(outputVar)" | |
name: RefVarInOtherJobStage | |
- script: "echo DefineVarDeploymentStage.DefineVarDeploymentJob.DefineVar2.outputVar2: $(outputVar2)" | |
name: RefVarInOtherDeploymentStage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment