Created
February 22, 2022 16:23
-
-
Save kiwimato/724335c7801e454064b84fb68b505271 to your computer and use it in GitHub Desktop.
Stops all Azure DevOps jobs for a specific repository. Useful in case where a lot of builds were started which deplets the pools
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
$targetRepository = "some_repository_name" | |
$AzureDevOpsPAT = "XX" | |
$OrganizationName = "projectName/organizationName" | |
$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