Last active
January 15, 2019 00:39
-
-
Save MarcusFelling/a5152cfd1c3ba9b73d71e3da03f918e1 to your computer and use it in GitHub Desktop.
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
<# | |
.Description | |
Script used to gather all releases that have been successfully deployed to QA, | |
but not yet promoted to Prod environments. Outputs release definition name and URL of release to LatestReleases.html | |
.Example | |
.\Get-ReleasesNotInProduction.ps1 -PAT "NoTaReAlPaT" -AzureDevOpsProjectURL "https://vsrm.dev.azure.com/{organization}/{project}" | |
#> | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true)] | |
$PAT, # Personal Access Token | |
[Parameter(Mandatory=$true)] | |
$AzureDevOpsProjectURL # https://vsrm.dev.azure.com/{organization}/{project} | |
) | |
# Base64-encodes the Personal Access Token (PAT) appropriately | |
# This is required to pass PAT through HTTP header | |
$script:User = "" # Not needed when using PAT, can be set to anything | |
$script:Base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$PAT))) | |
# Get list of all release definitions | |
[uri] $script:GetDefinitionsUri = "$AzureDevOpsProjectURL/_apis/Release/definitions" | |
# Invoke the REST call and capture the response | |
$GetDefinitionsUriResponse = Invoke-RestMethod -Uri $GetDefinitionsUri -Method Get -Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} | |
$DefinitionIDs = $GetDefinitionsUriResponse.value.id | |
# Create custom object to store output in, that can be used to build HTML report. | |
$objTemplateObject = New-Object psobject | |
$objTemplateObject | Add-Member -MemberType NoteProperty -Name DefinitionName -Value $null | |
$objTemplateObject | Add-Member -MemberType NoteProperty -Name Link -Value $null | |
# Create empty array which will become the output object | |
$objResult = @() | |
# Use definition ID's to loop and get latest deployments of each definition | |
ForEach($DefinitionID in $DefinitionIDs){ | |
# Invoke the REST call and capture the response | |
[uri] $GetLatestDeployments = "$AzureDevOpsProjectURL/_apis/release/deployments?definitionId=" + $DefinitionID + "&api-version=5.0-preview.8&deploymentStatus=succeeded" | |
$GetLatestDeploymentsResponse = Invoke-RestMethod -Uri $GetLatestDeployments -Method GET -Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} | |
# Get successful deployments to QA | |
$Deployments = "" | |
$Deployments = $GetLatestDeploymentsResponse.value | Where-Object {$_.releaseEnvironment.name -like "QA*" -AND $_.deploymentStatus -eq "succeeded"} | |
# Use first deployment ID in array to pick latest | |
Try{ | |
$LatestDeployment = "" | |
$LatestDeployment = $Deployments[0] | |
} | |
Catch{ | |
# Do nothing if null array | |
} | |
# Use Release ID to check if release is already deployed to Prod | |
$ReleaseId = $LatestDeployment.release.id | |
[uri] $GetRelease = "$AzureDevOpsProjectURL/_apis/Release/releases/" + $ReleaseId + "?api-version=5.0-preview.8" | |
$GetReleaseResponse = Invoke-RestMethod -Uri $GetRelease -Method GET -Headers @{Authorization=("Basic {0}" -f $Base64AuthInfo)} | |
# Get active releases only (not abandoned) | |
$GetReleaseResponse = $GetReleaseResponse | Where-Object {$_.status -eq "active"} | |
# Check if deployed to prod yet and has a status of notStarted | |
$NoDeployment = "" | |
$NoDeployment = $GetReleaseResponse.environments | Where-Object {$_.name -like "*PROD*" -AND $_.status -eq "notStarted"} | |
$NoDeploymentReleaseDefinitionName = $NoDeployment.releaseDefinition.name # Use definition name as output variable | |
If($NoDeployment){ | |
# Write output to Azure Pipeline log | |
$NoDeploymentReleaseDefinitionName | Select-Object -first 1 | |
$LatestDeployment.release.webAccessUri | |
# Create an instance of new object to prepare it with data and later add it to the result array for report | |
$objTemp = $objTemplateObject | Select-Object * | |
# Populate the custom object properties | |
$objTemp.DefinitionName = $NoDeploymentReleaseDefinitionName | Select-Object -first 1 | |
$objTemp.Link = $LatestDeployment.release.webAccessUri | |
# Add temp object to output array and get ready to loop back around | |
$objResult += $objTemp | |
} | |
} | |
# Set CSS properties for HTML report | |
$Header = @" | |
<style> | |
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;} | |
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;} | |
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;} | |
</style> | |
"@ | |
# Output to HTML file that is sent via email in release definition | |
$objResult = $objResult | | |
ConvertTo-Html @{Label="DefinitionName";Expression={$_.DefinitionName}},@{Label="Link";Expression={ "<a href='$($_.Link)'>$($_.Link)</a>" }} -Head $Header | |
Add-Type -AssemblyName System.Web | |
[System.Web.HttpUtility]::HtmlDecode($objResult) | Out-File "C:\Temp\LatestReleases.html" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment