;

Enabling WebDeploy in an Auto-Deployed Azure Cloud Service

Posted : Monday, 31 March 2014 21:49:00

I’ve just started a fantastic new job at truRating – it’s a really cool startup and 5 weeks in I am loving it. Its a really great team, great product and loads and loads of cool Tech. A large number of the components which comprise out application ecosystem are C# projects of one type or another and for the most part they’re hosted in Windows Azure. After establishing the environments we will initially support and an effective naming strategy for the various components (essentially based around a <brand>-<environment>-<product> pattern), we’re currently finalising our deployment/release pipeline.

My colleague Toby Reid did an excellent job of setting up TeamCity to build and deploy each of the components that comprise an environment and while this is very effective and well managed, there is a significant lead time (>10minutes) between the build being triggered and the code being available for any of the team to test/review/enjoy etc. This is principally down to the Azure resource commissioning system and the Blue-Green deployment process. While this is exactly what we want for most environments, one of the things we really need is a CD (Continuous Delivery) environment where, on every commit, the code is automatically compiled, unit tested and deployed to the CD environment as soon as possible.

One of the things I’ve become a massive fan of over the last couple of year is the WebDeploy deployment tool, there are tons of resources out there and I have blogged about how great it is before. Its a free install and easy to add via the Web Platform Installer. The principle attraction of the tool to me is just how fast it is – I previously worked in a team where we needed to deploy a platform consisting of 12 separate components and by moving from using MSBuild to build and deploy each step to a single initial step which built all the packages and then subsequent WebDeploy steps to actually install them the build time was cut by 60%.

NB this should not be implemented in a production environment for numerous obvious reasons. Also using WebDeploy in Azure requires the following (all reasonable) conditions to be met:

For development and testing purposes only

Only web roles can be updated

Can only support a single instance of a web role

You must enable remote desktop connections

I began a quest to see if we could use WebDeploy in our Azure cloud services...

After a few days struggling I have managed to discover, digest and harness the beauty that is WebDeploy in Microsoft Azure cloud services. This can be thought of as two distinct challenges. The first challenge is to create a cloud service capable of hosting a  WebDeploy service, the second (and far more simple) challenge is to WebDeploy into the cloud service. Incidentally publishing from Visual Studio gives different results than publishing from command-line/TeamCity and given that what we want is fully automated deployment (why would you ever want anything but?) – the goal of this exercise was to support WebDeploy on a newly created Azure cloud service with no manual intervention.

I have covered the important parts of each step below:

1) Create the Microsoft Azure cloud service

The Visual Studio publish wizard gives a lovely helpful dialogue which is pretty clear about how to go about this:

image

Indeed hitting publish after completing the wizard creates a new cloud service with three endpoints (browsing port 80, Remote Desktop port 3343 and WebDeploy port 8172). Lovely. Save the publish settings and we’re good to go right? Wrong!

I run publish from Visual Studio and can WebDeploy and Remote Desktop to the cloud service fine...

image

...but when I executed the build from the command-line the WebDeploy endpoint disappeared...

image

This drove me mad for about a day and a half!

So, Googling gave me nothing, Bing gave me nothing and DuckDuckGo DuckDuckWent! There were some helpful posts from guys a few years back but nothing since – I had to figure this one out myself. The heart of the problem was that Visual Studio MUST be doing something extra/different than plain old MSBuild from the command line. I ran the Visual Studio publish, verified the Cloud service was deployed with WebDeploy enabled and saved the contents of the Azure cloud project bin directory. Then I ran the build from the command line, verified that WebDeploy was not enabled and compared the service definition (.csdef) files from the two deployments.

The contents of the non-WebDeployable (non-working) ServiceDefinition.csdef file are shown below:

<ServiceDefinition name="ContinuousDelivery" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2013-10.2.2">
  <WebRole name="YOUR_SITE_NAME" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
    <Imports>
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
      <Import moduleName="RemoteForwarder" />
    </Imports>
  </WebRole>
</ServiceDefinition>

The contents of the WebDeployable (working) ServiceDefinition.csdef file are shown below – note the extra XML nodes shown in bold:

<ServiceDefinition name="ContinuousDelivery" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2013-10.2.2">
  <WebRole name="YOUR_SITE_NAME" vmsize="Small">
    <Sites>
      <Site name="Web">
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
      <InputEndpoint name="Microsoft.WindowsAzure.Plugins.WebDeploy.InputEndpoint" protocol="tcp" port="8172" localPort="8172" />
    </Endpoints>
    <Imports>
      <Import moduleName="Diagnostics" />
      <Import moduleName="RemoteAccess" />
      <Import moduleName="RemoteForwarder" />
      <Import moduleName="WebDeploy" />
    </Imports>
  </WebRole>
</ServiceDefinition>

So Visual Studio publish does “something” that causes the generated ServiceDefinition.csdef file to contain extra configuration data – slightly weird given that the source ServiceDefintion.csdef file used to generate these files was the same in both cases!

So to work out what extra magic Visual Studio was doing I set the Visual Studio output level as verbose as possible, published the project and examined the output - I found these lines in the log window…

2>    Task "Message"
2>        Task Parameter:Text=Adding Web Deploy component...
2>        Adding Web Deploy component...
2>    Task "Message"
2>        Task Parameter:Text=EnableWebDeploy is true
2>        EnableWebDeploy is true
2>    Task "Message"
2>        Task Parameter:Text=WebDeployPorts = YOUR_SITE_NAME:8172|
2>        WebDeployPorts = YOUR_SITE_NAME:8172|
2>    Task "Message"
2>        Task Parameter:Text=WebDeploy roles = YOUR_SITE_NAME
2>        WebDeploy roles = YOUR_SITE_NAME
2>    Task "Message"
2>        Task Parameter:Text=TargetServiceDefinition is bin\Release\ServiceDefinition.csdef
2>        TargetServiceDefinition is bin\Release\ServiceDefinition.csdef
2>    Using "EnableWebDeploy" task from assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Windows Azure Tools\2.2\Microsoft.VisualStudio.WindowsAzure.Tasks.2.2.dll".
2>    Task "EnableWebDeploy"
2>        Task Parameter:ServiceConfigurationFile=bin\Release\ServiceConfiguration.cscfg
2>        Task Parameter:ServiceDefinitionFile=bin\Release\ServiceDefinition.csdef
2>        Task Parameter:RolesAndPorts= YOUR_SITE_NAME:8172|

...which then led me to the Azure SDK build targets file, this section in particular...

<!--
  ===============ConfigureWebDeploy=============================================

    Enables Web Deploy on web roles if the user chooses to do so.
 
    [IN]
 
    TargetServiceDefinition: (item) The service definition file that is actually published.
    TargetServiceConfiguration: (item) The service configuration file that is actually published.
    WebRoleReferences: (item) The web roles being published.
   
  ==============================================================================
  -->
<Target Name="ConfigureWebDeploy" Condition="'$(EnableWebDeploy)'=='true'">

  <ItemGroup>
    <RolesToConfigure Include="@(WebRoleReferences->'%(RoleName)')" />
  </ItemGroup>

  <Message Text="Adding Web Deploy component..." />
  <Message Text="EnableWebDeploy is $(EnableWebDeploy)" />
  <Message Text="WebDeployPorts = $(WebDeployPorts)" />
  <Message Text="WebDeploy roles = @(RolesToConfigure)" />
  <Message Text="TargetServiceDefinition is @(TargetServiceDefinition)" />

  <!-- Add Web Deploy Plugin -->
  <EnableWebDeploy
    ServiceConfigurationFile="@(TargetServiceConfiguration)"
    ServiceDefinitionFile="@(TargetServiceDefinition)"
    RolesAndPorts="$(WebDeployPorts)" />
</Target>

I fiddled about with the settings listed and sure enough, adding the argument below to the MSBuild command line...

/p:EnableWebDeploy=true,WebDeployPorts=<YOUR_SITE_NAME>:8172,RolesToConfigure=<YOUR_SITE_NAME>

…caused the extra configuration info to be included in the resultant ServiceDefinition file. Hooking this up to actually publish the service was a simple matter of adding the extra parameter to the TeamCity build step. TeamCity will give a warning if you set system parameters this way (ie via the command line) but I cant see that this one has any reuse potential so am happy for it to remain a command line arg.

2) WebDeploy the new code

This is super-simple compare to challenge 1, and involves getting either the IP address or DNS name from the Azure management console and plugging them into the command line build (or WebDeploy powershell Snap-In that comes with WebDeploy)!

 

Also bare in mind that any time the role is restarted the WebDeploy-able customisations will be lost and you’ll need to redeploy the instance – this is due the the fact that WebDeploy goodness is added after the package is uploaded via startup tasks created dynamically and these are not reapplied when the role is recycled – we overcame this by adding logic to TeamCity to redeploy the whole instance to Azure in the event of any errors.

All this was very blackbox but its all good now – as stated previously there are limitations to using webdeploy in an Azure cloud service but IMHO its worth it – we can get instant (<1minute) feedback from other members of the team without needing to peer over a developer’s shoulder – as I’m finding out things move fast in the StartUp world so every second counts. It’s also pretty cool to know that despite all the great stuff that MSBuild and .Net give us, its not magic and if you can be bothered to poke around enough – the answers are there.

  • (This will not appear on the site)