Update2:
Using YAML should be the simplest solution. If you insist on Classic build view. You could try to accomplish this by storing the values in a file (json, xml, yaml, what have you), you can read the file in the Job either direct use or re-set the variable again.
When you queue next build, it will not effect the file in source control and the default value will also not change.
Passing variables between jobs in the same stage, it requires working with output variables.
However, according to this, using outputs in a different job
is not supported in Classic UI Format.
As workarounds in this scenario, you can share variables via Pipeline Variables(share variables across jobs in same pipeline).
1.You can set a key
variable in pipeline variables:

2.Add one Powershell Inline task with content below in your first job:
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/definitions/$($env:SYSTEM_DEFINITIONID)?api-version=5.0"
Write-Host "URL: $url"
$pipeline = Invoke-RestMethod -Uri $url -Headers @{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"
# Update an existing variable to its new value
$pipeline.variables.key.value = "value"
####****************** 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 key is updated to" $updatedef.variables.key.value
write-host "=========================================================="
3.Run the pipeline we can find the value of key
variable is successfully updated:

So you can run the ps script in first job to update the value of key
variable, then all next jobs can access the updated variable easily.
Note:
- For the script itself, you only need to change lines
$pipeline.variables.key.value = "value"
(necessary) and Write-host "The value of Varialbe key is updated to" $updatedef.variables.key.value
(optional).
If I want to set the variable named MyTest
to value MyValue
, the lines should be $pipeline.variables.MyTest.value = "MyValue"
and Write-host "The value of Varialbe MyTest is updated to" $updatedef.variables.MyTest.value
.
- To make sure the ps task in one job can access
OAuth Token
, we should Allow Scripts to Access OAuth Token
. Click the agent job name and check the box:

- To enable the pipeline has the permission to update pipeline variable (edit build pipeline), go pipeline security to set the
Edit build pipeline
allow for user xxx(ProjectName) build service
.
