Automated Deployment with TFS

One of the few things that can add true agility to the Agile practices that you claim to do is Automation. – “AUTOMATE EVERYTHING YOU CAN!

In my current project we have successfully automated our application deployment process. To us, deploying the ‘latest’ or an ‘already compiled revision’ of the build to the desired environment (Integration/QA/UAT/Prod etc) is just a click away. This encourages the team to deploy quickly, frequently and fearlessly.

This post will have us take a look at how to automate your application deployment using Microsoft Team Foundation Server (TFS). I am assuming that you are familiar with TFS and Team builds. Needless to say that you should have a Build Server (Build Controller, Agents) up and running. We will extend CI build capabilities of TFS to deploy our builds continuously to the integration environment. Assuming that you have the Build Controller and Agents in place, the first thing we might want to do is setup a basic CI build on the Build Server, selecting Continuous Integration as the build trigger. This will setup our out-of-the box CI Build Service using TFS.

The CI build definition we created is good enough to compile builds, run tests, deploy databases (if configured) and drop the builds to a specified location every time we check-in code. But what we want to do here is extend this build definition in a manner that in addition to doing all of the above; it will also go ahead and deploy the application on the web server.

Here is how we do it,

1.     Right click on the CI build-definition in team explorer.

2.     Select “Process”

3.     Expand “Advance” node, if not already expanded

4.     In the ‘MSBuild Arguments’, provide the following parameters,

/p:DeployOnBuild=True
/p:DeployTarget=MsDeployPublish
/p:CreatePackageOnPublish=False
/p:MSDeployPublishMethod=RemoteAgent
/p:AllowUntrustedCertificate=True
/p:MSDeployServiceUrl=<name of the Server>
/p:DeployIisAppPath=<IIS App>/<Virtual Dir>”
/p:UserName=<username>
/p:Password=<password>

And, there you go – simple, yet powerful. Try it out in your projects; you might be pleasantly surprised with the results.

Any discussion about Agile and Build Automation is not complete it without a mention of Continuous Deployment and Continuous Delivery. Continuous Deployment is about shipping out every feature developed by the team through the Deployment Pipeline all the way to the production in an automated fashion. Continuous Delivery is having a human intervention somewhere in the Deployment Pipeline to before making the feature available to the users.

These techniques are really about taking your agile practices to the extremes, both in terms of potential and performance. But, even if you don’t intend to get there immediately, it would still be unthinkable as to why wouldn’t you start automating things that you need to be doing as often as needed and as accurate as possible – Build Deployment definitely being one of them.

Advertisements

19 thoughts on “Automated Deployment with TFS

  1. Very good post, Anshu. Keep up the good work!!! It is helping us (speaking as someone who works with you on aforementioned project) and I am sure these posts help others too.
    Josh

  2. Does this only work if the build type is selected as continuous integration? For some reason it does not work for me.

    1. Anshuman on April 3, 2012 at 5:48 pm said: [Edit]
      wahmed, I don’t think so. It works with any build “trigger”. Please provide me with more details

      1. We are using TFS 2010 and passing the following build arguments from a build:

        /p:DeployOnBuild=True
        /p:DeployTarget=MSDeployPublish
        /p:MSDeployPublishMethod=WMSVC
        /p:CreatePackageOnPublish=False
        /p:MSDeployServiceUrl=https://:8172/msdeploy.axd
        /p:DeployIisAppPath=
        /p:AllowUntrustedCertificate=True
        /p:UserName=
        /p:Password=

        I simply want to deploy the site after it has been compiled and built.

        It works manually from VS 2010 when I right click on the project and then click on publish.

        Any advice?

      2. Sorry somehow the values for some variables got messed up

        /p:MSDeployServiceUrl=https://myserver:8172/msdeploy.axd
        /p:DeployIisAppPath=mysite
        /p:UserName=myuser
        /p:Password=somepassword

      3. TFS build deployment can fail for a variety of reasons – “failure to contact the service url” being the most common of them. Can you please provide with the error message that the TFS is reporting?
        You should be able to find it on the build log of the build that failed.

  3. I don’t see anything related to deployment in build logs. I think the default build template used for creating this build definition does not deploy builds automatically. I may have to change the process flow to accomplish the task of automatic deployment but that seems tedious and requires some xaml knowledge. How did you create your build definition?

    1. It’s works perfectly with default template. These are the parameters (switches) for MS Deploy, which is used under the hood. And I have mentioned how to specify them in the post. You might be missing out something. Check out the following video, it gave me some valuable clues when I was struggling to make it work,


      Hope it helps.

    2. To get this to work on my build machine, I had to copy the folder %ProgramFiles(x86)%\MsBuild\Microsoft\VisualStudio from a machine with Visual Studio installed to the build machine. Otherwise the build logs showed nothing but the standard copy to a drop location with no errors or any other info about why it was not doing a web deploy.

      1. Thanks for bringing that up Jonathan. I know it sucks, but the reality is that the build machine needs it to be able to trigger the build. The build manager creates a TFS workspace, downloads the solutions and builds it on the build machine locally. As surprising as it may sound, many people go as far as recommending a VS installation on the build machine.

  4. Thanks for posting. Can we publish WinForms app in the same way? I would like to publish (ClickOnce) as soon as the CI build is done in the same process. Any pointers are much appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s