Skip to content

Instantly share code, notes, and snippets.

@tumtumtum
Created July 3, 2015 14:50
Show Gist options
  • Save tumtumtum/f67e62ba3218ed8103a9 to your computer and use it in GitHub Desktop.
Save tumtumtum/f67e62ba3218ed8103a9 to your computer and use it in GitHub Desktop.
Octopus Deploy to Azure Website with Staging support
Import-Module 'C:\Program Files (x86)\Microsoft SDKs\azure\PowerShell\ServiceManagement\azure\Azure.psd1'
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Deployment")
$azureCertificatePassword = $OctopusParameters["AzureCertificatePassword"]
[byte[]]$certificatebytes = [Convert]::FromBase64String($OctopusParameters["AzureCertificate"])
$certificate = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList @($certificatebytes,$azureCertificatePassword)
$subscriptionId = $OctopusParameters["AzureSubscriptionId"]
$subscriptionName = $OctopusParameters["AzureSubscriptionName"];
Write-Host "Setting Azure Subscription $subscriptionName"
Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionId -Certificate $certificate
# A collection of functions that can be used by script steps to determine where packages installed
# by previous steps are located on the filesystem.
function Find-InstallLocations {
$result = @()
$OctopusParameters.Keys | foreach {
if ($_.EndsWith('].Output.Package.InstallationDirectoryPath')) {
$result += $OctopusParameters[$_]
}
}
return $result
}
function Find-InstallLocation($stepName) {
$result = $OctopusParameters.Keys | where {
$_.Equals("Octopus.Action[$stepName].Output.Package.InstallationDirectoryPath", [System.StringComparison]::OrdinalIgnoreCase)
} | select -first 1
if ($result) {
return $OctopusParameters[$result]
}
throw "No install location found for step: $stepName"
}
function Find-SingleInstallLocation {
$all = @(Find-InstallLocations)
if ($all.Length -eq 1) {
return $all[0]
}
if ($all.Length -eq 0) {
throw "No package steps found"
}
throw "Multiple package steps have run; please specify a single step"
}
function Test-LastExit($cmd) {
if ($LastExitCode -ne 0) {
Write-Host "##octopus[stderr-error]"
write-error "$cmd failed with exit code: $LastExitCode"
}
}
$stepName = $OctopusParameters['WebDeployPackageStepName']
$stepPath = ""
if (-not [string]::IsNullOrEmpty($stepName)) {
Write-Host "Finding path to package step: $stepName"
$stepPath = Find-InstallLocation $stepName
} else {
$stepPath = Find-SingleInstallLocation
}
Write-Host "Package was installed to: $stepPath"
Write-Host "##octopus[stderr-progress]"
$websiteName = $OctopusParameters['WebsiteName']
$publishUrl = $OctopusParameters['PublishUrl']
$stagingWebsiteName = $websiteName + "-staging"
$stagingPublishUrl = $publishUrl -replace "\.scm\.","-staging.scm."
Write-Host "Publishing $websiteName to staging at $stagingPublishUrl as $stagingWebsiteName"
$destBaseOptions = new-object Microsoft.Web.Deployment.DeploymentBaseOptions
$destBaseOptions.UserName = $OctopusParameters['Username']
$destBaseOptions.Password = $OctopusParameters['Password']
$destBaseOptions.ComputerName = "https://$stagingPublishUrl/msdeploy.axd?site=$stagingWebsiteName"
$destBaseOptions.AuthenticationType = "Basic"
$syncOptions = new-object Microsoft.Web.Deployment.DeploymentSyncOptions
$syncOptions.WhatIf = $false
$syncOptions.UseChecksum = $true
$enableAppOfflineRule = $OctopusParameters['EnableAppOfflineRule']
if($enableAppOfflineRule -eq $true)
{
$appOfflineRule = $null
$availableRules = [Microsoft.Web.Deployment.DeploymentSyncOptions]::GetAvailableRules()
if (!$availableRules.TryGetValue('AppOffline', [ref]$appOfflineRule))
{
throw "Failed to find AppOffline Rule"
}
else
{
$syncOptions.Rules.Add($appOfflineRule)
Write-Host "Enabled AppOffline Rule"
}
}
$deploymentObject = [Microsoft.Web.Deployment.DeploymentManager]::CreateObject("contentPath", $stepPath)
$deploymentObject.SyncTo("contentPath", $websiteName, $destBaseOptions, $syncOptions)
Write-Host "Switching $websiteName from Staging to Production"
Switch-AzureWebsiteSlot -Name $websiteName -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment