If you mean setting the build schedule using REST API, then you can use Definitions - Update
You can also press F12 in browser to track the API when set the schedule from the UI.
Back to your requirement:
How to schedule an on-premise Azure DevOps build to run every 5 minutes?
Just as you mentioned, currently On-premise Azure DevOps Server does not support schedules in the YAML. And the UI for defining time-based build triggers isn't flexible enough.
So, we cannot achieve that like the built-in feature.
However we can call the queue build REST API to queue the build every 5 minutes, we have two ways to do that:
Write a script to call the queue build REST API, then run it
periodically on a client machine, we can set it with Windows Task
Scheduler. Reference below blogs to do that:
- Hard-coded in the script, open a console to run the script in any
client which can access the Azure DevOps Server (below PowerShell
script works for me):
Example:
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 )
}
UPDATE1:
To update the build definition with the schedule trigger enabled, we need to append the trigger attributes in the request body.
GET build definition by calling the REST API, use the response as the request body.
Append the triggers attributes in the response request body:
"triggers": [
{
"schedules": [
{
"branchFilters": [
"+refs/heads/master"
],
"timeZoneId": "UTC",
"startHours": 5,
"startMinutes": 20,
"daysToBuild": 31,
"scheduleJobId": "5e8e3663-2d1c-482e-bb4d-91f804755010",
"scheduleOnlyWithChanges": true
}
],
"triggerType": "schedule"
}
]
UPDATE2:
Well, you can use below PowerShell script to enable/update the build schedule trigger by updating the build definition:
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)