Skip to content

Latest commit

 

History

History
127 lines (92 loc) · 3.49 KB

set-variables-in-scripts.md

File metadata and controls

127 lines (92 loc) · 3.49 KB
ms.topic ms.technology ms.manager ms.author author ms.date
include
devops-cicd
mijacobs
jukullam
juliakm
08/19/2020

Define and modify your variables in a script

To define or modify a variable from a script, use the task.setvariable logging command. Note that the updated variable value is scoped to the job being executed, and does not flow across jobs or stages. Variable names are transformed to uppercase, and the characters "." and " " are replaced by "_".

For example, Agent.WorkFolder becomes AGENT_WORKFOLDER. On Windows, you access this as %AGENT_WORKFOLDER% or $env:AGENT_WORKFOLDER. On Linux and macOS, you use $AGENT_WORKFOLDER.

Tip

You can run a script on a:

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)"

:::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)