如何以编程方式将 Azure 构建定义代理从 vs2017-win2016 更新到 windows-2019

How to update Azure build definition agent from vs2017-win2016 to windows-2019 programmatically

我正在尝试以编程方式将我所有的构建管道代理从 vs2017-win2016 更新到 windows-2019。我可以访问 AZ CLI、PowerShell 和 REST API,但我还没有找到让它工作的魔法咒语。具体来说,我想为特定项目和构建定义名称更新 Azure Pipeline 代理。

How to update Azure build definition agent from vs2017-win2016 to windows-2019 programmatically

我们可以使用 REST API Definitions - Get to get all the info about this definition in the body, then we could update the body and use the Definitions - Update 从构建管道更新发布定义变量的值:

PUT https://dev.azure.com/{organization}/{project}/_apis/build/definitions/{definitionId}?api-version=5.0

以下是我的测试内联 powershell 脚本:

$url = "https://dev.azure.com/{organization}/{project}/_apis/build/definitions/57?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 build definition named and build definition agent to its new value 2
$pipeline.process.target.agentSpecification.identifier= "windows-2019"
$pipeline.name= "UpdateVariabletest"

####****************** 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 build definition named and build definition agent is updated to" 

$updatedef.process.target.agentSpecification.identifier
$updatedef.name

您可以查看 了解更多详情。