Saturday, January 14, 2012

Continuous Integration( Part Two) :Web Config Transformation

Note : In this post I will use Microsoft based technologies. But the main concept describes in a general manner.

In my previous post I mentioned that a build server should run the tests with its configurations(eg: data base connection strings) and if the test are successful then it should deploy working packages in to many web container location according to your requirement.For an example it should deploy a working package in to productions server as well as in to the live server with their specific configurations.(both application are pointing their own data bases)

production server => production database with tests data for QA guys
Live Server => live data base with the actual data 

In order to do this, build server has to create several packages with different configurations and deploy them in to required locations.There are many packaging tools which facilitate configuration transformation functionality.In this post I will use MSBuild command line tool to create IIS deployable packages.Before Moving on we need to know a bit detail knowledge about web config transformation.

If you have a close look in to your web.config file which created by VS2010 you will notice that there are two config files which named as web.Debug.config and web.release.config. Those two files default files which offer by VS2010.If you want your own you can add since VS2010 provides you an extensible configuration setting environment.

                          Build =>Configuration Manager  => Add your own configuration 

Lets assume we have created configuration for our production server which I mentioned above.Till you cant see it your latest configuration setting appears on web.config file. In order to set it you should do following step.

                         right click the original web.config file and click the context menu command “Add Config Transforms” 
In the Web.BuildServer.config file you can write specific settings which is specific to the build Server settings. Following is the way we do the we web.config transformation. 



You can see here we have used XML Transformation Engine which does the actual task when we package our project with build Server configurations.Followings are the some of functionality offer by transformation engine.

Other than the xdt:Transform tag you can see we have used a locator which refer the original web.config configurations. 
And following is the MSBuild command which we can transform web.config which has the build server configurations. 

                   /t:TransformWebConfig /p:Configuration=BuildServer" 

Also you can create your web deployment package with the build server settings by following MSBuild command.

           "ProjectName.csproj" /T:Package/P:Configuration=BuildServer;

It creates a ZIP file which has batch file which can be executed from MSDepoly command line tool.And also we can see transformed and original web.config files. Here we have a small problem.That is if we manually unzip this created zip file and then manually copy to the IIS location . then build server specific settings are not applying to it.Since in web.config file refers the transform elements from a external XML file which is used by the MSDeploy command and it is the task of MSDeploy tool to read the external XML property file and replace the matching tokens. So if you do the deployment manually I recommend you to copy the transformed web.config and replace the existing web.config file in the package.Then you can manually copy  package in to the IIS location and your app will work since now you have the build server configurations.

Ok.Now we should back to our main topic again which is Continuous Integration. If we can use above concept in the build server side (through a build server job) we can create a package in build server machine with its specific configurations and then deploy it. Also we can use same strategy for multiple deployment with many configurations.And my next post will describe how we can achieve this task. I hope to use jenkins build server and some of its plugins to do it. 

1 comment: