Skip to content

Instantly share code, notes, and snippets.

@mcindea
Created August 4, 2022 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcindea/a4cc732b696bd898fb0aeb7d6543a7f0 to your computer and use it in GitHub Desktop.
Save mcindea/a4cc732b696bd898fb0aeb7d6543a7f0 to your computer and use it in GitHub Desktop.
Stops batch azure devops jobs/builds related to a specific repo
# This script can help to stop multiple jobs if there are too many to stop by point and click
# Just modify targetRepository, AzureDevOpsPAT and OrganizationName and it will stop all builds related to the target repository
$targetRepository = "some_repo_name"
$AzureDevOpsPAT = "XXXXX"
$OrganizationName = "org/project"
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$builds = Invoke-RestMethod -Uri "https://dev.azure.com/$OrganizationName/_apis/build/builds?api-version=6.0&repositoryId=" -Method get -Headers $AzureDevOpsAuthenicationHeader
$runningBuilds = $builds.value | Where-Object { $_.repository.name -like $targetRepository } | Where-Object { $_.status -eq 'inProgress' }
foreach($build in $runningBuilds){
$uri = "https://dev.azure.com/$OrganizationName/_apis/build/builds/$($build.id)?api-version=5.1"
$json = @{status="Cancelling"} | ConvertTo-Json -Compress
Write-Host "Stopping job $($build.id) for repository: $($build.repository.name)"
$stop = Invoke-RestMethod -Uri $uri -Method Patch -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $json
Write-Host $stop
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment