Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Promote release from staging to other environments
$api = "http://octopus.dev.azure.fundapps.co/"
$url = $api + "/api/projects/projects-1/most-recent-deployment/"
$response = Invoke-RestMethod -Uri $url -Method GET -Headers @{"X-Octopus-ApiKey"="x"}
$latestMaster = $response | where {$_.Name -eq "Deploy to Staging (master)"}
$url = $api + "/api/environments"
$stagingEnvironments = Invoke-RestMethod -Uri $url -Method GET -Headers @{"X-Octopus-ApiKey"="x"}
$stagingEnvironments = $stagingEnvironments | where {$_.Name.StartsWith("Staging ")} | where {$_.Name -ne "Staging (master)"} | where {$_.Name -ne "Staging (develop)"}
foreach ($environment in $stagingEnvironments) {
$command = '{"ReleaseId":"' + $latestMaster.ReleaseId + '" ,"EnvironmentId":"' + $environment.Id + '"}'
$url = $api + "/api/projects/projects-1/releases/" + $latestMaster.ReleaseId + "/deployments"
$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Method POST -Headers @{"X-Octopus-ApiKey"="x"} -Body $command
$url = $api + $response.Links.Task
$taskStatus = Invoke-RestMethod -Uri $url -Method GET -Headers @{"X-Octopus-ApiKey"="x"}
Write-Host "Deploying" $latestMaster.ReleaseId "to" $environment.Name "-" $environment.Id " " -nonewline;
do
{
$taskStatus = Invoke-RestMethod -Uri $url -Method GET -Headers @{"X-Octopus-ApiKey"="x"}
Write-Host "." -nonewline;
sleep 1
if($taskStatus.State -eq "Failed")
{
write-output "Deploy Failed" $taskStatus.ErrorMessage $taskStatus.Links.Web
exit 1
}
}
while($taskStatus.State -ne "Success")
Write-Host "."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment