Last active
November 23, 2016 22:56
-
-
Save NickMRamirez/5e44dd86f1ee09f8a295a254c80bd24e to your computer and use it in GitHub Desktop.
This file contains 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 | |
Gets an array of hashes where each hash contains the properties ProjectName | |
and ReleaseVersion. Each is the latest release to be deployed to Production | |
for this project. | |
#> | |
function Get-LatestProdReleases( | |
$base_address = "http://octopus.aclens.local", | |
$apikey = "API-9W4ROZVB6M5D6OEPWJ7CDZ6RZSO") | |
{ | |
# Array to hold the results | |
$latest_prod_deployments = New-Object System.Collections.ArrayList | |
# Get all projects | |
$projects = Invoke-RestMethod -Uri "$base_address/api/projects/all?apikey=$apikey" | |
Write-Host "Finding latest releases..." | |
foreach ($project in $projects) | |
{ | |
# Get the Default channel for the project | |
$channels_path = $project.Links.Channels | |
$channels_url = "${base_address}${channels_path}?apikey=$apikey" | |
$channels = Invoke-RestMethod $channels_url | |
$default_channel = $channels.Items.Where({ $_.IsDefault }) | |
# Get the releases for this channel | |
$releases_path = $default_channel.Links.Releases | |
$releases_url = "${base_address}${releases_path}?apikey=$apikey" | |
$releases = Invoke-RestMethod $releases_url | |
$break_from_release_loop = $FALSE | |
foreach ($release in $releases.Items) | |
{ | |
# Break out of the loop if we already found what we were looking for | |
if ($break_from_release_loop -eq $TRUE) | |
{ | |
break; | |
} | |
# Find out which release got deployed to Production most recently | |
$progression_path = $release.Links.Progression | |
$progression_url = "${base_address}${progression_path}?apikey=$apikey" | |
$progressions = Invoke-RestMethod $progression_url | |
foreach ($progression in $progressions) | |
{ | |
$production_progressions = $progression.Phases.Where({ $_.Name -eq "Production" -and $_.Progress -eq "Complete" }) | |
if ($production_progressions -ne $NULL) | |
{ | |
# Store the project name and release version in a hash and add it to our array | |
$latest_prod_deployments.Add(@{ ProjectName = $project.Name; ReleaseVersion = $release.SelectedPackages[0].Version }) | Out-Null | |
$break_from_release_loop = $TRUE | |
break; | |
} | |
} | |
} | |
} | |
# return the array of hashes | |
return $latest_prod_deployments | |
} | |
# Check results | |
foreach ($prod_deployment in Get-LatestProdReleases) | |
{ | |
$prod_deployment.ProjectName | |
$prod_deployment.ReleaseVersion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment