The right web.config for the right server

We have to manage 3 pretty large projects for a customer. The customer wants to test first, then say “ok, deploy it to production”.

There are about 16 settings per project, which are different for each of the 3 environments. Yes, I forgot to add that we have also the development area, where we publish bleeding edge features.

This scenario brings a problem, how to have the right config settings for the right server, without headaches.

The build is done automagically, through CruiseControl.NET, after each developer commits its part. Now back to web.config.

In trunk we have the good-for-dev version, which is the same for all developers.

Then we have an xml, which contains the good values for each server, then we have the keys and the new values that must be added to the file, overwriting the default ones:

 
<project name="Delta" configuration="PR" xmlns="http://www.delta.com/map.config.xsd" >
	<!-- RequestsConnectionString -->
	<key xpath="/configuration/appSettings/add[@key='RequestsConnectionString']/@value"
		   newValue="application name=huis-req;server=pr-server;database=Live_Delta;uid=****;pwd=****;Connect Timeout=100" />
	<key xpath="/configuration/system.web/compilation/@debug"
		   newValue="false" />
</project>

Then, a nant target loads the xml and the web.config and and replaces the values:

 
<!-- replaces certain fields in web.config with production values -->
<target name="prepare.web.config" description="replace certain fields with production values in web.config" >
	<xml-foreach file="${map.file}" xpath="/project/key" >
		<xmlpropertybinding>
			<get xpath="@xpath" property="xpath" />
			<get xpath="@newValue" property="newValue" />
		</xmlpropertybinding>
		<do>
			<xmlpoke file="${web.config.file}" xpath="${xpath}" value="${newValue}" verbose="false" />
		</do>
	</xml-foreach>
</target>

This target is called within the main target that wraps the whole project build task:

<!-- prepare Delta web.config in deployment directory -->
<target name="prepare.Delta.config">
	<property name="map.file" value="Delta.${db.type}.config.xml" />
	<property name="web.config.file" value="${kit.dir}\Delta\Web.config" />
	<call target="prepare.web.config" />
</target>
<!-- build Delta -->
<target name="Delta" >
	<call target="update.dependencies" cascade="false"/>
	<call target="startup" cascade="false"/>
	<call target="ms.build" />
	<call target="make.Delta.kit" />
	<call target="prepare.Delta.config"/>
	<call target="zip.Delta" />
	<echo message="Build finished." />
</target>
So, within seconds from clicking the "Force Build" or from last commit in svn, we have a package ready to be deployed to either DEV, TEST or PR environments ;).

ccnet

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s