Continuous Integration Tip #2 – Using App_offline.htm in Your Build
Deploying in an automated fashion using Continuous Integration doesn’t happen instantly, and depending on the size of your application, your continuous integration deployment can get caught in a state of unknown/in-between if a user visits your application half way through deployment. This can be far from optimal, but ASP.Net has a trick up it’s sleave in the form of the App_offline.htm file.
This is the second post in a series on Continuous Integration Tips & Tricks in follow up to a post i made about setting up Continuous Integration Automated Deployment with Web Deployment Projects & Team City.
Basically the gist of this is simple: if you place a file named app_offline.htm in the root of your application, all requests to asp.net pages or web services, is met by the contents of this file.
Because of this really cool feature that ASP.Net has on offer, we can use it to our advantage in our automated build. What we need to do is copy a file into the root of our application before we copy the site.
The example build file
In all my examples, I've been using Microsoft Web Deployment Projects and TeamCity. This solution uses MSBUILD for it’s automation, therefore I’m going to show you an example MSBUILD script that you can add to your web deployment project. You'll have to alter these for your build provider of choice if you use a different one (Nant etc).
Create an HTML file called DeploymentInProgress.htm in the root of your application (it can be anywhere, as long as you update your build script to reflect the new path) and in it put some HTML that mentions that the app is being deployed. I suggest that if you use CSS include all the styles in the header instead of linking to it, and if you use any images in the page they are hosted on another domain/location (because you’ll be deploying your site and want to make sure the image file you calls exists and hasn’t been deleted by your deployment process).
Add the following to bottom of your Web Deployment project file.
<Target Name="AfterBuild"> <PropertyGroup> <!-- This is where i want to copy my deployment to--> <DeploymentDestination>C:\MyIISRoot\</DeploymentDestination> </PropertyGroup> <ItemGroup> <FilesToCopy Include="$(OutputPath)\**\*.*" /> </ItemGroup> <!-- Copy the App Offline file--> <Copy SourceFiles="$(OutputPath)DeploymentInProgress.htm" DestinationFiles="$(DeploymentDestination)app_offline.htm" /> <!-- Copy the deployment files--> <Copy SourceFiles="@(FilesToCopy)" DestinationFiles="@(FilesToCopy->'$(DeploymentDestination)%(RecursiveDir)%(Filename)%(Extension)')" />
<!-- Delete the app offline file--> <Delete Files="$(DeploymentDestination)app_offline.htm" /> </Target>
This will copy your file (in the process renaming it to app_offline.htm), copy your project files, and then delete the app_offline.htm file so that your application can start.
This can be further integrated with the ftp upload i mentioned in the previous post to give a remote deployment option.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)