Skip to content

Instantly share code, notes, and snippets.

@nordineb
Forked from plillevold/DeployToAzure.ps1
Created March 13, 2014 16:12
Show Gist options
  • Save nordineb/9531411 to your computer and use it in GitHub Desktop.
Save nordineb/9531411 to your computer and use it in GitHub Desktop.
## --------------------------------------------------------------------------------------
##
## 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment