Skip to content
Latchkey

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: Build

Gotchas

  • A variable set with task.setvariable is available in later steps, not the step that sets it.
  • To use a set variable in another job, add isOutput=true and reference it via the job's dependencies output.
  • Mark secret variables with issecret=true (or define them in the UI/library) so they are masked in logs.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →