[英]Update build definition using Azure DevOps REST API in PowerShell
I'm attempting to update my build definitions in Azure DevOps using the REST API via a PowerShell script... 我正在尝试通过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
}
It's not particularly clear from the Azure DevOps documentation how I should update the build definition using this method, but the above results in the following error: 从Azure DevOps文档中尚不清楚,我应该如何使用此方法更新生成定义,但是以上内容导致出现以下错误:
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\\r\\nParameter name: definition.Repository ","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
调用-RestMethod:{ “的$ id”: “1”, “的InnerException”:空, “消息”: “值不能为空\\ r \\ n参数名:definition.Repository”, “的typeName”:“System.ArgumentNullException, mscorlib“,” typeKey“:” ArgumentNullException“,” errorCode“:0,” eventId“:0}
This is where I'm wondering if I'm barking up the wrong tree as this should surely be simpler (I found a simple solution on SO here for creating a new build definition). 这就是我想知道如果我找错了树,因为这肯定应该是简单(我发现SO简单的解决方案在这里创建一个新的构建定义)。 In fact, all I want to do is update the trigger branch filters.
实际上,我要做的就是更新触发器分支过滤器。
How do I achieve this using PowerShell and the REST API? 如何使用PowerShell和REST API实现此目标?
The triggers
is array so you can't just edit it, you need to edit the triggers[0]
, the same thing it's the branchFilters
, you need to edit the branchFilters[0]
. triggers
是数组,因此您不仅可以编辑它,还需要编辑triggers[0]
, branchFilters
相同的是branchFilters
,还需要编辑branchFilters[0]
。 also, you don't need to touch in the triggerType
. 同样,您无需触摸
triggerType
。
All the above it's assuming that there is already a trigger in the build and you want to edit it, not add a new trigger section. 以上所有操作都假设构建中已经有一个触发器,并且您要编辑它,而不是添加新的触发器部分。
There is also a tricky thing in the branchFilters
array, if you have only 1 branch ( master
for example) and you want to add another branch you need to add it to the array and not just edit the branchFilters[0]
value. branchFilters
数组中还有一个棘手的事情,如果您只有1个分支(例如master
),并且想要添加另一个分支,则需要将其添加到数组中,而不仅仅是编辑branchFilters[0]
值。
Last thing that should be fixed is the branches value, it should be +refs/heads/branchName
and not just the branch name. 应该固定的最后一件事是分支的值,它应该是
+refs/heads/branchName
,而不仅仅是分支名称。
So, I have a pipeline with test
branch trigger and I succeeded to edit the trigger to master
and feature/*
with this script: 因此,我有一个带有
test
分支触发器的管道,并且我使用此脚本成功将触发器编辑为master
和feature/*
:
# I get only one definition and update him, not iterate all my definitions
$definition = Invoke-RestMethod -Uri $url -Method Get
# Change the branch trigger from "test" to "master"
$definition.triggers[0].branchFilters[0] = "+refs/heads/master"
# Add another branch trigger - "feature/*"
$definition.triggers[0].branchFilters[0] += "+refs/heads/feature/*"
$body = $definition | ConvertTo-Json -Depth 10
Write-Host $body
Invoke-RestMethod -Uri $url -Method Put -ContentType application/json -Body $body
It appears that the definitions received from the list method cannot be used directly with the update method . 看来从list方法收到的定义不能直接与update方法一起使用 。 This is quite clear in the list response type
BuildDefinitionReference
which doesn't include properties such as triggers
. 这在列表响应类型
BuildDefinitionReference
非常清楚,该类型不包含triggers
属性。 The definitions must be obtained from the get method using the definition IDs from the list method . 必须使用列表方法的定义ID从get方法获得定义。 This returns a
BuildDefinition
which does indeed have the triggers
property. 这将返回一个
BuildDefinition
,它确实具有triggers
属性。 This can then be modified and passed to the update method . 然后可以对其进行修改并将其传递给update方法 。
This is the working code: 这是工作代码:
$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) {
$definitionToUpdate = Invoke-RestMethod -Uri "$($collection)$($project.name)/_apis/build/definitions/$($definition.id)" -Method GET -Header $header
$trigger = $definitionToUpdate.triggers | Where {$_.triggerType -eq 'continuousIntegration'}
if ($trigger) {
$trigger.branchFilters = $branchNames | % {"+refs/heads/$_/*"}
Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions/$($definition.id)?api-version=5.0" -Method PUT -ContentType application/json -Body ($definitionToUpdate | ConvertTo-Json -Depth 10) -Header $header
}
}
The code checks that the CI trigger exists before updating its branch filters. 该代码在更新其分支过滤器之前检查CI触发器是否存在。
Here is slide correction that works for me, 这是对我有用的幻灯片校正,
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($personalAccessToken)"))
$headers.Add("Authorization", "Basic $token")
$headers.Add("Content-Type", "application/json")
$definitions = Invoke-RestMethod -Uri "https://devops.domain.com/Collection/Project/_apis/build/definitions" -Method GET -Headers $headers
ForEach ($definition in $definitions.value) {
$definition.triggers = (@{ triggerType = 'continuousIntegration'; branchFilters = 'master', 'feature/*' })
$definition.revision++
$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 -Headers $headers
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.