Continous Delivery: full script for deploying Azure WebRole and WorkerRole from Powershell

After I posted Windows Azure Deployment – Problem and Solution #1 and Windows Azure Deployment – Problem and Solution #2 here last week, one of my blog readers emailed me whether or not I have the full script for deploying the WebRole or Worker Role on Windows Azure.

Yes. I still have it but I stopped using webrole and worker role because deploying web role or worker role takes so much time (around 20-40 min) which is unacceptable for us. Anyways,  if 20-40 min deployment time is not your concern then sure, you can use it.

I wrote and tested this script with Azure SDK 2.1 on Windows 2012 server.

You can download it from my github repository as well. Here is the link https://github.com/michaelsync/Michael-Sync-s-blog-sample/blob/master/azure_deploy.ps1


#Modified and simplified version of https://www.windowsazure.com/en-us/develop/net/common-tasks/continuous-delivery/

$thumbprint = "{Your Cert's Thumbprint}"
$myCert = Get-Item cert:\\CurrentUser\My\$thumbprint
$subscriptionId = "{Your Subscription Id}"
$subscriptionName = "{Your Subscription Name}"
$webroleservice = "{Your Web Role Name}"
$workerroleservice = "{Your Worker Role Name}"

$slot = "staging" #staging or production

$package = "{Path of your Azure project}\bin\Release\app.publish\{Your Project}.cspkg"
$configuration = {Path of your Azure project}\bin\Release\app.publish\ServiceConfiguration.Cloud.cscfg"

$timeStampFormat = "g"

Write-Output "Running Azure Imports"
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1"
Import-AzurePublishSettingsFile "{Path where you stored your azure setting\Azure.publishsettings"

function Publish(){
 PublishInternal $webroleservice
 PublishInternal $workerroleservice
}

function PublishInternal($service){

Write-Output "Publising"
 Write-Output $service

Set-AzureSubscription -CurrentStorageAccount $service -SubscriptionName $subscriptionName -SubscriptionId $subscriptionId -Certificate $myCert
 Write-Output "Set-AzureSubscription"
 $deploymentLabel = "ContinuousDeploy to $service v%build.number%"

Write-Output $deploymentLabel

$deployment = Get-AzureDeployment -ServiceName $service -Slot $slot -ErrorVariable a -ErrorAction silentlycontinue
 Write-Output a

if ($a[0] -ne $null) {
 Write-Output "$(Get-Date -f $timeStampFormat) - No deployment is detected. Creating a new deployment. "
 }

 if ($deployment.Name -ne $null) {
 #Update deployment inplace (usually faster, cheaper, won't destroy VIP)
 Write-Output "$(Get-Date -f $timeStampFormat) - Deployment exists in $servicename. Upgrading deployment."
 UpgradeDeployment $service $deploymentLabel
 } else {
 CreateNewDeployment $service $deploymentLabel
 }
}
function CreateNewDeployment($service, $deploymentLabel)
{
 write-progress -id 3 -activity "Creating New Deployment" -Status "In progress"
 Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: In progress"

$opstat = New-AzureDeployment -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service

$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
 $completeDeploymentID = $completeDeployment.deploymentid

write-progress -id 3 -activity "Creating New Deployment" -completed -Status "Complete"
 Write-Output "$(Get-Date -f $timeStampFormat) - Creating New Deployment: Complete, Deployment ID: $completeDeploymentID"
}

function UpgradeDeployment($service, $deploymentLabel)
{
 write-progress -id 3 -activity "Upgrading Deployment" -Status "In progress"
 Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: In progress"

# perform Update-Deployment
 $setdeployment = Set-AzureDeployment -Upgrade -Slot $slot -Package $package -Configuration $configuration -label $deploymentLabel -ServiceName $service -Force

$completeDeployment = Get-AzureDeployment -ServiceName $service -Slot $slot
 $completeDeploymentID = $completeDeployment.deploymentid

write-progress -id 3 -activity "Upgrading Deployment" -completed -Status "Complete"
 Write-Output "$(Get-Date -f $timeStampFormat) - Upgrading Deployment: Complete, Deployment ID: $completeDeploymentID"
}

Write-Output "Create Azure Deployment"
Publish

Here is the log for deploying webrole from Visual Studio.

New Deployment

10:56:32 PM – Warning: There are package validation warnings.
10:56:32 PM – Checking for Remote Desktop certificate…
10:56:33 PM – Uploading Certificates…
10:56:51 PM – Preparing deployment for MvcApplication1.Azure – 10/7/2013 10:56:06 PM with Subscription ID ‘1775dfasa8a81fd58’ using Service Management URL ‘https://management.core.windows.net/’…
10:56:51 PM – Connecting…
10:56:51 PM – Verifying storage account ‘mymvctestx01’…
10:56:52 PM – Uploading Package…
11:04:04 PM – Creating…
11:04:54 PM – Created Deployment ID: 337b850dfa3c4dd38dd35441b5b8e337.
11:04:54 PM – Instance 0 of role MvcApplication1 is stopped
11:04:55 PM – Starting…
11:05:45 PM – Initializing…
11:05:45 PM – Instance 0 of role MvcApplication1 is creating the virtual machine
11:06:18 PM – Instance 0 of role MvcApplication1 is starting the virtual machine
11:07:57 PM – Instance 0 of role MvcApplication1 is busy
11:09:37 PM – Instance 0 of role MvcApplication1 is ready
11:09:37 PM – Created Website URL: http://mymvctestx01.cloudapp.net/
11:09:37 PM – Complete.

Upgrading

11:12:40 PM – Warning: There are package validation warnings.
11:12:40 PM – Checking for Remote Desktop certificate…
11:12:42 PM – Preparing deployment for MvcApplication1.Azure – 10/7/2013 11:12:14 PM with Subscription ID ‘17751asdfs8a81fd58’ using Service Management URL ‘https://management.core.windows.net/’…
11:12:42 PM – Connecting…
11:12:42 PM – Verifying storage account ‘mymvctestx01’…
11:12:44 PM – Uploading Package…
11:20:08 PM – Updating…
11:23:19 PM – Instance 0 of role MvcApplication1 is ready
11:23:21 PM – Starting…
11:23:39 PM – Initializing…
11:23:40 PM – Created Website URL: http://mymvctestx01.cloudapp.net/
11:23:40 PM – Complete.

Generally, it takes around 10 to 20 mins but sometimes, it takes around 40 mins. I am not the only one who experienced this problem. You search about it online. Hope that Azure team will do something about it.

3 thoughts on “Continous Delivery: full script for deploying Azure WebRole and WorkerRole from Powershell

  1. However there is a catch: whilst you should be able to benefit from the speedy LTE network,
    your iPhone won’t work over AT&T or T-Mobile’s GSM-primarily
    based voice and textual content messaging network.

Leave a Reply

Your email address will not be published. Required fields are marked *