Skip to content

Files

Latest commit

43b6403 · Aug 16, 2024

History

History
132 lines (96 loc) · 3.83 KB

set-variables-in-scripts.md

File metadata and controls

132 lines (96 loc) · 3.83 KB
ms.topic ms.service ms.manager ms.author author ms.date
include
azure-devops-pipelines
mijacobs
jukullam
juliakm
08/16/2024

Define and modify your variables in a script

To define or modify a variable from a script, use the task.setvariable logging command. The updated variable value is scoped to the job being executed and doesn't persist across jobs or stages. Note that variable names are transformed to uppercase, with "." and " " replaced with "_".

For example, Agent.WorkFolder becomes AGENT_WORKFOLDER.

  • On Windows, access this variable as %AGENT_WORKFOLDER% or $env:AGENT_WORKFOLDER.
  • On Linux and macOS, use $AGENT_WORKFOLDER.

Tip

You can run a script on:

Batch script

:::image type="icon" source="../tasks/utility/media/batch-script.png" border="false"::: Set the sauce and secret.Sauce variables

@echo ##vso[task.setvariable variable=sauce]crushed tomatoes
@echo ##vso[task.setvariable variable=secret.Sauce;issecret=true]crushed tomatoes with garlic

:::image type="icon" source="../tasks/utility/media/batch-script.png" border="false"::: Read the variables

Arguments

"$(sauce)" "$(secret.Sauce)"

Script

@echo off
set sauceArgument=%~1
set secretSauceArgument=%~2
@echo No problem reading %sauceArgument% or %SAUCE%
@echo But I cannot read %SECRET_SAUCE%
@echo But I can read %secretSauceArgument% (but the log is redacted so I do not spoil the secret)

PowerShell script

:::image type="icon" source="../tasks/utility/media/powershell.png" border="false"::: Set the sauce and secret.Sauce variables

Write-Host "##vso[task.setvariable variable=sauce]crushed tomatoes"
Write-Host "##vso[task.setvariable variable=secret.Sauce;issecret=true]crushed tomatoes with
            garlic"

:::image type="icon" source="../tasks/utility/media/powershell.png" border="false"::: Read the variables

Arguments

-sauceArgument "$(sauce)" -secretSauceArgument "$(secret.Sauce)"

Script

Param(
   [string]$sauceArgument,
   [string]$secretSauceArgument
)
Write-Host No problem reading $env:SAUCE or $sauceArgument
Write-Host But I cannot read $env:SECRET_SAUCE
Write-Host But I can read $secretSauceArgument "(but the log is redacted so I do not spoil the secret)"

Inline PowerShell script

Use the sauce and secret.Sauce variables in an inline script.

- pwsh: |
      Write-Host No problem reading $(sauce)
      Write-Host But I cannot read $env:SECRET_SAUCE
      Write-Host But I can read $(secret.Sauce) "(but the log is redacted so I do not spoil the secret)"

:::image type="icon" source="../tasks/utility/media/shell-script.png" border="false"::: Set the sauce and secret.Sauce variables

#!/bin/bash
echo "##vso[task.setvariable variable=sauce]crushed tomatoes"
echo "##vso[task.setvariable variable=secret.Sauce;issecret=true]crushed tomatoes with garlic"

:::image type="icon" source="../tasks/utility/media/shell-script.png" border="false"::: Read the variables

Arguments

"$(sauce)" "$(secret.Sauce)"

Script

#!/bin/bash
echo "No problem reading $SAUCE"
echo "But I cannot read $SECRET_SAUCE"

Console output from reading the variables:

No problem reading crushed tomatoes or crushed tomatoes
But I cannot read 
But I can read ******** (but the log is redacted so I do not spoil the secret)