My release pipeline has two different agents, one azurehosted and one selfhosted.
In the Azure hosted agent, i'm setting my variable.
Later, in the self hosted agent, i need to read to variable's value.
However, it's not working to share variable values accross the two agents... any idea?
Attachments:
Basically, we can use the Logging commands with isoutput=true
to pass variable between jobs for YAML pipelines. See SetVariable: Initialize or modify the value of a variable and
Use output variables from tasks for details.
Write-Host "##vso[task.setvariable variable=outputSauce;isoutput=true]canned goods"
But for the classic release pipelines, it’s not supported to pass variables across jobs/stages like YAML.
In your scenario, across multiple agents means in different jobs. So, it doesn't work.
However, we can use another way to achieve that:
Make sure the build service account has the permission to manage the release:
Enable the option Allow scripts to access the OAuth token
in job
agent.
Add a PowerShell task to run a script to update the current release variable value if needed by calling REST API Releases - Update. You can reference the following PowerShell script to do that.
Then we can use the updated variable in other jobs across agents.
PowerShell script for your reference:
$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/release/releases/$($env:RELEASE_RELEASEID)?api-version=7.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 99)"
# Update an existing variable named FOO to its new value 1234
$pipeline.variables.FOO.value = "1234"
#****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99
$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
write-host "=========================================================="
Write-host "The value of Varialbe 'FOO' is updated to" $updatedef.variables.FOO.value
write-host "=========================================================="