开发者

Swapping test database connection code as a post-build step

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 connect开发者_C百科ion 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.


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>)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜