Microsoft's hidden gem: MSDeploy

MSDeploy will be central to many of the posts in this series. MSDeploy is the tool behind the scenes in Web Deploy, but can do much more than deploying IIS sites through it's provider extensions. We've used it's powerful command execution and file syncing functionality for:

Maintainable, large-scale continuous delivery with TeamCity series

This post is part of a blog series jointly written by myself and Rob Moore called Maintainable, large-scale continuous delivery with TeamCity:

This post outlines how using OctopusDeploy for deployments can fit into a TeamCity continuous delivery deployment pipeline.

Maintainable, large-scale continuous delivery with TeamCity series

This post is part of a blog series jointly written by myself and Matt Davies called Maintainable, large-scale continuous delivery with TeamCity:

  1. Intro
  2. TeamCity deployment pipeline
  3. Deploying Web Applications
    • MsDeploy (onprem and Azure Web Sites)
    • OctopusDeploy (nuget)
    • Git push (Windows Azure Web Sites)
  4. Deploying Windows Services
    • MsDeploy
    • OctopusDeploy
    • Git push (Windows Azure Web Sites Web Jobs)
  5. Deploying Windows Azure Cloud Services
    • OctopusDeploy
    • PowerShell
  6. How to choose your deployment technology

Documentation

Some of the official documentation on the more advanced commands is not clear and doesn't warn you about some of the pitfalls that you can run into. Some good introductory links:

http://raquila.com/software/ms-deploy-basics/ (well organised post on the options and commands available)

http://blog.torresdal.net/2010/08/16/no-click-web-deployment-part-2-web-deploy-a-k-a-msdeploy/ (good for an extremely comprehensive listing of the various options available)

I've also linked to several posts below with information more targeted to specific msdeploy options and commands which are somewhat under-documented.

Our frequently used providers/options

We use the runCommand provider for remotely executing powershell scripts:

-presync:runCommand

-postsync:runCommand

-source:runCommand

Other useful commands/options:

 -useCheckSum

As the name implies, uses checksums to determine which files to sync as opposed to timestamps. In a continuous integration environment where every file is updated upon each build, this flag improves the speed of build times (and has helped us overcome locking issues with some unmanaged DLLs which are never updated).

-allowUntrusted

Useful if you trust your network and target msdeploy server (avoids certificate validation issues during deployment).

Powershell scripts and MSDeploy

Example teamcity parameters list for an msdeploy command line step:

-postSyncOnSuccess:runCommand='"powershell.exe -ExecutionPolicy ByPass -InputFormat None -NonInteractive -File C:msbuild_scriptstopshelf_deploy_postsync.ps1 -serviceAssemblyName %env.DeployServiceName% -environment %env.Env% -servicesPath %DeployTopshelfPath%"',waitInterval=600000

In the above powershell command:

Error handling in powershell

Our error handling process for powershell scripts executed via msdeploy is best described in the following posts: Powershell error handling and why you should care and Caught in a trap - dealing with errors. TeamCity is set up to fail a deployment build if the following text is detected from the msdeploy output: "exited with code '0x1'". We can ensure powershell scripts exit with that error by organising scripts with the following structure:

$ErrorActionPreference = "Stop"

try
{
    eventcreate /ID 1 /L APPLICATION /T INFORMATION /SO $websiteName /D "A deployment of $websiteName started"
}
catch
{
    write-error $_
    exit 1
}

If you're interested in reading more about msdeploy, you should check out Richard Szalay's posts on msdeploy - I've learnt a lot from them before!

Tweet