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).
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
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.
There are some requirements for the webserver as well: you have to install the following software on web server in order to Web Deploy works properly.
The easiest way to install all required software is to use Web Platform Installer
Make sure then you have .net 4 installed, and .net version for application pool is 4.0 as well
After you install Teamcity you have to create a new project.
Give Project a name and press “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 “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 username/password or upload Private key.
Do not forget to press “Test Connection” button before saving your source control configuration.
Next step will be to add a “build step”. TeamCity supports a number of build runner, but I prefer to use MSBuild
In case of more complex build system, you probably have some build steps before and after main "deployment" step, for example I have iisreset step, because sometimes logging system does not allow to republish application when IIS process is running. In that case add 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 project in Visual Studio, you have two configuration Debug and Release, but you can create as many configuration as you need, for example: UAT, SIT, PROD... and during deployment MSBuild will transform your web.config file with configuration you provided. For example one of my project has a number of different options for configuration and different transformation files:
MsDeployServiceUrl - after you install Web Deploy package on web server, it creates 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 - location where you want to deploy your web application, in my case I am not deploying it to root of website, but you can deploy it to "Default Web Site" or you can create your own website and provide it's 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 “Run” button at the top right corner. After the build finished, you can see the following result
And application is deployed on target web server
For 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 existing project and modify it's properties:
And your final build projects can look like this: