我正在尝试通过 PowerShell 脚本使用 REST API 更新 Azure DevOps 中的构建定义...
$header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))}
$definitions = Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions" -Method GET -Header $header
$branchNames = 'master', 'feature'
ForEach ($definition in $definitions.value) {
$definition | Add-Member -NotePropertyName triggers -NotePropertyValue (@{ triggerType = 'continuousIntegration'; branchFilters = $branchNames | % {"+refs/heads/$_/*"} }) -Force
$body = $definition | ConvertTo-Json
Write-Host $body
Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions/$($definition.id)?api-version=5.0" -Method PUT -ContentType application/json -Body $body -Header $header
}
Azure DevOps 文档中并没有特别清楚我应该如何使用这种方法更新构建定义,但是上面会导致以下错误:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"值不能为空。\r\n参数名称:define.Repository " ,"typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
这就是我想知道我是否在吠叫错误的树的地方,因为这肯定会更简单(我在 SO 上找到了一个简单的解决方案,用于创建新的构建定义)。事实上,我想做的只是更新触发器分支过滤器。
如何使用 PowerShell 和 REST API 实现此目的?