Set variables in scripts - Azure Pipelines (2024)

  • Article

Azure DevOps Services | Azure DevOps Server 2022 - 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. Newly set variables aren't available in the same task.

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.

Note

Deployment jobs use a different syntax for output variables. To learn more about support for output variables in deployment jobs, see Deployment jobs.

To use a variable with a condition in a pipeline, see Specify conditions.

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 job 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.

  • Bash
  • PowerShell

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 security 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 logs.

Note

Azure Pipelines makes an effort to mask secrets when emitting data to pipeline logs, so you may see additional variables and data masked in output and logs that are not set as secrets.

  • Bash
  • PowerShell

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.

Set variables in scripts - Azure Pipelines (1)

Levels of output variables

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

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

Set an output variable for use in the same job

When you use an output variable in the same job, you don't 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.

  • Bash
  • PowerShell

The script here 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.

  • Bash
  • PowerShell

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. You can use output variables to 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.

Output variables are only available in the next downstream stage. If multiple stages consume the same output variable, use the dependsOn condition.

  • Bash
  • PowerShell

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)

In case your value contains newlines, you can escape them and the agent will automatically unescape it:

steps:- bash: | escape_data() { local data=$1 data="${data//'%'/'%AZP25'}" data="${data//$'\n'/'%0A'}" data="${data//$'\r'/'%0D'}" echo "$data" } echo "##vso[task.setvariable variable=myStageVal;isOutput=true]$(escape_data $'foo\nbar')" name: MyOutputVar

FAQ

My output variable isn't rendering. What is going wrong?

There are a few reasons why your output variable may not appear.

  • Output variables set with isoutput aren't available in the same job and instead are only available in downstream jobs.
  • Depending on what variable syntax you use, a variable that sets an output variable's value may not be available at runtime. For example, variables with macro syntax ($(var)) get processed before a task runs. In contrast, variables with template syntax are processed at runtime ($[variables.var]). You'll usually want to use runtime syntax when setting output variables. For more information on variable syntax, see Define variables.
  • There may be extra spaces within your expression. If your variable isn't rendering, check for extra spaces surrounding isOutput=true.

You can troubleshoot the dependencies output for a pipeline job or stage by adding a variable for the dependencies and then printing that variable. For example, in this pipeline job A sets the output variable MyTask. The second job (B) depends on job A. A new variable, deps holds the JSON representation of the job dependencies. The second step in Job B uses PowerShell to print out deps so that you can see the job dependencies.

trigger:- '*'pool: vmImage: 'ubuntu-latest' jobs:- job: A steps: - script: | echo "##vso[task.setvariable variable=MyTask;isOutput=true]theoutputval" name: ProduceVar - job: B dependsOn: A variables: varFromA: $[ dependencies.A.outputs['ProduceVar.MyTask'] ] deps: $[convertToJson(dependencies)] # create a variable with the job dependencies steps: - script: echo $(varFromA) # - powershell: Write-Host "$(deps)"
Set variables in scripts - Azure Pipelines (2024)

FAQs

How do I set variables in Azure pipeline? ›

You can set a variable for a build pipeline by following these steps:
  1. Go to the Pipelines page, select the appropriate pipeline, and then select Edit.
  2. Locate the Variables for this pipeline.
  3. Add or update the variable.
  4. To mark the variable as secret, select Keep this value secret.
  5. Save the pipeline.
Apr 4, 2024

Which two environment variables must you set for the script to complete successfully? ›

Final answer: For the build-agent.sh script from GitHub to run successfully while adding VM1 to Project1 as a self-hosted agent, two environment variables need to be set: AZP_URL and AZP_TOKEN.

How do I use Azure pipeline variables in PowerShell script? ›

You'll use the task. setvariable logging command to set variables in PowerShell and Bash scripts. Deployment jobs use a different syntax for output variables. To learn more about support for output variables in deployment jobs, see Deployment jobs.

How do I set parameters in Azure pipeline? ›

Option 1: Create a pipeline parameter in the settings panel
  1. Next to the name of your pipeline draft, select the gear icon to open the Settings panel.
  2. In the Pipeline parameters section, select the + icon.
  3. Enter a name for the parameter and a default value.
May 29, 2023

How do you set a variable in a variable group in Azure pipeline? ›

To use a variable group, open your pipeline. Select Variables > Variable groups, and then choose Link variable group. In a build pipeline, you see a list of available groups. In a release pipeline, for example, you also see a drop-down list of stages in the pipeline.

What is the difference between parameters and variables in Azure pipeline? ›

Pipeline variables are values that can be set and modified during a pipeline run. Unlike pipeline parameters, which are defined at the pipeline level and cannot be changed during a pipeline run, pipeline variables can be set and modified within a pipeline using a Set Variable activity.

How do you set a variable in a script? ›

To create a variable, put a line in the script that contains the name of the variable followed immediately by an equal sign ("="). No spaces are allowed. After the equal sign, assign the information to store.

How to set env variables in script? ›

To set an environment variable, use the command " export varname=value ", which sets the variable and exports it to the global environment (available to other processes). Enclosed the value with double quotes if it contains spaces. To set a local variable, use the command " varname =value " (or " set varname =value ").

What are the two types of variables in scripting? ›

System variables are typically written in all caps, and some common examples include $PATH , $HOME , and $USER . User-defined variables: These are variables that you define yourself within your shell script. They are used to store values that can be referenced and manipulated throughout the script.

How do I set a variable in Yaml? ›

The syntax for defining a variable as a content block is: define: &varblock <directive>: <value> <directive>: <value> ... <block>: - <directive>: <value> <directive>: <value> ... Any number of directives or blocks of directives can be inside the definition block.

How do I update pipeline variables in Azure DevOps? ›

You need to use the API to update a variable in a variable group. You actually can't just update a single variable, without removing all the other variables in a group. You need to retrieve the group, modify the variables you want to change and then post the whole object back to Azure DevOps.

How do you add parameters to a scripted pipeline? ›

Configuring parameters with Scripted Pipeline is done with the properties step, which can be found in the Snippet Generator. If you configured your pipeline to accept parameters using the Build with Parameters option, those parameters are accessible as members of the params variable.

How do you use parameters in pipeline? ›

You can add and define user-defined parameters on incoming parameters of task operators in a pipeline. How you define a user-defined parameter depends on the type of the incoming parameter. For all parameter types, you provide a name and an optional description, and set a default value for the user-defined parameter.

What are the four parameters of the pipeline? ›

The pipeline parameters that apply at the functional area levels are currency, exchange rate type, and initial extraction date and time to schedule the incremental job to run.

How do I set a variable in YAML? ›

The syntax for defining a variable as a content block is: define: &varblock <directive>: <value> <directive>: <value> ... <block>: - <directive>: <value> <directive>: <value> ... Any number of directives or blocks of directives can be inside the definition block.

How do you pass variables in Azure release pipeline? ›

3 Answers
  1. If you want to pass variables from one stage to another stage in yml pipelines for release, you are supposed to use echo "##vso[task....." follow the doc.
  2. You are supposed to get the value set from stage 'BuildStage'.
  3. You could refer the blog for more details. ...
  4. results in the second stage:
Jan 29, 2023

How do I set environment variables in Azure? ›

To set environment variables, use one the following commands, where the ENVIRONMENT_VARIABLE_KEY is the named key and value is the value stored in the environment variable.
  1. Command Line.
  2. PowerShell.
  3. Bash.
Jan 20, 2024

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 5606

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.