Automated Deployments with TeamCity, Deployment Projects & SVN
Join the DZone community and get the full member experience.Join For Free
one of the continually high risk and sometimes fiddly operations in web deployment is a web application’s deployment, and yet a lot of people working in smaller teams seem to have become stuck in the land of cowboys because the task of automated deployments seems either too difficult, too time consuming to setup or is perceived as un-needed. i’m about to attempt to prove all of these things wrong, while at the same time allowing you to get back to doing what you do best: write code.
depending on the company you work in, you either have a locked down automated process for deployments or some dude is deploying from his local machine using drag and drop into an ftp client. this may work if a project is infitisimaly small or if you know you only have to do it once. happily this is almost always not the case: most projects contain more than one file, and will probably grow as your client comes back for changes, because they are so happy with your twitter replacement that costs money .
what is the problem?
deploying a website means changing a sites configuration, maybe adding some new items to your sites environment – who knows, each application is different. these can be done by hand, but it is better if it is done in an automated fashion, so there is less chance of getting things wrong. this is what we are going to solve.
there are many many things
that could make deployment of your project complicated, but we will
start with what even the simplest websites can get value from;
- different app settings between local and live servers
different connection string between local and live servers
some other things you can automate, that i will add in a later post will cover some of the more advanced topics, such as;
- database schema changes between local and live servers
synchronisation of live dynamic assets such as uploaded images etc between local and live servers
these are the settings that you need to change when you deploy your everyday
as i said, these are really, really basic things, but the most
important thing to be gained from having an automated deployment setup
is one golden thing above all else:
you need the piece of mind that every time you deploy there is less chance of issues, you get rid of so many potential business changing problems:
- you always deploy the latest from your source control so that if someone has forgotten to get the latest on their local machine, they don’t inadvertently create some bug reversions.
- if you are away from the office and one of your colleagues needs to deploy for you, the process is simple.
- you place more responsibility on the quality of checked in code – if you check in, it needs to build, it needs to work, and it needs to not break anything.
- no one accidentally uploads site settings that are dated, or potentially wrong for the deployment environment.
- plus many more potential gremlins.
what we are going to do about it
what i am going to show you to do is the following:
- setup team city for continuous integration
- add web deployment projects to your sites
- get team city to build and deploy your site locally every time you check in.
this deployed folder will be the one that everyone will use for making deployments from – never from your local machine.
please note that i am using visual studio 2008 for this demo, and visual studio 2010 has some wonderful new capabilities that can make what i am about to show you even easier – scott hanselman talks about them here .
let’s get to it – install software
so the first thing your going to have to do is download and install the following things on the pc you want to use as your build machine – this doesn’t have to be a separate pc, and could be installed on your local machine, or even inside a locally hosted virtual machine.
team city (on the build machine)
asp.net web deployment projects (on both the build machine and the development machine)
will also need some form of source control, for the setup i'm going to
show you i am using visual svn server, which i have preconfigured. team
city supports many different types of source control through addons, but
out of the box it supports the following (not a small list):
- team foundation server
visual source safe
so no matter what you are using for source control team city should have you covered.
during the installation of team city it will install a web server on a designated port that you enter. take note of this port for our configuration later.
once installation of teamcity is complete start both the build agent and server services. when the installer is done you will be presented with a license agreement and be asked to create the first user account with administrator privileges. take note of these details so that you can login later.
step 1. adding deployment projects to your web sites
so this is where things get interesting. you’ve installed the web deployment project support on your local development machine, and are ready to go. now all we need to do is add the new project type to your site.
start by opening your web application/website in visual studio.
then right click on the project you want to setup automated deployments for and click “add web deployment project” to add the new project type to your solution, enter a name for your deployment project. i usually call mine “[solutionname].[webappname].deployment”.
if you’ve never added a web deployment project to a website before this will all be new to you. to give you a really quick rundown on web deployment projects i’ll just say this: web deployment projects are basically just build scripts for msbuild.
now you want to create a new build configuration for your deployment. this allows us to target a build for teamcity to target later, and stop the deployment project from running when your building locally.
in the build menu click “ configuration manager ” to open the screen below:
click the solution configuration drop down this is currently selected “ debug ” and select “<new…>”.
in the new windows enter the configuration name as “ deployment ” and click ok
now using the same drop down select your debug and release build configurations and unselect the deployment project you just created from the build by un-ticking it.
move the configuration drop down back to the deployment configuration you just created so that we can set it up for team city to run. now close this window.now you need to create alternate configuration files for both your applicationsettings and connectionstrings configurations. so go ahead and create two new configuration files. i save mine as /configs/appsettings.live.config and /configs/connectionstrings.live.config. the contents of these files need to be your live or staging settings, that will be deployed when teamcity builds the deployment.
<appsettings> <add key="mysetting" value="different from local settings"/> </appsettings>
save the files. now right click web deployment project and select properties to open its configuration. make sure you have selected “ deployment ” from the configuration drop down, then select the compilation option on the left and enter the location for the build server to deploy to. this needs to be a local address as if it was being run on the build server . not locally. in my case i’ve entered a unc path to my webserver’s network share to deploy to, just to show you its possible. you’ll probably prefer to enter a local path like c:\builds\mywebsite . remember this is as-if the path was being entered on the build server itself, as this project will be run on the build server once it downloads it from your source control.
then select the deployment option on the left. this where you can setup some basic transforms for your web.config files. we want to replace our applicationsettings and connectionstrings sections. you can simply enter the section to replace and the path to the file to replace it with. enter yours to point to the file you just created.
save this by clicking ok and your done with the visual studio settings for your deployments. now onto the automation side of things.
step 2. creating a new team city project
login to team city by using the ip of your team city server following by the port your entered earlier and using the user your created earlier into a web browser
once logged in click administration on the top right. once this page has loaded, click the create project link.
enter a name & description for your new project. save this dialog.
the next page will have two extra tabs. one of these tabs is labelled vcs roots . click this link so that we can add your source control servers settings to team city. then click create vcs root .
enter a name for your vcs root and select the provider. as i stated i'm using visual svn, but your configuration may change – the advanced configuration of your source control provider is out of the scope of this post – refer to the team city help for setting up yours. for me i just enter the url for my svn repo and the username and password i want to use and i also select full support in the externals support setting.
make sure you also check the box make this vcs root available to all of the projects if you want to do multiple projects with this svn server (not necessary for our configuration though) then click save. now click back to the general tab. now click the create new build configuration link.
on this page enter a name for your build – i call mine local deployment. leave all the rest of the settings as is and click the button labelled vcs settings >>
on the next page select your recently created svn repo from the drop down above. leave everything else as is, and click the button “ choose build runner ”.
on the build runner configuration page, select msbuild from the list.
then enter the project or solution file to run from within your project into the build file path . my solution filename is called mywebsite.sln
leave everything else as is, except for two important textboxes labelled “ targets ” and “ command line parameters ”.
in these two enter targets: “ rebuild ” and command line parameters: “/p:configuration=deployment ”
its really important to note the second one, as this setting tells msbuild to run the “ deployment ” configuration we created earlier when a build is fired.
click save .
on the next page there will be a bunch of new navigation items on the right. click on the link titled build triggering. this page will take you to the configuration of when builds should be run. i want a new deployment to fire every time i check in. this way whatever is running on my staging server is always up to date.
to do this, tick the box labelled enable triggering when files are checked into vcs.
click save and your done!
to test the deployment is very simple. click build queue to take you to the build queue watching page.
now open visual studio on your development machine, make a change and check it in. this should now fire a build. refresh your build queue page a few times and you should be able to watch the build/deployment happen in real-time.
any errors that occur will be very easy to fix as the build will give you a full report if it fails.
a common error that may happen if you have not installed the web deployment project types onto the build server is:
microsoft.webdeployment.targets" was not found.
this can be fixed by either installing the project types on the build server or copying the folder c:\program files\msbuild\microsoft\webdeployment\v9.0\microsoft.webdeployment.targets to the build server from your local development machine.
a few other posts i have written that take what we’ve learned above further:
- continuous integration tip #1 - sftp/ftp deployment
- continuous integration tip #2 - using an app_offline.htm in your build
- finding the build configuration drop down in visual studio
Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.