There are a couple of ways to set a Variable from a script using magic commands. A few people have also written extensions to do this:
- task: SetValueFromJSON@0
inputs:
variableName: 'package.version'
jsonPathExpression: '$.version'
jsonFile: 'package.json'
From your own bash or powershell script you can also register a variable using magic log strings:
- bash: |
echo "##vso[task.setvariable variable=sauce;]crushed tomatoes"
name: SetVarsBash
- pwsh: |
Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
name: SetVarsPwsh
You can use existing script commands to read the json file and fetch the value. Here is an example of me doing something similar in GitHub Actions in a powershell script. While the syntax to register a variable is a little different, the concept of taking the variable value from somewhere and registering it as a variable is the same:
$release = (& gh release view $tag --json url) | ConvertFrom-Json
if (-not $release)
{
$env:TAG = "m$version"
$env:VERSION = "$version"
Write-Host "##vso[task.setvariable variable=VERSION;]$version"
Write-Host "##vso[task.setvariable variable=TAG;]m$version"
}
There are some specifics documented about naming ang encoding.
These examples are in YAML, since that's easier in StackOverflow, but you can take the script contents and use them 1-on-1 in the graphic release pipelines:

Pass a variable to a script task the safe way:
