Deploy to Azure with Powershell
Last time I wrote about this, I had a very simple deploy scenario. Now I have updated my deployment workflow, which calls for an updated blog post.
Working with Azure you really need to automate your deployment, because there are so many steps involved from building packages, uploading them and swapping environments. Your local environment also differs alot from your hosted Azure, and that demands heavy testing in your staging environment.
Here's my deploy workflow.
If you run your deployment in a build servier, the "Was staging OK?" step would probably involve some Watin/Selenium tests. I trigger my deploy script manually and verify staging through Internet Explorer.
This deployment workflow will always delete staging at the end. This is because I have no value in a staging environment laying around. Instead it will only cost me money and no gain. That is why I spin up a new staging environment when I want to release something, and delete it when I'm done. All, automated of course.
Powershell
Powershell is the obvious choice for automating this since there are cmdlets which simplify communication with the Azure API alot.
I have omitted the psake parts for readability. Sorry for the long lines.
HELPER FUNCTION
Function Get-Staging ([string]$serviceName, [string]$subscriptionId, [System.Security.Cryptography.X509Certificates.X509Certificate2]$cert) { Get-HostedService -serviceName $serviceName -subscriptionId $subscriptionId -certificate $cert | Get-Deployment -slot Staging }
VARIABLES
$biniexplore = "C:\Program Files (x86)\Internet Explorer\iexplore.exe" $azureservice = "litemediainfo" $azuresub = "aacbcab88-4f34ef4-8330-090abf3c3b" $azurecert = Get-Item "cert:\CurrentUser\My\089E87E963B472906F6345345345NBMNBN345NBBBB" $azurepackage = "build\Deploy\LiteMedia.Web.Azure.cspkg" $azureconfig = "build\Deploy\ServiceConfiguration.cscfg" $azurelabel = "litemedia.info " + [System.DateTime]::Now.ToString() $azurerole = "litemedia.info" $azure_storage = "litemedia"
DEPLOY WORKFLOW STARTS HERE
GET STAGING ENVIRONMENT
$staging = Get-Staging $azureservice $azuresub $azure_cert
FOUND STAGING
if ($staging.Url) { # YES Write-Host "Upgrade existing staging environment" $staging | Set-Deployment -mode Auto -package $azurepackage -label $azurelabel -StorageServicename $azurestorage -configuration $azureconfig | Get-OperationStatus –WaitToComplete } else { # NO Write-Host "Create new staging environment" New-Deployment -serviceName $azureservice -subscriptionId $azuresub -certificate $azurecert -slot Staging -package $azurepackage -label $azurelabel -StorageServicename $azurestorage -configuration $azure_config | Get-OperationStatus -WaitToComplete }
GET STAGING ENVIRONMENT
$staging = Get-Staging $azureservice $azuresub $azure_cert
Write-Host "Startup Staging" $staging | Set-DeploymentStatus running | Get-OperationStatus –WaitToComplete
Write-Host "Starting browser for staging review" &$bin_iexplore $staging.Url $deploy = Read-Host "Move staging to production? (y/N)"
WAS STAGING OK?
if ($deploy -eq 'y') { # YES
Write-Host "Swapping staging for production" $staging | Move-Deployment | Get-OperationStatus –WaitToComplete }
Write-Host "Suspend staging environment" $staging | Set-DeploymentStatus suspended | Get-OperationStatus –WaitToComplete
Write-Host "Remove staging environment" Remove-Deployment -slot Staging -serviceName $azureservice -subscriptionId $azuresub -certificate $azure_cert | Get-OperationStatus –WaitToComplete