Select Page

The blog post will explain how to create a build system and deploy application code to different environments, such as DEV, UAT, PROD… using TeamCity and MSDeploy. The application which needs to be deployed is an ASP.NET MVC 4 application, but it does not matter, it can be any web application (ASP.NET, ASP.NET MVC, HTML app).

Build server

I assume you already have TeamCity installed on your build server, but if you don’t have it, you can get the latest version from JetBrains. In my case, I used TeamCity 8.0.2 installed on Windows Server 2008R2 (but it does not matter, it works on Windows 8 and Windows Server 2012 the same way)

Download and install TeamCity from http://www.jetbrains.com/teamcity/

Also, your build server requires the following software installed, in order to perform deployment using Web Deploy

  • Microsoft Web Platform Installer (it will be used to install all dependencies)
  • .NET Framework 4 (this is only in case you building application targeting .NET 4 framework)
  • TeamCity
  • Visual Studio Express for Web

You have to install Visual Studio Express for Web on the build server, otherwise, you will get the following error message:

error MSB4019: The imported project “C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\ v11.0\WebApplications\ Microsoft.WebApplication.targets” was not found. Confirm that the path in the <Import> declaration is correct and that the file exists on disk.

You can install Visual Studio Express using Web Platform Installer. The web edition of Visual studio is less than 100 Mb

But in case if you don’t want to install the visual studio, you can just copy the content of WebApplications folder from your local machine (I am sure you have the Visual Studio installed on your machine, so the folder will be there) to build server.

Web Server

There are some requirements for the webserver as well: you have to install the following software on the webserver in order for Web Deploy to work properly.

  • Microsoft Web Platform Installer (it will be used to install all dependencies)
  • Web Deploy 3.5
  • Web Deploy 3.5 for Hosting Servers
  • Web Deployment Tool 2.1

The easiest way to install all required software is to use Web Platform Installer

Make sure then you have .net 4 installed, and the .net version for the application pool is 4.0 as well

TeamCity project

After you install Teamcity you have to create a new project.

Give Project a name and press the “Create” button

On the next screen, you need to add a new build configuration

Give a name to build configuration and go to Source control settings by clicking the “VCS settings” button

Now you need to add your source control system

I prefer Git, but it does not matter, you can use Subversion or TFS, or different providers which Teamcity support.

In my case, the authentication method is Anonymous, because I used GitHub public repository, but in case if you use Bitbucket or SVN you can provide a username/password or upload a Private key.

Do not forget to press the “Test Connection” button before saving your source control configuration.

The next step will be to add a “build step”. TeamCity supports a number of build runners, but I prefer to use MSBuild

In the case of the more complex build system, you probably have some build steps before and after the main “deployment” step, for example, I have iisreset step because sometimes the logging system does not allow to republish applications when the IIS process is running. In that case, add a new build step with runner type “Command line” and in Custom script field type iisreset

Command line parameter explanation

/P:Configuration=%environment-name% /P:DeployOnBuild=True
/P:DeployTarget=MSDeployPublish 
/P:MsDeployServiceUrl=https://%destination-server%:8172/MsDeploy.axd 
/P:AllowUntrustedCertificate=True 
/P:MSDeployPublishMethod=WMSvc 
/P:CreatePackageOnPublish=True 
/P:UserName=%destination-server-user% 
/P:Password=%destination-server-password% 
/p:DeployIisAppPath="Default Web Site/myapp-%environment-name%"

Configuration – by default, when you create a project in Visual Studio, you have two configurations Debug and Release, but you can create as many configurations as you need, for example, UAT, SIT, PROD… and during deployment MSBuild will transform your web.config file with the configuration you provided. For example one of my projects has a number of different options for configuration and different transformation files:

MsDeployServiceUrl – after you install Web Deploy package on a web server, it creates a service on port 8172 which is responsible for publishing web applications to IIS

MSDeployPublishMethod – when you use basic authentication (using username/password) you have to use WMSvc

Username – User who has administrator permissions on webserver Password – Password for the user

DeployIISAppPath – the location where you want to deploy your web application, in my case, I am not deploying it to the root of the website, but you can deploy it to the “Default Web Site” or you can create your own website and provide its name there. In my case app deployed to http://servername/myapp-release

Now navigate to build parameters and give values to configuration parameters

And the build is ready. Try to run it, by pressing the “Run” button at the top right corner. After the build is finished, you can see the following result

And the application is deployed on the target webserver

For a different environment, you have to create the same build project, but provide different “Build parameters” (EnvironmentName, DestinationServer…), the best way to do it, is to copy the existing project and modify its properties:

And your final build projects can look like this: