Deploying Cloud Services with TeamCity and Octopus Deploy

This is an older post published on my blog on Jan 23 2015, I’m moving a few old posts over here.

What are we doing?

We’re going to use our continuous integration pipeline to build, test and deploy an Azure Cloud Service. The process we work with requires that work isn’t just pushed into production before going through a review process; as such it’s quiet important that the steps to get the application deployed when reviewed is as simple as posible. This is part of a larger system they will move our code production from a single press of the “Approve” button.

  1. You are using TeamCity.
  2. You are using Octopus Deploy.
  3. You’re using NUnit for your tests. This is the step I’ll show here but you can insert your own test runner steps. (You are unit testing this, right?)

Setting the project up

We need to start in visual studio. Sadly we can’t just use octopack like we would with other projects as you can’t currently add nuget projects to a “.ccproj” file. We’ll start by creating a folder called “deployment” within our cloud service project. Within this folder we’ll place a nuspec file that is going to be used to tell teamcity what files to add to the nuget package.

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata>
<id>[Package ID]</id>
<version>1.0</version>
<authors>[Authors]</authors>
<description>Cloud Service Package</description>
</metadata>
<files>
<file src="..\bin\Release\app.publish\*.*" target="" />
</files>
</package>

TeamCity

Hopefully this doesn’t seem like I’m skipping over things but I’m not sure this is the place to explain TeamCity in great detail. You need to:

  1. Add a build configuration for this deployment.
  2. Attach your repository settings and any triggers you want.
  1. I set my “configuration” to “release”. This is your transforms.
  2. Finally I set “command line parameters” to “/p:TargetProfile=Cloud /p:Configuration=Release”. Similar to configuration but it also passes the cloud service target.
  3. I stare longingly at the octopack option that would make this so much simpler.
  1. I set the “command line parameters” to “/t:Publish /p:TargetProfile=Cloud /p:Configuration=Release /p:PublishDir=bin\Release\app.publish\”
  2. Again, I stare longingly at the octopack option that would make this so much simpler.
  1. Give it a version number. We do something clever with automatic versioning but you can put your version variable in here until I get around to writing that process up!
  2. Set your output folder. This is going to depend on how your going to publish the nuget file. I set mine to “Build/packages” because I’ve set “Artifact paths” to “Build/packages/** =>” in General Settings. If you have nuget enabled for TeamCity then this will put your packages in that feed.
  3. Finally in “Properties” I set “Configuration=Release”.

Octopus

In octopus you need to create yourself a project and go to the process tab on the left, keep a not of the name for later. Click to add a step and then choose “Deploy to Windows Azure”. When this step opens we’ll need to set some stuff up.

  1. Select your package. You need to have configured the feed inside octopus. If you have it’ll give you some help by listing the different packages.
  2. “Subscription id” is pretty self explanatory, you get this from your Azure subscription portal.
  3. “Cloud service” is the name of your cloud service you created in the portal earlier.
  4. “Storage account” this is where octopus is going to upload your nuget package before deploying it into the cloud service.
  5. Follow the instructions for adding the certificate if this is your first time.
  6. Finally you get to configure the slot you want to deploy into. Here I’ve just got “production” at the moment.
  7. Instance count is interesting, you can choose wether to ignore the scaling configuration in your project if Azure has already scaled your service further. This make most sense for deploying over the top of a service, you wouldn’t want to be under heavy load and have the service kill lots of instances.
  8. Select your environment and save.

Back to TeamCity

In the build configuration from before you need one final step, add a “Deploy Release” step. The first thing you’ll need to set in here is the location of your octopus server and the api key that you can get from your user profile on there. Then you can add:

  1. Your release number to “Release number”, this is probably similar to your build number before.
  2. Your environment name depending where your deploying. Mine is “production”.
  3. You can pass extra command line parameters. I send my package number as I don’t always just want the latest: “ — packageversion %Major.Minor.Build%.%build.number%”.