Change vmsize for a azure cloud service in octopus deploy

A common cases when running a test/staging and production environment using Azure Cloud Service is that you don’t want to use the same vmsize for all environments. It can actual be very expensive. To solve this we need to-do some variable substitution in the azure cloud service package. Doing variable substitution on ServiceConfiguration.XXX.cscfg is done by default when using the built in template “Deploy an Azure Cloud Service” but it doesn’t support variable substitution on ServiceDefinition.csde by default where the vmsize is defined. To accomplish this we need to create a pre-deployment PowerShell script.

1. In you process step based on “Deploy an Azure Cloud Service” click on enable features
2. Check mark “Custom deployment scripts” and apply the change
3. You can now add script for pre-deployment, deployment, and post-deployment.
4. The last thing is to add the variable Deploy.WebApiRoleVMSize setting the vmsize you want for different scope or environments.

Add this scrip to the “Pre-deployment” section and replace “NNN” with the name of the webrole.

write-host "Starting Service Definition rewrite" 
 
$def = "ServiceDefinition\ServiceDefinition.csdef"
 
$csdeffile = Get-Item $def 
[xml]$csdefcontent = get-content $csdeffile 
 
#Find the webrole we want in a multi-webrole cloud service. Assume this exists 
$webrole = $csdefcontent.ServiceDefinition.WebRole | where {$_.name -eq "NNNN"}
 
##Update the vmsize. Add if not available 
$webrole.SetAttribute("vmsize",$OctopusParameters['Deploy.WebApiRoleVMSize']) 
write-host "Using VMSize '" $OctopusParameters['Deploy.WebApiRoleVMSize'] "' for NNNNN"
 
#Save the changes 
$csdefcontent.Save($csdeffile.FullName)
 
write-host "Service Definition rewrite successful"