Build once, MSDeploy all over the place

· Code, Uncategorized

There are about 3 dozen ways to automate web app deployment.  All have their candy and their evil.  MSDeploy is one of the more intense tools, but if you don’t collapse at the command line syntax you’ll find it can be pretty sweet.

It’s rather easy to create a basic package, but step beyond the basics and the story isn’t laid out clearly before you.  Many jump on the Config Transform wagon, building 9 versions of their app for each environment they may target.  Transforms look good at first, but then you’re compiling your app 20 times and it just get’s annoying.  I’d much rather build once and specify the parameter transformations at deploy time.  That’s what I’ve got for you today.

Here I’ll walk you through creating custom parameters for deploy time change via MSDeploy.

I’m using an ASP.Net MVC 4 Internet Project, but it’s not critical.

image

Environment Specific Settings

To emulate an environment specific setting, I’ve added appsetting EnvironmentImIn.  I’ll commit this with the Get Latest and Go™ value of LocalDev.

imageimage

Building the Package (Manually)

Right click the project MVC project and click Build Deployment Package.

image

This will create your package as a folder with a series of files under <Project>\obj\package.

image

Great, but we don’t deploy from dev machines ‘cause we have a habit of installing crazy stuff.  You should…

Build the Package (Automation)

ASP.Net project files, being MSBuild files, have a target called Package.  This will first compile the project and then generate the package folder just like VS did above (Details).  You can adapt this with ease to TFS, TeamCity, etc.SNAGHTML46e20cf

Deployment Parameters

MSDeploy uses a parameters.xml file to determine what is “changeable” at deploy time.  This file is consumed at package creation time.  I consider this a good thing since you end up committing the file and are explicit about what’s changeable.

If you look in the .zip file in your package directory you’ll find a default parameters.xml.  This is because VS/MSBuild makes certain things changeable by default.  If you drop a parameters.xml file into your project root, MSBuild will use that.  Coolio.  Here’s ours:

image

Vishal Joshi does all the hard work of explaining what the elements all mean here.  Vishal is the man!  What we’re doing is adding a parameter EnvironmentImIn, giving it a description, and then telling MSDeploy how to find the entry.  Beware, there be regex in them thar hills.

Changeable Package Settings

If you build your Deployment Package now (msbuild /t:Package) you’ll find the *.SetParameters.xml file now contains our EnvironmentImIn setting where before it only had IIS Web Application Name and DefaultConnection.  Saweeet!

image

The *.zip file will also have a parameters.xml file like this:

image

Deployment (IIS Manager)

I find many deployment teams like to do their magic manually.  These are often the infrastructure or a Release management group.  They spurn the console at their own peril.  IIS is cool with that though.

You can import packages through IIS Manager and you’ll be presented a UI to change your setting.  #huzzuh!

SNAGHTML4c25a19

Set it to “Staging” and Finish click Finish results in:

SNAGHTML66b5a51

Deployment (Command line)

One of the convenient pieces created for you by msbuild /t:Package is the MSDeploySample.deploy.cmd file.  It helps parse parameters and construct the not so straight forward msdeploy.exe command line.

If you don’t want to mess with msdeploy.exe directly, you can customize the SetParameters.xml file and just run the cmd file like this:

MSDeploySample.deploy.cmd [/T|/Y]

/T means tell me what’s gonna happen, but don’ t actually do anything yet.

/Y is for the big kids.  Applies the changes.

The alternative is to supply –setParam like this

–setParam:EnvironmentImIn=Staging

Easiest way to construct an MSDeploy.exe command line is to use the .cmd and modify what it gives you.  Look here for detailed –setParam options.