'Azure DevOps: Override the YAML continuous integration trigger - With DevOps API

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



Solution 1:[1]

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!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tommasodotNET