doubledecker
doubledecker

Reputation: 363

Azure DevOps REST api to add continuous deployment trigger

I am trying to add a new branch to existing set of release triggers. I am able to view the URL content for the release definition however PATCH operation is failing with the following error. Could someone assist me in reviewing and refining my code?

"innerException": null, "message": "VS403051: Could not delete pipeline with ID 515 because it was not found. Specify a valid ID | and try again.", "typeName": "Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.DeletedReleaseDefinitionNotFoundException

Code for reference

$releaseDefinitionId = "515"
$pat = "test_pat_token"

# Define the continuous deployment trigger
$continuousDeploymentTrigger = @{
    triggerType = "continuousIntegration"
    settingsSourceType = "definition"
    branchFilters = @(
        "+refs/heads/develop"
    )
}

# Convert the continuous deployment trigger to JSON format
$body = @{
    triggers = @(
        $continuousDeploymentTrigger
    )
} | ConvertTo-Json

# Define the REST API URL
$url = "https://vsrm.dev.azure.com/org/project/_apis/release/definitions/$($releaseDefinitionId)?api-version=7.1-preview.4"

# Invoke the REST API to update the release definition
Invoke-RestMethod -Uri $url -Method Patch -Headers @{
    Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
} -Body $body -ContentType "application/json"

Upvotes: 0

Views: 124

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13944

To update the release definition, you should use the REST API "Definitions - Update" with the PUT method.

  • You first need to call the API "Definitions - Get" to get the response body of the release definition.

  • Update the response body by adding the new branch filter to the CD trigger.

  • Finally, you can call the API "Definitions - Update" with the new body to update the release definition.

Below is a PowerShell script sample as reference.

$organization = "{organization}"
$project = "{project}"
$definitionId = "{definitionId}"
$branchName = "{branchName}"  # e.g., develop

# Need to convert the PAT to be a base64 string.
$pat = "{pat}"
$base64Token = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))

# Set HTTP request headers.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64Token))

# Get the release definition that needs to be updated.
$getDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions/${definitionId}?api-version=7.0"
$Def = Invoke-RestMethod -Method GET -Uri $getDef_uri -Headers $headers

# Set the new branch filter that will be added to the CD trigger of the release definition.
$newTrigger = @{
    sourceBranch = "$branchName"
    tagFilter = $null
    useBuildDefinitionBranch = $false
    createReleaseOnBuildTagging = $false
}

# Add the new branch filter to the CD trigger. Get a new json body of the release definition. 
$triggerConditions = $Def.triggers[0].triggerConditions
$triggerConditions += $newTrigger
$Def.triggers[0].triggerConditions = $triggerConditions
$updateDef_body = @($Def) | ConvertTo-Json -Depth 100

# Update the release definition with the new json body.
$updateDef_uri = "https://vsrm.dev.azure.com/${organization}/${project}/_apis/release/definitions?api-version=7.0"
$updateDef = Invoke-RestMethod -Method PUT -Uri $updateDef_uri -Body $updateDef_body -ContentType "application/json" -Headers $headers
Write-Host $($updateDef | ConvertTo-Json -Depth 100)

enter image description here


Upvotes: 0

Related Questions