如果您的意思是使用 REST API 设置构建计划,那么您可以使用 Definitions - Update
您也可以在从 UI 设置计划时在浏览器中按 F12 来跟踪 API。
回到你的要求:
如何安排本地 Azure DevOps 构建每 5 分钟运行一次?
正如您所提到的,目前本地 Azure DevOps 服务器不支持 YAML 中的计划。而且用于定义基于时间的构建触发器的 UI 不够灵活。
因此,我们无法像内置功能那样实现这一点。
但是我们可以调用queue build REST API 每 5 分钟将构建排队,我们有两种方法可以做到这一点:
-
编写一个脚本来调用队列构建 REST API,然后运行它
定期在客户端机器上,我们可以使用 Windows Task 进行设置
调度器。参考下面的博客来做到这一点:
- 在脚本中硬编码,打开控制台以任意方式运行脚本
可以访问 Azure DevOps 服务器的客户端(在 PowerShell 下
脚本适合我):
例子:
Param(
[string]$collectionurl = "https://server/DefaultCollection",
[string]$projectName = "ProjectName",
[string]$BuildDefinitionId = "11",
[string]$user = "username",
[string]$token = "password/PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{
"definition": {
"id": $BuildDefinitionId
}
}
"@
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/build/builds?api-version=5.1"
$EndTime = Get-Date
while($true) {
$EndTime = $EndTime.AddMinutes(5)
###Queue build###
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Start-Sleep -Seconds $( [int]( New-TimeSpan -End $EndTime ).TotalSeconds )
}
更新1:
要在启用计划触发器的情况下更新构建定义,我们需要在请求正文中附加触发器属性。
GET build definition 通过调用 REST API,将响应用作请求正文。
在响应请求正文中附加触发器属性:
"triggers": [
{
"schedules": [
{
"branchFilters": [
"+refs/heads/master"
],
"timeZoneId": "UTC",
"startHours": 5,
"startMinutes": 20,
"daysToBuild": 31,
"scheduleJobId": "5e8e3663-2d1c-482e-bb4d-91f804755010",
"scheduleOnlyWithChanges": true
}
],
"triggerType": "schedule"
}
]
更新 2:
好吧,您可以使用下面的 PowerShell 脚本通过更新构建定义来启用/更新构建计划触发器:
Param(
[string]$collectionurl = "https://server/DefaultCollection",
[string]$project = "projectname",
[string]$definitionid = "183",
[string]$user = "username",
[string]$token = "password/PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$ErrorActionPreference = 'SilentlyContinue'
#Get resonse of the build definition
$defurl = "$collectionurl/$project/_apis/build/definitions/$($definitionid)?api-version=5.1"
$definition = Invoke-RestMethod -Uri $defurl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
#Set trigger array
$triggers = '
[{
"schedules": [
{
"branchFilters": [
"+refs/heads/master"
],
"timeZoneId": "UTC",
"startHours": 9,
"startMinutes": 40,
"daysToBuild": 31,
"scheduleOnlyWithChanges": true
}
],
"triggerType": "schedule"
}]'
cls
#Add a trigger block to the response body
$definition | Add-Member -NotePropertyName "triggers" -NotePropertyValue (Convertfrom-Json $triggers) -Force
Remove-TypeData System.Array # Remove the redundant ETS-supplied .Count and values property
#Convert the response body to Json
$json = @($definition) | ConvertTo-Json -Depth 99
#Update build definition
$updatedef = Invoke-RestMethod -Uri $defurl -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host ($updatedef.triggers | ConvertTo-Json -Depth 99)