Skip to content

Instantly share code, notes, and snippets.

@plillevold
Forked from PaulStovell/DeployToAzure.ps1
Last active August 29, 2015 13:56
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save plillevold/9068433 to your computer and use it in GitHub Desktop.
Save plillevold/9068433 to your computer and use it in GitHub Desktop.
Modified version of the bundled Windows Azure deployment process used by Octopus 2.0. This version will do a deploy to Staging, VIP swap to Production and then delete the Staging slot
## --------------------------------------------------------------------------------------
##
## This script is used to control how we deploy packages to Windows Azure.
## Orignial script (https://gist.github.com/PaulStovell/5234255) customized for
## Staging-To-Production with VIP swap deployment.
##
## NOTE: the script will only do Staging+VIP swap if $OctopusAzureSlot == "Staging".
## If $OctopusAzureSlot == anything else, the script will deploy directly to that slot
## without any further swapping.
##
## NOTE2: the script waits for all instances to become ready before VIP swap is performed.
## (code borrowed from https://gist.github.com/chartek/5265057)
##
##
## When the script is run, the correct Azure subscription will ALREADY be selected,
## and we'll have loaded the neccessary management certificates. The Azure PowerShell module
## will also be loaded.
##
## If you want to customize the Azure deployment process, simply copy this script into
## your NuGet package as DeployToAzure.ps1. Octopus will invoke it instead of the default
## script.
##
## The script will be passed the following parameters in addition to the normal Octopus
## variables passed to any PowerShell script.
##
## $OctopusAzureSubscriptionId // The subscription ID GUID
## $OctopusAzureSubscriptionName // The random name of the temporary Azure subscription record
## $OctopusAzureServiceName // The name of your cloud service
## $OctopusAzureStorageAccountName // The name of your storage account
## $OctopusAzureSlot // The name of the slot to deploy to (Staging or Production)
## $OctopusAzurePackageUri // URI to the .cspkg file in Azure Blob Storage to deploy
## $OctopusAzureConfigurationFile // The name of the Azure cloud service configuration file to use
## $OctopusAzureDeploymentLabel // The label to use for deployment
## $OctopusAzureSwapIfPossible // (IGNORED) "True" if we should attempt to "swap" deployments rather than a new deployment
##
## Pass in the following custom variables:
## $DeleteStagingAfterDeployment // "True" if we should delete the Staging slot after successfull swap to Prodution
$SwapStagingToProduction = ($OctopusAzureSlot -eq "Staging")
function CreateOrUpdate([string] $slot)
{
$deployment = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
if (($a[0] -ne $null) -or ($deployment.Name -eq $null))
{
CreateNewDeployment $slot
return
}
UpdateDeployment $slot
}
function SwapDeployment()
{
Write-Host "Swapping the staging environment to production"
Move-AzureDeployment -ServiceName $OctopusAzureServiceName
}
function DeleteStagingDeployment()
{
Write-Host "Deleting the staging environment"
Remove-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot "Staging" -Force
}
function UpdateDeployment([string] $slot)
{
Write-Host "A deployment already exists in $OctopusAzureServiceName for slot $slot. Upgrading deployment..."
Set-AzureDeployment -Upgrade -ServiceName $OctopusAzureServiceName -Package $OctopusAzurePackageUri -Configuration $OctopusAzureConfigurationFile -Slot $slot -Mode Auto -label $OctopusAzureDeploymentLabel -Force
}
function CreateNewDeployment([string] $slot)
{
Write-Host "Creating a new deployment..."
New-AzureDeployment -Slot $slot -Package $OctopusAzurePackageUri -Configuration $OctopusAzureConfigurationFile -label $OctopusAzureDeploymentLabel -ServiceName $OctopusAzureServiceName
}
function WaitForComplete([string] $slot)
{
$completeDeployment = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot $slot
$completeDeploymentID = $completeDeployment.DeploymentId
Write-Host "Deployment complete; Deployment ID: $completeDeploymentID"
}
function WaitForRoleInstancesReady([string] $slot)
{
Write-Host "Waiting for all instances in $slot to report ready..."
while ($true)
{
$roles = Get-AzureRole –ServiceName $OctopusAzureServiceName –Slot $slot -InstanceDetails
if ($roles -eq $null -or $roles.Count -eq 0)
{
Write-Host "There are no roles in $OctopusAzureServiceName for slot $slot. Skipping..."
break
}
$rolesNotReady = $roles | Where-Object { $_.InstanceStatus -ne "ReadyRole" }
if ($rolesNotReady -eq $null)
{
Write-Host "All role instances are now ready"
break
}
[System.Threading.Thread]::Sleep([System.TimeSpan]::FromSeconds(30))
}
}
if ($SwapStagingToProduction -eq $true) {
# Deploy to Staging, vip-swap to Production
Write-Host "Starting Staging-to-Production deployment using VIP swap..."
CreateOrUpdate "Staging"
WaitForComplete "Staging"
WaitForRoleInstancesReady "Staging"
SwapDeployment
WaitForComplete "Production"
if ($DeleteStagingAfterDeployment -eq $true) {
DeleteStagingDeployment
}
}else {
# Deploy directly to $OctopusAzureSlot
Write-Host "Starting direct to $OctopusAzureSlot deployment... (no swap or slot delete will be performed)"
CreateOrUpdate $OctopusAzureSlot
WaitForComplete $OctopusAzureSlot
}
@pauliusnorkus
Copy link

Hi Peter,
I'm getting error using this script.
Get-AzureRole : "An exception occurred when calling the ServiceManagement API. HTTP Status Code: 400. Service Management Error Code: BadRequest. Message: The hosted service name is invalid.

Is it possible that script tries to get role, but deployment on the azure side haven't started yet?

@tumtumtum
Copy link

@pauliusnorkus I had the same problem. I updated WaitForRoleInstanceReady to handle this situation. https://gist.github.com/tumtumtum/09acc1f2385575484716

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment