Customizing site deployment based on site's app settings in Azure Web Apps (Websites)

In previous posts I talked about Microsoft Azure Web App's custom deployment feature and the tool to easily generate a deployment script, in this post I'll do a step by step guide on writing a custom deployment script.

Let's say we have two websites in azure, one is a node.js website and the other is an mvc4 web application, both sites sources are in the same repository so we need a deployment script that handles differently based on which website it is deploying.

Full sample repository

Some Prerequisites:

  1. Assuming git is installed and the websites are in a local git repository.
  2. Install node.js.
  3. Install azure-cli by running the following command: npm install azure-cli -g

(a) First let's generate a deployment script for the node.js website:

  • Go to the root of the repository.
  • Enter the following command: azure site deploymentscript --node --sitePath nodejs
  • Where "nodejs" is the path to the node.js website directory.

  • Notice the files that were generated:

    • .deployment - a file telling which command to run for deployment (currently deploy.cmd).
    • deploy.cmd - the deployment script.
    • nodejs\web.config - configuration for iis to run node.js.
    • nodejs\iisnode.yml - this file allows some configuration settings relating to node.js, more info on iisnode.yml
  • Rename deploy.cmd to deploy.node.cmd: move deploy.cmd deploy.node.cmd

(b) Let's generate a deployment script for the mvc4 web application:

At the root of the repository enter the command:

azure site deploymentscript --aspWAP mvc4\Mvc4WebApplication\Mvc4WebApplication.csproj -s mvc4\Mvc4WebApplication.sln

  • Rename deploy.cmd to deploy.mvc4.cmd: move deploy.cmd deploy.mvc4.cmd

NOTE: You can also edit this generated file (deploy.cmd) with any custom steps you have, you can also test it on your machine simply by running it, it will publish your website to %REPOSITORY_ROOT%\artifacts.

(c) To decide which script should run, based on the website we are currently deploying, we'll use the "app settings" feature in Microsoft Azure Web Apps:

  • Create a deploy.cmd file under the root of the repository with the following:
  • %SITE_FLAVOR% will be set by Windows Azure with the value we'll give it in the management portal.

  • Add all generated files and commit them to the repository.

(d) Now let's try to push our repository to our Microsoft Azure Web App:

git push WA master

  • We receive an error and the deployment fails since we still haven't set the app setting yet, so let's do that.

  • Go to the website on windows azure management portal and add under the CONFIGURATION tab under "app settings" a setting with name SITE_FLAVOR and value nodejs/mvc3 (based on the current site we're configuring).

  • Click on the "Save" button.

  • Now we can either push our changes again (we'll need a new commit, even an empty one, otherwise it'll tell us that nothing has changed and the deployment won't reinitiate).

  • Or we can go to the DEPLOYMENTS tab in Windows Azure portal, select the last deployment which failed and push the RETRY button to retry the deployment.

That's it, now we have a working mvc4/node.js website

NOTE: Another improvement we could do here is to store the repository on GitHub/Bitbucket and connect them to our 2 sites, now every time we push to GitHub/Bitbucket, both of our sites will be deployed.

The repository I've used can be cloned from here.

comments powered by Disqus