Set variables in scripts

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

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

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

Virtually job.setvariable

When you add a variable with task.setvariable, the post-obit tasks can apply the variable using macro syntax $(myVar). The variable will only exist 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 chore.

  • Fustigate
  • PowerShell

Fix the variable myVar with the value foo.

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

Read the variable myVar:

                  - fustigate: |     repeat "You can use macro syntax for variables: $(myVar)"                                  

Set variable properties

The task.setvariable command includes properties for setting a variable as hole-and-corner, every bit an output variable, and as read merely. The available backdrop 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 adjacent phase, set the isoutput property to true. To reference a variable with the isoutput set to true, yous'll include the task name. For case, $(TaskName.myVar).

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

Gear up a variable as clandestine

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

  • Bash
  • PowerShell

Set the secret variable mySecretVal.

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

Go the secret variable mySecretVal.

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

Hole-and-corner variable output in bash.

Output of bash variable.

Levels of output variables

In that location are 4 dissimilar types of output variables with distinct syntaxes:

  • Output variables set in the same job without the isoutput parameter. To reference these variables, you'll utilise macro syntax. Instance: $(myVar).
  • Output variables set up in the same job with the isoutput parameter. To reference these variables, you'll include the chore name. Example: $(myTask.myVar).
  • Output variables set in a hereafter job. To reference these variables, you'll reference the variable in the variables section with dependency syntax.
  • Output variables set in hereafter stages. To reference these variables, you'll reference the variable in the variables section with stageDependencies syntax.

Ready an output variable for employ in the same task

When you lot use an output variable in the same job, you do non have to utilize the isoutput property. Past default, the variable will be available to downstream steps inside the same chore. However, if you do add the isoutput property, you'll need to reference the variable with the task name.

  • Fustigate
  • PowerShell

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

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

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

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

Set an output variable for apply in futurity jobs

When you apply 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 human relationship between the setter and consumer of the variable. Learn about each case in dependencies.

  • Bash
  • PowerShell

First, ready 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 time to come job and output the variable as myVarFromJobA. To use dependencies, you need to gear up the dependsOn holding on the hereafter chore using the proper name of the by task in which the output variable was prepare.

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

Set an output variable for utilize in time to come stages

Output variables can be used across stages in pipelines. This helps you pass useful information, such every bit the ID of a generated output, from one phase to the adjacent.

When y'all set a variable with the isoutput property, you tin reference that variable in later stages with the task name and the stageDependencies syntax. Learn more nearly dependencies.

  • Bash
  • PowerShell

First, ready the output variable myStageVal.

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

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

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