Monday, May 6, 2013

How to Automate App Deployment in TeamCity (Part 1)

Please notice that this is a multi-part, step-by-step guide, on how to automate application deployment in TeamCity CI Server.

Part 1: Setting Up the Deploy Step in TeamCity

The first step is to modify our build script on our project to include the new steps, which will enable TeamCity to perform the deployment process.

  1. Let’s open our MVC application in Visual Studio.
  2. Let’s modify the MSBuild script of our project to include the deploy steps. To start we are just going to add a basic deploy application step, which will just copy the application files to an specified location.
  3. Save the script.
  1: <!-- Copies the application files to the corresponding directory -->
  2: <Target Name="DeployApp">
  3:     <Message Text="Copying application files..." />
  4:     <ItemGroup>
  5:         <ApplicationFiles Include="$(ApplicationOutputDirectory)\**\*.*" />
  6:     </ItemGroup>
  7:     <Copy SourceFiles="@(ApplicationFiles)" DestinationFolder="$(DestinationPath)\%(RecursiveDir)"/>
  8: </Target>
  9: 


Before committing the changes, let’s add the deploy step to our project build configuration.

1. Login into your TeamCity administration portal.


TC_webdeploy_02

2. Click on the Build Configuration you are going to modify by adding the new deploy step.

TC_webdeploy_03

3. Click on the “Edit Configuration Settings” link at the top right corner, below the search box.

TC_webdeploy_04

TC_webdeploy_05

4. Once in the configuration settings page, click on the “Build Steps” section of the panel on the right.

TC_webdeploy_06

TC_webdeploy_07

5. Once in the build step sections, click on the “Add build step” below the listed steps, so we can add our new deploy step.

TC_webdeploy_08

6. Since we will be using our MSBuild script, choose MSBuild as the runner type.
7. Enter the name of the step.
8. Enter the build script relative path.
9. Select the MSBuild version you are targeting.
10. Enter the Target variable (MSBuild script step name we just added – notice that you can actually target a parameter here, that you can then add in the parameters section, we will do that next time).

TC_webdeploy_09

11. Click save.
12. You should be redirected to the steps overview and the new deploy step should have been added.

TC_webdeploy_10

13. Click on the Build parameters section so we can enter the deploy parameters we need.

TC_webdeploy_11

14. Click on the “Add new parameter” link at the top to add a new parameter.
15. The new parameter we need is the DestinationPath we specified in the MSBuild script. This is the path where the application files will be copied.

TC_webdeploy_13


16. Enter the value, which should be the folder path were you want the application files to be copied. In my case I’m entering D:\inetpub\web.contoso.com.

TC_webdeploy_12

17. Click save and make sure the new parameter was added correctly.

TC_webdeploy_14

We are done configuring the new step. Let’s commit the changes to our project’s MSBuild script file and see if the TeamCity steps are executed correctly, and our app files deployed to the location we specified.

TC_webdeploy_15

TC_webdeploy_16

If you have problems where no files are being copied, review your build script top parameters, where paths are being defined. Make sure the “ApplicationOutputDirectory” parameter defined in the script and outputted in the build log actually exists on the server.

TC_webdeploy_19

If everything was correct, you should see the build log messages for all the files copied and if you open the deploy path, you should see the application files there. Job done.

TC_webdeploy_21

TC_webdeploy_20

If you haven’t realized it to this point, let me point out something quite important. What we have done in this step is actually deploy the application files, which is not the same as deploying our web application. This means that although we are able to build, test and copy the application files, we are not publishing our web app so it can be viewable on our web server.

In the next part, we will set up the IIS website configuration we need to deploy our web application package.

Next Article: Part 2 – Configuring the Web Application in IIS7

No comments:

Post a Comment