简体   繁体   English

在PowerShell中使用Azure DevOps REST API更新内部版本定义

[英]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分支触发器的管道,并且我使用此脚本成功将触发器编辑为masterfeature/*

# 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.

相关问题 如何使用 Azure DevOps REST API 设置发布定义工件 - How to set Release Definition artifacts using Azure DevOps REST API 使用 Powershell 的 Azure Devops 审计 rest api 延续令牌 - Azure Devops audit rest api continuation token using powershell Azure devops rest api 结果使用 powershell 脚本循环 - Azure devops rest api result looping using powershell script 如何使用 Devops REST API 搜索带有 Powershell 的 Azure Devops 代码存储库并输出到 CSV - How to search Azure Devops Code Repos w/ Powershell using Devops REST API and output to CSV Azure Devops yaml 构建定义 PowerShell 内联语法问题 - Azure Devops yaml build definition PowerShell inline Syntax problem 如何使用 PowerShell 从 Azure CloudShell 访问 Azure DevOps REST API? - How to access Azure DevOps REST API from Azure CloudShell using PowerShell? 尝试使用 Azure DevOps Rest API 创建发布定义时出错 - Error while trying to create a release definition using Azure DevOps Rest API 根据Azure DevOps Rest API对Azure Pipelines Powershell任务进行身份验证 - Authenticate Azure Pipelines Powershell task against Azure DevOps Rest API 使用 PowerShell 更新 Azure DevOps 中的服务挂钩 - Update Service Hook in Azure DevOps Using PowerShell 如何使用 powershell(Azure devops REST API)获取用户提交更改的 Azure devops git repo 文件夹名称 - How to get Azure devops git repo folder name where user committed changes using powershell (Azure devops REST API)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM