I'm attempting to update my build definitions in Azure DevOps using the REST API via a PowerShell script...
$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:
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}
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). In fact, all I want to do is update the trigger branch filters.
How do I achieve this using PowerShell and the REST API?
It appears that the definitions received from the list method cannot be used directly with the update method. This is quite clear in the list response type
BuildDefinitionReferencewhich doesn't include properties such astriggers. The definitions must be obtained from the get method using the definition IDs from the list method. This returns aBuildDefinitionwhich does indeed have thetriggersproperty. This can then be modified and passed to the update method.This is the working code:
The code checks that the CI trigger exists before updating its branch filters.