Azure Websites are designed to provide a turnkey solution for getting a web application up and running on the net. Build it, and then either push it to a connected service such as GitHub or Bitbucket, or to a specialised deployment Git repository and you’re set.

However this simplicity comes at a cost of reduced flexibility. If you’re building a continuous integration server you probably won’t want to have to maintain a separate Git repository just to deploy your app, and deploying straight from GitHub or another similar service isn’t really an option if you want to have a controlled build chain that you’re managing yourself.

However it is entirely possible to integrate Azure Websites with a CI system by using MSBuild and MSDeploy. Here’s how:

This requires a simple call to your source control repository. For Git, we do the following:

I put all of my CI-related files into a folder called CI in the server user’s folder. You can of course put them wherever you want, so long as the user has the appropriate rights to perform all of the necessary actions in the location where the files are stored.

Quick note here before we continue. Notice that we’re checking out origin/master and not pulling. This updates our code to the latest commit on the master branch of the remote server. Pulling takes the changes on the remote server and merges them into our local version of the branch. Normally on a CI server these two actions would give us the same result, but there is no guarantee of such. Checking out origin/master is semantically exactly what we want to do.

This step is a bit tricky because MSBuild has so many different options and therefore there are many ways that you can get this step wrong. What we actually need here is not to build the project, but to publish it. Building the project won’t transform web.config files, for example. Publishing it to the filesystem will give us exactly what we need to send to Azure.

To do this, run MSBuild with the following options.

This will publish the project to the build folder of your choosing. At this point you could run IIS on this directory and your app would be up and running.

To retrieve the values for [PublishUrl] and [DeployPassword] you’ll need to sign into the Azure Management Portal, open up your site, and click on “Get Publish Profile”. This will download a file that you can then open up in notepad.
In the first <publishProfile> element there is an attribute called publishUrl that contains a url pointing to port 443. This is the URL you need. If the URL doesn’t start with https://, be sure to add it. This same element also contains an attribute called userPWD, which is the [Password] that you need to inject into the above command.