Automating Cloud Service Deployment in Azure

The build and deployment of Cloud Services can be automated, so your continuous integration server can push updates automatically to your (test) infrastructure.

First of all, build the solution, and use CSPack to package the deployment:

%WINDIR%\Microsoft.NET\Framework\v4.0.30319\MSBuild SOLUTION.sln /p:Configuration=Release /t:Publish

You will need to update the command with the name of your solution.

Then using PowerShell, upload to staging, wait for the instances to start, swap staging and production slots, and delete the staging environment.

$path = (Get-location).Path + "\Azure\bin\Release\app.publish"
$hostedService = "YOUR_HOSTED_SERVICE"

# deploy the package to staging slot
New-AzureDeployment -ServiceName $hostedService -Package "$path\ServiceDefinition.cspkg" -Configuration "$path\ServiceConfiguration.Cloud.cscfg" -Slot Staging

do {

	# query the status of the running instances
	$list = (Get-AzureRole -ServiceName $hostedService -Slot Staging -InstanceDetails).InstanceStatus 

	# count the number of ready instances
	$list | foreach-object { IF ($_ -eq "ReadyRole") { $ready++ } }

	# count the number in total
	$list | foreach-object { $total++ } 

	"$ready out of $total instances are ready"

	# sleep for 10 seconds
	Start-Sleep -s 10
while ($ready -ne $total)

# swap staging and production
Move-AzureDeployment -ServiceName $hostedService

# remove the staging slot
Remove-AzureDeployment -ServiceName $hostedService -Slot Staging -Force