[英]How to set Release Definition artifacts using Azure DevOps REST API
[英]Update build definition using Azure DevOps REST API in PowerShell
我正在尝试通过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文档中尚不清楚,我应该如何使用此方法更新生成定义,但是以上内容导致出现以下错误:
调用-RestMethod:{ “的$ id”: “1”, “的InnerException”:空, “消息”: “值不能为空\\ r \\ n参数名:definition.Repository”, “的typeName”:“System.ArgumentNullException, mscorlib“,” typeKey“:” ArgumentNullException“,” errorCode“:0,” eventId“:0}
这就是我想知道如果我找错了树,因为这肯定应该是简单(我发现SO简单的解决方案在这里创建一个新的构建定义)。 实际上,我要做的就是更新触发器分支过滤器。
如何使用PowerShell和REST API实现此目标?
triggers
是数组,因此您不仅可以编辑它,还需要编辑triggers[0]
, branchFilters
相同的是branchFilters
,还需要编辑branchFilters[0]
。 同样,您无需触摸triggerType
。
以上所有操作都假设构建中已经有一个触发器,并且您要编辑它,而不是添加新的触发器部分。
branchFilters
数组中还有一个棘手的事情,如果您只有1个分支(例如master
),并且想要添加另一个分支,则需要将其添加到数组中,而不仅仅是编辑branchFilters[0]
值。
应该固定的最后一件事是分支的值,它应该是+refs/heads/branchName
,而不仅仅是分支名称。
因此,我有一个带有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
看来从list方法收到的定义不能直接与update方法一起使用 。 这在列表响应类型BuildDefinitionReference
非常清楚,该类型不包含triggers
属性。 必须使用列表方法的定义ID从get方法获得定义。 这将返回一个BuildDefinition
,它确实具有triggers
属性。 然后可以对其进行修改并将其传递给update方法 。
这是工作代码:
$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
}
}
该代码在更新其分支过滤器之前检查CI触发器是否存在。
这是对我有用的幻灯片校正,
$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.