-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAzureDevOps_Get-ReleasesToPromote.ps1
More file actions
101 lines (83 loc) · 4.52 KB
/
AzureDevOps_Get-ReleasesToPromote.ps1
File metadata and controls
101 lines (83 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<#
.Description
Script used to gather all releases that have been successfully deployed to QA,
but not yet promoted to Pre-Prod or Prod environments.
.Outputs
"C:\Temp\LatestReleases.html"
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
$PAT, # Personal Access Token
[Parameter(Mandatory=$false)]
$TFSBaseURL
)
# 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 = "$TFSBaseURL/_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){
[uri] $GetLatestDeployments = "$TFSBaseURL/_apis/release/deployments?definitionId=" + $DefinitionID + "&api-version=4.0-preview&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 Pre-Prod or Prod
$ReleaseId = $LatestDeployment.release.id
[uri] $GetRelease = "$TFSBaseURL/_apis/Release/releases/" + $ReleaseId + "?api-version=4.0-preview"
$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 pre-prod or prod yet, and active (not abandoned)
$NoDeployment = ""
$NoDeployment = $GetReleaseResponse.environments | Where-Object {$_.name -like "*PROD*" -AND $_.status -eq "notStarted"}
$NoDeploymentReleaseDefinitionName = ""
$NoDeploymentReleaseDefinitionName = $NoDeployment.releaseDefinition.name
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"