Created
August 4, 2022 11:36
-
-
Save mcindea/a4cc732b696bd898fb0aeb7d6543a7f0 to your computer and use it in GitHub Desktop.
Stops batch azure devops jobs/builds related to a specific repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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