You can use the REST API (Definitions - Update) to update the value of the release definition variable from a release task.
- Go to the
Agent Phase
and select Allow Scripts to Access OAuth Token
. See Use the OAuth token to access the REST API
- Grant
Project Collection Build Service (xxx)
account the edit
release pipeline permission. (Select the release pipeline --> ...
--> Security
--> Edit release definition
set to Allow
)
- Add a PowerShell task in your release pipeline
Run inline script: (Update the value of variable v1030
in below sample)
$url = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0-preview.3"
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 named v1030 to its new value 1035
$pipeline.variables.v1030.value = "1035"
####****************** 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 'v1030' is updated to" $updatedef.variables.v1030.value
write-host "=========================================================="
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…