Skip to content

Instantly share code, notes, and snippets.

@stevenkuhn
Forked from PaulStovell/DeployToAzure.ps1
Last active December 15, 2015 13:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stevenkuhn/5265057 to your computer and use it in GitHub Desktop.
Save stevenkuhn/5265057 to your computer and use it in GitHub Desktop.
## Octopus Azure deployment script, version 1.1
## --------------------------------------------------------------------------------------
##
## This script is used to control how we deploy packages to Windows Azure.
##
## 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 // "True" if we should attempt to "swap" deployments rather than a new deployment
function CreateOrUpdate()
{
$deployment = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot $OctopusAzureSlot -ErrorVariable a -ErrorAction silentlycontinue
if (($a[0] -ne $null) -or ($deployment.Name -eq $null))
{
CreateNewDeployment
return
}
if (($OctopusAzureSwapIfPossible -eq $true) -and ($OctopusAzureSlot -eq "Production"))
{
Write-Host "Checking whether a swap is possible"
$staging = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot "Staging" -ErrorVariable a -ErrorAction silentlycontinue
if (($a[0] -ne $null) -or ($staging.Name -eq $null))
{
Write-Host "Nothing is deployed in staging"
}
else
{
Write-Host ("Current staging deployment: " + $staging.Label)
if ($staging.Label -eq $OctopusAzureDeploymentLabel) {
SwapDeployment
return
}
}
}
UpdateDeployment
}
function SwapDeployment()
{
Write-Host "Swapping the staging environment to production"
Move-AzureDeployment -ServiceName $OctopusAzureServiceName
}
function UpdateDeployment()
{
Write-Host "A deployment already exists in $OctopusAzureServiceName for slot $OctopusAzureSlot. Upgrading deployment..."
Set-AzureDeployment -Upgrade -ServiceName $OctopusAzureServiceName -Package $OctopusAzurePackageUri -Configuration $OctopusAzureConfigurationFile -Slot $OctopusAzureSlot -Mode Auto -label $OctopusAzureDeploymentLabel -Force
}
function CreateNewDeployment()
{
Write-Host "Creating a new deployment..."
New-AzureDeployment -Slot $OctopusAzureSlot -Package $OctopusAzurePackageUri -Configuration $OctopusAzureConfigurationFile -label $OctopusAzureDeploymentLabel -ServiceName $OctopusAzureServiceName
}
function WaitForDeploymentComplete()
{
$completeDeployment = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot $OctopusAzureSlot
$completeDeploymentID = $completeDeployment.DeploymentId
Write-Host "Deployment complete; Deployment ID: $completeDeploymentID"
}
function WaitForRoleInstancesReady()
{
Write-Host "Waiting for all role instances to report ready..."
while ($true)
{
$roles = Get-AzureRole –ServiceName $OctopusAzureServiceName –Slot $OctopusAzureSlot -InstanceDetails
if ($roles -eq $null -or $roles.Count -eq 0)
{
Write-Host "There are no roles in $OctopusAzureServiceName for slot $OctopusAzureSlot. 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))
}
}
CreateOrUpdate
WaitForDeploymentComplete
WaitForRoleInstancesReady
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment