views:

33

answers:

2

When promoting or deploying a build, I'd like to automate swapping out a single database connection file. This could be done as either a post-build step or as a pre-packaging step before deployment.

The file that's being swapped out is a test file; the file being swapped in should have the real database connection configuration.

How can this be done? I use Hudson as the CI server, if that helps, and use GitHub for SCM.

Manual swapping of the files, IMHO, is fraught with human error and can be altogether forgotten. Plus it adds just one more thing to do and impedes the momentum of a continuous deployment cycle.

A: 

Maybe you don't need to replace the entire file. As part of a deployment script we use powershell to read the config file and use a bit of xpath magic to find and change the database connection strings to test/production settings depending on where we're deploying.

Effectively its a search and replace.

The following is a snippet for modifying a .NET web config file but the same could be done for other xml based configuration files.

$Config = (Get-Content -Path <Path to web.config>) -as [xml]
@(
    @{ 
        xpath = '/configuration/appSettings/add[@key="Setting1"]'
        edit = { $_.value = <Setting1 value> } 
    },
    @{ 
        xpath = '/configuration/connectionStrings/add[starts-with(@name, "ConnString")]'
        edit = { $_.connectionString = "Data Source=<servername>;Initial Catalog=NorthWind;Integrated Security=SSPI" } 
    }
) | 
    ForEach-Object {
        $Config.SelectNodes($_.xpath) |
            ForEach-Object -Process $_.edit
    }
$Config.Save(<path to web.config>)
Darren
Great! Would you mind sharing a bit more (a bit of a mini how-to)? I'm a bit of a noob and would deeply appreciate it.
Josh Smith
Here's a small snippet. I'm not too familiar with Hudson so I'm not sure where this would be executed from.
Darren
A: 

Windows

Since I don't have powershell: I implemented a search and replace based on this blog post. Be aware of a few limitations of this approach.

linux/Unix

Learn how to use sed (man pages). If you have cygwin, you can use sed as well.

Peter Schuetze