Azure DevOps:使用 DevOps API 覆盖 YAML 持续集成触发器
我们希望自动化构建管道,并且应该在特定分支上的每次提交时触发它。 有一种方法可以覆盖 yaml 文件(参见图片)并设置分支过滤器。 但是有没有办法用 API 来做同样的事情呢?
我尝试更新管道定义。 目标是让 JSON 文件看起来像通过 Web 界面手动创建的文件,以设置分支过滤器。 我成功了,但是当我发送后请求并更新网页时,设置了“禁用持续集成”处的勾号,并且再次调用 JSON 后,JSON 文件中的关键“触发器”不再存在。
$apiVersion = "6.0"
$Organization = "Orga"
$Project = "TestProject"
$PipelineId = "42"
$token = "PAT"
$authorization = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$token"))
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=$($apiVersion)"
$json = Invoke-WebRequest -Uri $request -Method Get -Headers @{Authorization = "Basic $authorization" } -ContentType "application/json" | ConvertFrom-Json
#set Branch Filter
$defaultBranch = "folder/branchName"
$branchFilters += @("+refs/heads/$($defaultBranch)")
$json.triggers[0].branchFilters = $branchFilters
#Delete Key: "settingsSourceType"
$json.triggers = $json.triggers | Select-Object -Property * -ExcludeProperty settingsSourceType
#Adding Key: "pollingInterval"
$json.triggers | Add-Member -type NoteProperty -name pollingInterval -value 0
#Convert PSObject back to Json
$json = $json | ConvertTo-Json -Depth 20
#Update Pipeline
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=$($apiVersion)"
$result = Invoke-RestMethod -Uri $request -Method Put -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $authorization) } -Body $json
We want to automate the build pipelines and it should be triggered at each commit on a specific branch.
There is a way to overwrite the yaml-file (see pic) and set a branch filter.
But is there a way to do the same thing with an API?
I tried to update the pipeline definition.
The goal was to get the JSON file to look like one that was created manually via the web interface to set the Branch filter.
I succeeded, but when I send the post-request and update the webpage, the tick at "Disable continuous integration" was set and the key "triggers" in the JSON file is no longer present after calling the JSON again.
$apiVersion = "6.0"
$Organization = "Orga"
$Project = "TestProject"
$PipelineId = "42"
$token = "PAT"
$authorization = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$token"))
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=$($apiVersion)"
$json = Invoke-WebRequest -Uri $request -Method Get -Headers @{Authorization = "Basic $authorization" } -ContentType "application/json" | ConvertFrom-Json
#set Branch Filter
$defaultBranch = "folder/branchName"
$branchFilters += @("+refs/heads/$($defaultBranch)")
$json.triggers[0].branchFilters = $branchFilters
#Delete Key: "settingsSourceType"
$json.triggers = $json.triggers | Select-Object -Property * -ExcludeProperty settingsSourceType
#Adding Key: "pollingInterval"
$json.triggers | Add-Member -type NoteProperty -name pollingInterval -value 0
#Convert PSObject back to Json
$json = $json | ConvertTo-Json -Depth 20
#Update Pipeline
$request = "https://dev.azure.com/$($Organization)/$($Project)/_apis/build/definitions/$($PipelineId)?api-version=$($apiVersion)"
$result = Invoke-RestMethod -Uri $request -Method Put -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $authorization) } -Body $json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看了你的剧本。删除 SettingSourceType 和 PollingInterval 部分可以解决问题:
让我知道该解决方案是否也适合您。
请注意,有一种更简单的方法来设置分支过滤器。事实上,您可以直接从 YAML 执行此操作,无需覆盖 YAML 配置。
看一下 此处的 Azure DevOps 文档段落!
I took a look at your script. Removing the SettingSourceType and PollingInterval section did the trick:
Let me know if the solution works for you as well.
Please note that there's an easier way to set branch filters. You can in fact do that straight from the YAML, without needing to override the YAML configuration.
Take a look at this Azure DevOps Docs paragraph here!