Search code examples
azure-devops-rest-api

API to cancel build


I have a script to cancel previous builds in Azure DevOps pipeline.

Here is my code:

$uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId?api-version=5.1"
$json = @{status="Cancelling"} | ConvertTo-Json -Compress
Invoke-RestMethod -Uri $uri -Method Patch -Headers <headers> -ContentType "application/json" -Body $json        

On running this, I see the PATCH API failing:

Invoke-RestMethod : 
  
    Page not found.
    html {
    height: 100%;
}           <img class="logo" src="data:image/png;base64
test.ps1:23 char:18
+ ...    $build = Invoke-RestMethod -Uri $uri -Method Patch -Headers @{Auth ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

What am I missing?


Solution

  • Change your uri from:

    $uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId?api-version=5.1"
    

    to:

    $uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$($buildId)?api-version=5.1"
    

    or to

    $uri = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId" + "?api-version=5.1"