Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created March 25, 2013 00:49
Show Gist options
  • Save PaulStovell/5234255 to your computer and use it in GitHub Desktop.
Save PaulStovell/5234255 to your computer and use it in GitHub Desktop.
This is the bundled version of the Windows Azure deployment process used by Octopus.
## Octopus Azure deployment script, version 1.0
## --------------------------------------------------------------------------------------
##
## 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 WaitForComplete()
{
$completeDeployment = Get-AzureDeployment -ServiceName $OctopusAzureServiceName -Slot $OctopusAzureSlot
$completeDeploymentID = $completeDeployment.DeploymentId
Write-Host "Deployment complete; Deployment ID: $completeDeploymentID"
}
CreateOrUpdate
WaitForComplete
@johnkors
Copy link

Hi Paul,

say you have two steps (which cannot have the same name in the Portal) in a release plan with the step names

  1. Deploy to staging slot
  2. Promote from staging slot

The deployment label of the initial deployment from 1 is generated to be: Deploy to staging slot - v.1.2.3.4
The deployment label of the second step will be generated as Promote from staging slot - v.1.2.3.4

Won't this have the effect that the SwapDeployment will never occur?

if ($staging.Label -eq $OctopusAzureDeploymentLabel) {
                SwapDeployment
                return
}

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