![](/img/trans.png)
[英]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.