Azure DevOps:使用 DevOps API 覆盖 YAML 持续集成触发器

发布于 2025-01-16 20:47:06 字数 1658 浏览 3 评论 0原文

我们希望自动化构建管道,并且应该在特定分支上的每次提交时触发它。 有一种方法可以覆盖 yaml 文件(参见图片)并设置分支过滤器。 但是有没有办法用 API 来做同样的事情呢?

Web 界面示例

我尝试更新管道定义。 目标是让 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?

An Example of the Web Interface

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

The Interface after this Code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆で 2025-01-23 20:47:06

我看了你的剧本。删除 SettingSourceType 和 PollingInterval 部分可以解决问题:

$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 = "feature/myfeature"
$branchFilters += @("+refs/heads/$($defaultBranch)")
$json.triggers[0].branchFilters = $branchFilters

#Convert PSObject back to Json
$json = $json | ConvertTo-Json -Depth 20

让我知道该解决方案是否也适合您。

请注意,有一种更简单的方法来设置分支过滤器。事实上,您可以直接从 YAML 执行此操作,无需覆盖 YAML 配置。
看一下 此处的 Azure DevOps 文档段落

I took a look at your script. Removing the SettingSourceType and PollingInterval section did the trick:

$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 = "feature/myfeature"
$branchFilters += @("+refs/heads/$($defaultBranch)")
$json.triggers[0].branchFilters = $branchFilters

#Convert PSObject back to Json
$json = $json | ConvertTo-Json -Depth 20

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文