Set variables in scripts

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018

When you use PowerShell and Bash scripts in your pipelines, it's often useful to be able to set variables that you can then use in future tasks. Scripts are great for when you want to do something that isn't supported by a task like calling a custom REST API and parsing the response.

You'll use the task.setvariable logging command to set variables in PowerShell and Bash scripts.

About task.setvariable

When you add a variable with task.setvariable, the following tasks can use the variable using macro syntax $(myVar). The variable will only be available to tasks in the same stage by default. If you add the parameter isoutput, the syntax to call your variable changes. See Set an output variable for use in the same job.

Set the variable myVar with the value foo.

- bash: |
    echo "##vso[task.setvariable variable=myVar;]foo"

Read the variable myVar:

- bash: |
    echo "You can use macro syntax for variables: $(myVar)"

Set variable properties

The task.setvariable command includes properties for setting a variable as secret, as an output variable, and as read only. The available properties include:

  • variable = variable name (Required)
  • issecret = boolean (Optional, defaults to false)
  • isoutput = boolean (Optional, defaults to false)
  • isreadonly = boolean (Optional, defaults to false)

To use the variable in the next stage, set the isoutput property to true. To reference a variable with the isoutput set to true, you'll include the task name. For example, $(TaskName.myVar).

When you set a variable as read only it can't be overwritten by downstream tasks. Set isreadonly to true. Setting a variable as read only enhances securing by making that variable immutable.

Set a variable as secret

When issecret is set to true, the value of the variable will be saved as secret and masked out from log.

Set the secret variable mySecretVal.

- bash: |
    echo "##vso[task.setvariable variable=mySecretVal;issecret=true]secretvalue"

Get the secret variable mySecretVal.

- bash: |
    echo "##vso[task.setvariable variable=mySecretVal;issecret=true]secretvalue"
- bash: |
    echo $(mySecretVal)

Secret variable output in bash.

Output of bash variable.

Levels of output variables

There are four different types of output variables with distinct syntaxes:

Set an output variable for use in the same job

When you use an output variable in the same job, you do not have to use the isoutput property. By default, the variable will be available to downstream steps within the same job. However, if you do add the isoutput property, you'll need to reference the variable with the task name.

The script below sets the same-job output variable myJobVar without specifying isoutput and sets myOutputJobVar with isoutput=true.

jobs:
- job: A
  steps:
  - bash: |
     echo "##vso[task.setvariable variable=myJobVar]this is the same job"
  - bash: |
     echo "##vso[task.setvariable variable=myOutputJobVar;isoutput=true]this is the same job too"
    name: setOutput

This script gets the same-job variables myJobVar and myOutputJobVar. Notice that the syntax changes for referencing an output variable once isoutput=true is added.

jobs:
- job: A
  steps:
  - bash: |
     echo "##vso[task.setvariable variable=myJobVar]this is the same job"
  - bash: |
     echo "##vso[task.setvariable variable=myOutputJobVar;isoutput=true]this is the same job too"
    name: setOutput
  - bash: |
     echo $(myJobVar) 
  - bash: |
     echo $(setOutput.myOutputJobVar)

Set an output variable for use in future jobs

When you use output variables across jobs, you'll reference them with dependencies. The syntax for accessing an output variable in a future job or stage varies based on the relationship between the setter and consumer of the variable. Learn about each case in dependencies.

First, set the output variable myOutputVar.

jobs:
- job: A
  steps:
  - bash: |
     echo "##vso[task.setvariable variable=myOutputVar;isoutput=true]this is from job A"
    name: passOutput

Next, access myOutputVar in a future job and output the variable as myVarFromJobA. To use dependencies, you need to set the dependsOn property on the future job using the name of the past job in which the output variable was set.

jobs:
- job: A
  steps:
  - bash: |
     echo "##vso[task.setvariable variable=myOutputVar;isoutput=true]this is from job A"
    name: passOutput
- job: B
  dependsOn: A
  variables:
    myVarFromJobA: $[ dependencies.A.outputs['passOutput.myOutputVar'] ]  
  steps:
  - bash: |
     echo $(myVarFromJobA)

Set an output variable for use in future stages

Output variables can be used across stages in pipelines. This helps you pass useful information, such as the ID of a generated output, from one stage to the next.

When you set a variable with the isoutput property, you can reference that variable in later stages with the task name and the stageDependencies syntax. Learn more about dependencies.

First, set the output variable myStageVal.

steps:
    - bash: echo "##vso[task.setvariable variable=myStageVal;isOutput=true]this is a stage output variable"
      name: MyOutputVar

Then, in a future stage, map the output variable myStageVal to a stage, job, or task-scoped variable as, for example, myStageAVar. Note the mapping syntax uses a runtime expression $[] and traces the path from stageDependencies to the output variable using both the stage name (A) and the job name (A1) to fully qualify the variable.

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - bash: echo "##vso[task.setvariable variable=myStageVal;isOutput=true]this is a stage output variable"
       name: MyOutputVar
- stage: B
  dependsOn: A
  jobs:
  - job: B1
    variables:
      myStageAVar: $[stageDependencies.A.A1.outputs['MyOutputVar.myStageVal']]
    steps:
      - bash: echo $(myStageAVar)