Skip to content

Instantly share code, notes, and snippets.

@Antebios
Created October 20, 2021 21:25
Show Gist options
  • Save Antebios/93d4fff10ce77d0b9395585503bf9dce to your computer and use it in GitHub Desktop.
Save Antebios/93d4fff10ce77d0b9395585503bf9dce to your computer and use it in GitHub Desktop.
Get Azure DevOps Release Pipeline Status
# Define the Release that you want to monitor or pass it as a variable.
$TestReleaseID = 4003
# https://docs.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-6.0#environmentstatus
# EnvironmentStatus
# Gets environment status.
# Name Type Description
# canceled ==> Environment is in canceled state.
# inProgress ==> Environment is in progress state.
# notStarted ==> Environment is in not started state.
# partiallySucceeded ==> Environment is in partially succeeded state.
# queued ==> Environment is in queued state.
# rejected ==> Environment is in rejected state.
# scheduled ==> Environment is in scheduled state.
# succeeded ==> Environment is in succeeded state.
# undefined ==> Environment status not set.
function isStageRunning {
[CmdletBinding()]
param (
[string]$ReleaseID,
[string]$StageName
)
begin {
$adoPAT=":<SET_PAT_HERE>"
$patBytes = [System.Text.Encoding]::UTF8.GetBytes($adoPAT)
$encodedPAT =[Convert]::ToBase64String($patBytes)
$url = "https://vsrm.dev.azure.com/<ORG_NAME>/<PROJECT_NAME>/_apis/release/releases/$($releaseID)?api-version=6.0"
$headers = @{
"Accept"="application/json"
"Authorization"="Basic $encodedPAT"
}
$releaseObj = Invoke-RestMethod -uri $url -Method GET -Headers $headers
$releaseStages = $releaseObj.environments
$stageSetupContainers = $releaseStages | Where-Object { $_.name -match "$StageName" }
$stageStatus = $stageSetupContainers.status
Write-Host "Status for $StageName is $stageStatus"
$statusArray = @('canceled','succeeded','partiallySucceeded','rejected')
if ($statusArray.contains($stageStatus)) {
# Stage is completed
return $true
} else {
# Stage has not completed
return $false
}
}
}
# Loop forever!
while (1) {
$stageSetupContainers = isStageRunning -ReleaseID $TestReleaseID "Setup Containers"
$stageHubMetrics = isStageRunning -ReleaseID $TestReleaseID "FitNesse-HubMetrics"
$stageSmokeTest = isStageRunning -ReleaseID $TestReleaseID "FitNesse-SmokeTests"
# Check if all are returning false
if (($stageSetupContainers) -and
($stageHubMetrics) -and
($stageSmokeTest)) {
# All Stages within release are stopped
"All Stages are stopped!"
break
} else {
Write-Host "Inspecting... "
Write-Host "Setup Containers = $stageSetupContainers"
Write-Host "FitNesse-HubMetrics = $stageHubMetrics"
Write-Host "FitNesse-SmokeTests = $stageSmokeTest"
Write-Host "Sleeping for 30 seconds."
Start-Sleep -Seconds 30
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment