How to Set a Variable in Azure Pipelines
Azure Pipelines sets static variables in a variables: block and dynamic ones via the ##vso[task.setvariable] logging command.
Use variables: for static values and reference them as $(NAME). To compute a value in a script and use it in later steps, emit the task.setvariable logging command.
Static and runtime variables
Static variables: are read as $(NODE_ENV); the runtime one is set with a logging command and marked isOutput to cross jobs.
azure-pipelines.yml
variables:
NODE_ENV: production
steps:
- script: |
SHORT=$(git rev-parse --short HEAD)
echo "##vso[task.setvariable variable=GIT_SHA]$SHORT"
displayName: Compute SHA
- script: echo "Building $(GIT_SHA) in $(NODE_ENV)"
displayName: BuildGotchas
- A variable set with
task.setvariableis available in later steps, not the step that sets it. - To use a set variable in another job, add
isOutput=trueand reference it via the job'sdependenciesoutput. - Mark secret variables with
issecret=true(or define them in the UI/library) so they are masked in logs.
Related guides
How to Set an Environment Variable in GitHub ActionsSet environment variables in GitHub Actions at the workflow, job, or step level with env:, and export dynamic…
How to Use Secrets in Azure PipelinesUse secrets in Azure Pipelines safely - secret pipeline variables, variable groups linked to Key Vault, and m…