在PowerShell中使用Azure DevOps REST API更新内部版本定义 [英] Update build definition using Azure DevOps REST API in PowerShell

查看:167
本文介绍了在PowerShell中使用Azure DevOps REST API更新内部版本定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过PowerShell脚本使用REST API在Azure DevOps中更新构建定义...

I'm attempting to update my build definitions in Azure DevOps using the REST API via a PowerShell script...

$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文档我应如何使用此方法更新构建定义,但以上内容会导致以下错误:

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:


Invoke-RestMethod:{ $ id: 1, innerException:null, message:值不能为null。 n参数名称:
definition.Repository , typeName: System.ArgumentNullException,mscorlib, typeKey: ArgumentNullException, errorCode:0, eventId:0 }

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}

这是我想知道是否在吠叫的地方错误的树,因为这肯定会更简单(我在此处找到了一个简单的解决方案,用于创建新的构建定义)。实际上,我要做的就是更新触发器分支过滤器。

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). In fact, all I want to do is update the trigger branch filters.

如何使用PowerShell和REST API做到这一点?

How do I achieve this using PowerShell and the REST API?

推荐答案

似乎从列表方法不能直接与更新方法。在列表响应类型 BuildDefinitionReference 不包含诸如 triggers 。必须从获取方法,它使用来自列表方法。这将返回 BuildDefinition 确实具有 triggers 属性。然后可以对其进行修改并将其传递给更新方法

It appears that the definitions received from the list method cannot be used directly with the update method. This is quite clear in the list response type BuildDefinitionReference which doesn't include properties such as triggers. The definitions must be obtained from the get method using the definition IDs from the list method. This returns a BuildDefinition which does indeed have the triggers property. This can then be modified and passed to the update method.

这是工作代码:

$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触发器是否存在。

The code checks that the CI trigger exists before updating its branch filters.

这篇关于在PowerShell中使用Azure DevOps REST API更新内部版本定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆