交换测试数据库连接代码作为构建后步骤

发布于 2024-09-18 20:11:28 字数 232 浏览 7 评论 0原文

在推广或部署构建时,我想自动交换单个数据库连接文件。这可以作为构建后步骤或部署前的预打包步骤来完成。

被换出的文件是测试文件;被交换的文件应该具有真实的数据库连接配置。

这怎么能做到呢?我使用 Hudson 作为 CI 服务器(如果有帮助的话),并使用 GitHub 作为 SCM。

恕我直言,手动交换文件充满了人为错误,并且可能会被完全忘记。此外,它还增加了一件事情要做,并阻碍了持续部署周期的势头。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-09-25 20:11:28

也许您不需要替换整个文件。作为部署脚本的一部分,我们使用 powershell 读取配置文件,并使用一些 xpath 魔法来查找数据库连接字符串并将其更改为测试/生产设置,具体取决于我们部署的位置。

实际上它是一个搜索和替换。

以下是用于修改 .NET Web 配置文件的代码片段,但也可以对其他基于 xml 的配置文件执行相同的操作。

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

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>)
捎一片雪花 2024-09-25 20:11:28

Windows

由于我没有 powershell:我基于 这篇博文。请注意此方法的一些限制。

linux/Unix

了解如何使用 sed(手册页)。如果您有 cygwin,您也可以使用 sed。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文