Azure Pipelines: an output variable is empty in the next job or stage
A script sets a variable with echo "##vso[task.setvariable variable=myVar]...", and the next job or stage reads nothing, or the text $(myVar) itself: a macro whose variable does not exist stays as it is. By default the variable reaches only the later steps of the same job. Each step further away needs one more thing, and a missing one gives an empty value, not an error.
Set it once, with a step name
- job: A
steps:
- bash: echo "##vso[task.setvariable variable=myVar;isOutput=true]42"
name: setVar
isOutput=truemakes it an output variable, which other jobs and stages can read.name:(notdisplayName:) is what every expression below calls the step, so the step that sets the variable needs one.
When that pipeline is a pull request's build validation: Azure DevOps Pull Requests, a plugin for JetBrains IDEs, lists the pull request's branch policies with their state in IntelliJ IDEA, Rider or PyCharm, and with Pro runs a failed build policy again once the pipeline is fixed. Its Pipelines tab (version 1.0.86 and later) lists the runs of the repositories you have open and opens a failed task's log in the editor, free to read.
Read it from where you are
| Where you read it | What you write |
|---|---|
| A later step, same job | $(setVar.myVar) (without isOutput: $(myVar)) |
| Another job, same stage | dependsOn: A and, under variables:, v: $[ dependencies.A.outputs['setVar.myVar'] ] |
| A job in another stage | The stage dependsOn the first one, and v: $[ stageDependencies.One.A.outputs['setVar.myVar'] ] |
| A stage's condition | eq(dependencies.One.outputs['A.setVar.myVar'], '42'): the job name comes inside the quotes |
| From a deployment job (runOnce) | dependencies.A.outputs['A.setVar.myVar']: the job name twice; with a resource, 'Deploy_VM1.setVar.myVar' |
From Microsoft's pages on setting variables in scripts, variables, expressions and deployment jobs. Stages and jobs here are named One and A.
Why it is still empty
- No dependsOn. A job or stage reads output variables only from jobs and stages it depends on. Jobs in one stage run without a dependency unless you write one; stages depend only on the stage just before them by default.
- A stage further down. Microsoft's page says output variables are available in the next downstream stage. A stage two steps away needs its own
dependsOnon the stage that set the variable. ${{ }}instead of$[ ]. Template expressions are expanded before the run starts, before any script has set anything, and a missing value becomes an empty string. Map the variable with a runtime expression$[ ]undervariables:, then use$(v)in the steps.- The same step.
$(myVar)is replaced before a step starts, so the step that sets the variable still sees the old value. Read it in the next step. - A matrix or parallel job. The name of the output variable changes with the job's configuration. Microsoft suggests printing the variables with a
- script: envstep to see the name.
Similar pages
- "The step name X appears more than once": the step names that output variables are read through, in a template used twice.
- "A template expression is not allowed in this context": which expression is allowed where.
Azure DevOps pull requests in IntelliJ IDEA, Rider and PyCharm
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, reads the organization, project and repository from your git remote and lists the pull requests that wait for your review in an IDE tool window, on Azure DevOps Services and on Azure DevOps Server. It shows the branch policies of a pull request with their state, so a required build that has not passed is next to its pull request, and with Pro you run a failed or expired build policy again from there. The review inbox is free; voting, commenting, completing and creating are Pro, with a 30-day trial.
Get it on JetBrains Marketplace
Azure DevOps and Azure Pipelines are trademarks of Microsoft Corporation; IntelliJ IDEA, Rider and PyCharm are trademarks of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: ##_vso[ in the log · The step name appears more than once · A template expression is not allowed in this context · Build validation · 日本語