Mike Hodnick's Blog

Automated Azure Deployments with TFS Build

I recently had the pain pleasure of setting up automatic Azure deployments as part of our TFS build process for an ASP.NET MVC app. There is a fair amount of documentation out there already on the tasks required to get this working, but you have to connect a few dots and correct a few problems along the way.

First read Dominic Green’s post on the topic: http://blogs.msdn.com/domgreen/archive/2009/09/29/deploying-to-the-cloud-as-part-of-your-daily-build.aspx. For the most part, his post covers about 90% of what you need to know.

One “gotcha” is with certificates and the TFS build server. You will need to create a certificate on the build machine and upload it to Azure management API certificate area. However you will likely run into an “Anonymous” scheme error and will need to follow the guidelines that Alexander Strauss lays out in his post http://blogs.msdn.com/astrauss/archive/2010/01/28/tales-about-working-with-the-azure-service-management-api-chapter-i.aspx:

…in my first try I run into an error that I was not able to get my Service Management API requests authenticated. The service always returned:

Error 403 (Forbidden): The HTTP request was forbidden with client authentication scheme ‘Anonymous’.

In order to have the requests working you need to meet two prerequisites for your certificate:

  1. The certificate needs to be installed in the certificate store of the client machine from where you send the requests to the Service Management API.
  2. The certificate must have an associated private key.

Certificates in Windows are kind of magical to me and I’m glad there are folks out there who have already dug in to that stuff.

Last, you will need to modify the source code of the csmanage.exe tool referenced by Dominic. You can get the csmanage.exe tool here: http://code.msdn.microsoft.com/Wiki/View.aspx?ProjectName=windowsazuresamples. The problem with csmanage.exe at the time of this writing is that it incorrectly returns an exit code of “1” when the operations run successfully and an exit code of “0” when there are errors. In the Main() method of csmanage.exe you can find this code:

The problem is that Program.ExecuteActions() returns a bool that represents a “hasErrors” value internally. Thus, if “hasErrors” is false, then the program should return “0”. You can simply modify the source code and change the condition to deal with this:

Happy deploying!