Deploying to Azure VMs using VSTS Release Management
- Published on
- Reading time
- Authors
- Name
- Simon Waight
- Mastodon
- @simonwaight
I am going to subtitle this post "the missing manual" because I spent quite a bit of time troubleshoothing how this should all work.
Microsoft provides a bunch of useful information on how to deploy from Visual Studio Team Services (VSTS) to different targets, including Azure Virtual Machines.
Updated Nov 2017: it looks like Microsoft has updated their documentation site making the above link invalid. Take a look at the bottom of this post to see how you can use Release Management to deploy the package and run the PowerShell I talk about.
In an ideal world I wouldn't be using VMs at all, but for my current particular use case I have to use VMs so the above (linked) approach worked.
The approach sounds good but I ran into a few sharp edges that I thought I would document here (and hopefully the source documentation will be updated to reflect this in due course).
Preparing deployment targets
Azure FQDNs
Note: Please see my update at the bottom of this post before reading this section. While you can use IP addresses (if you make them static) it's worth configuring test certs with the FQDN.
I thought I'd do the right thing by configuring the Azure IP of my hosts to have a full FQDN rather than just an IP address.
As I found out this is not a good idea.
The main issue you will run into is the generated certs on target hosts only have the hostname in them (i.e. azauhost01) rather than the full public FQDN (i.e. azauhost01.australiaeast.cloudapp.azure.com).
When the Release Management infrastructure tries to connect to a host this cert mismatch causes a fatal error. I didn't spend much time troubleshooting so decided to revert to use of IP addresses only.
When using dynamic IP addresses the first Release Management action "Azure Deployment:Select Resource Group action" is important as it allows for discovery of all VMs and their IPs (i.e. no hardcoding required). This apprach does mean, however, you need to consider how you group VMs into Resource Groups to allow any VM in the Resource Group to be used as the deployment target.
Local permissions
I was running my deployments to non-Domain joined Windows 2012 R2 server instances with local administrative accounts and had opened the necessary port in the NSG rules to allow WinRM into the servers from the VSTS infrastructure.
Everything looked good on deployment until PowerShell execution on the target hosts resulted in errors due to permissions. As it turns out the error message was actually useful in resolving this problem :)
In order to move beyond this error I had to prepare each target host by running these commands at an admin command prompt on the host:
winrm quickconfig
Enable-PSRemoting
We could drop these into a DSC module and run that way if we wanted to make this repeatable across new hosts.
There is a good troubleshooting overview for this from Microsoft.
Wait! Where did my PowerShell script go?
If you follow the instructions provided by Microsoft you need to add a deployment Powershell script (actually a DSC module) to your Web App (their example uses "ConfigureWebserver.ps1" for the filename).
There is one issue with this approach - the build step to package the Web App actually ends up bundling the PowerShell inside of a deployment zip which means once the files are copied to your target VM the PowerShell can't be invoked.
The fix for this is to add an additional build step that copies the PowerShell to the drops folder on VSTS which means the PowerShell will be transferred to the target VM.
Your final build definition should look like the below
and the Copy Files task should be configured like this (note below that /Deploy is the folder in my solution that contains the PowerShell I've included for deployment purposes):
Once you have done this you will find that the script is now available in the VSTS drops folder and can be copied to the VMs which allows you to execute it via the Release Management action.
Wrapping up
Once I had these changes in place and had made some minor path / project name tweaks to match my project I could run the process end-to-end.
The one final item I'll call out here is the default deployment location of the solution on the target VM ends up being the wwwroot of your inetpub folder with a subfolder named ProjectName_deploy. If you map this to an Application in IIS you should be good to go :).
Update - 8 December 2016
After I'd been running happily for a while my Release started failing. It turns out my target VMs were cycled and moved to different public IP addresses. As the WinRM HTTP certificate had the old IP address in the remote calls failed.
I found a great blog post on how to rectify this situation though: http://www.dotnetcurry.com/windows-azure/1289/configure-winrm-execute-powershell-remote-azure-with-arm
Update - 18 November 2017
As mentioned above, Microsoft has reorganised some of their content and seems to have retired the post I used originally. The missing piece to my post here is how does Release Management (RM) fit into the picture - see below for what my RM process looks like. The highlighted Task is the one that executes the PowerShell on the VM - details below as well!
Here is the PowerShell Task.