Description

After a TFS Build there are several tasks that usually still need to be run. These include copying to a development server, running web tests, and updating the Build Quality to reflect it's current status.

Requisites

To get started, you must have a solution which contains a Web Application Project. You should also have created the initial build definition and confirmed that the build is succeeding.

Next, you should have a web site setup in IIS on the server you are deploying to. That server should expose a file share which the user that TFSBuild is running under has full rights to.

Finally, we have multiple web.config files defined depending on the environment you are deploying to. We've named these web.config (local testing), web.development.config (dev server), web.staging.config, etc. Although a lot of the configuration is the same, things like connection strings, error pages, debug logging, etc is different. By keeping them in separate files it is trivial to deploy to the correct environment.

Usage

1. Check out the TFSBuild.proj file and edit it within Visual Studio. I typically take this time to remove all of the comments from the file in order to make it easier to look through.

2. Override the BeforeTest target. This will cause the deployment to occur after it is compiled and before any unit or web test lists are run. The idea being that you want to run your automated web tests against your development server.

This target will look like:
  <Target Name="BeforeTest">
    <CreateProperty value="\\devserver\WebRoot\MyWebApplication">
      <Output TaskParameter="Value" PropertyName ="MyDropLocation" />
    </CreateProperty>

    <Exec Command='xcopy /y /e "$(OutDir)_PublishedWebsites\MyWebApplication" $(MyDropLocation)' />

    <Delete Files="$(MyDropLocation)\web.config" ContinueOnError="true" />
    <Copy SourceFiles="$(MyDropLocation)\web.development.config"
          DestinationFiles="$(MyDropLocation)\web.config"
          ContinueOnError="true" />
    <Delete Files="$(MyDropLocation)\web.development.config" ContinueOnError="true" />
    <Delete Files="$(MyDropLocation)\web.staging.config" ContinueOnError="true" />
  </Target>


Note that the target does a couple of different things. First it copies the compiled web application to the development server. Second, it replaces the web.config file with the one named web.development.config. By having the build server effect this change we don't have to rely on our developers keeping up with it.

It is important to change the MyWebApplication to be the exact name of your web application project file. If you aren't certain of what this name is, navigate to the build path on your build server and look at the directory names under _PublishedWebsites.

Also you need to change the \\devserver\webroot path to match the path to your dev server.

3. The third step is to update the Build Quality. This requires overriding the AfterDropBuild target.
  <!-- Set default build quality to Ready for Initial Test -->
  <Target Name="AfterDropBuild">
    <SetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Quality="Ready for Initial Test" />
  </Target>


4. Done. Check your updated TFSBuild.proj file into source control and queue a new build. Once the build finishes your web application should have been deployed to your dev server.

Script

  <!-- Copy the files to the dev server -->
  <Target Name="BeforeTest">
    <CreateProperty value="\\devserver\WebRoot\MyWebApplication">
      <Output TaskParameter="Value" PropertyName ="MyDropLocation" />
    </CreateProperty>

    <Exec Command='xcopy /y /e "$(OutDir)_PublishedWebsites\MyWebApplication" $(MyDropLocation)' />

    <Delete Files="$(MyDropLocation)\web.config" ContinueOnError="true" />
    <Copy SourceFiles="$(MyDropLocation)\web.development.config"
          DestinationFiles="$(MyDropLocation)\web.config"
          ContinueOnError="true" />
    <Delete Files="$(MyDropLocation)\web.development.config" ContinueOnError="true" />
    <Delete Files="$(MyDropLocation)\web.staging.config" ContinueOnError="true" />
  </Target>
  <!-- Set default build quality to Ready for Initial Test -->
  <Target Name="AfterDropBuild">
    <SetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" Quality="Ready for Initial Test" />
  </Target>

Notes

The account that the TFSBuild.exe runs under must have full access to the share on the development server.

You must have "Start a build", "Administer a build", and "Edit build quality" security rights. We typically assign these to the Contributors security group so that all devs can manage this.

Last edited Nov 2, 2010 at 6:13 PM by wbarthol, version 2

Comments

No comments yet.