在 TeamCity 中更改 app.config 中的值

Change value in app.config within TeamCity

在包含我们所有单元测试的 Visual Studio 解决方案中,我们有一些文本文件。这些文本文件是根据我们的单元测试生成的一些结果进行检查的。

为了加载文件,我们有一个 app.config:

 <appSettings>
    <add key="BaseTestDataPath" value="D:\MyPath\MySolution\" />
 </appSettings>

在每个构建的 TeamCity 中 运行 我想:

将 BaseTestsDataPath 更改为代理的特定工作路径,例如。

C:\TeamCity\buildAgent\workca1a73fe3dadf57\MySolution\

我知道代理工作文件夹中的物理布局,所以我需要知道的是:

有几种方法可以解决这个问题。

只需选择以下脚本之一,将其添加到您的源代码管理并在您的构建配置中设置一个 PowerShell 构建 运行ner 以 运行 脚本传递所需的参数,在您之前运行 NUnit 步骤。如果您选择选项二,那么您还需要考虑转换 dll。

AppSettingReplace.ps1

如果您只想更改单个值,您可以使用一些简单的 PowerShell 来实现,它将配置文件加载到 xml 文档中,迭代应用程序设置并更改匹配的设置。

# -----------------------------------------------
# Config Transform
# -----------------------------------------------
#
# Ver   Who                     When      What
# 1.0   Evolve Software Ltd     13-05-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $ConfigurationFile = $(throw "-ConfigurationFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApplicationSetting = $(throw "-ApplicationSetting is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $ApplicationSettingValue = $(throw "-ApplicationSettingValue is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"

    Write-Host "================== Config Transform - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    try {
        $xml = [xml](get-content($ConfigurationFile))
        $conf = $xml.configuration
        $conf.appSettings.add | foreach { if ($_.key -eq $ApplicationSetting) { $_.value = $ApplicationSettingValue } }
        $xml.Save($ConfigurationFile)
    } 
    catch [System.Exception] {
        Write-Output $_
        Exit 1
    }

    Write-Host "================== Config Transform - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "ConfigurationFile: " $ConfigurationFile
    Write-Host "ApplicationSetting: " $ApplicationSetting
    Write-Host "ApplicationSettingValue: " $ApplicationSettingValue
    Write-Host "Computername:" (gc env:computername)
}

Main

用法

AppSettingReplace.ps1 "D:\MyPath\app.config" "BaseTestDataPath" "%teamcity.build.workingDir%"


XdtConfigTransform.ps1

另一种方法是使用 XDT 提供完整的配置转换支持——这确实需要 Microsoft.Web.XmlTransform.dll 以某种方式结束在服务器上(我通常把它放在进入源代码管理)。

以下脚本会将一个配置文件转换为另一个配置文件。

# -----------------------------------------------
# Xdt Config Transform
# -----------------------------------------------
#
# Ver   Who                     When      What
# 1.0   Evolve Software Ltd     14-05-16  Initial Version

# Script Input Parameters
param (
    [ValidateNotNullOrEmpty()]
    [string] $ConfigurationFile = $(throw "-ConfigurationFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $TransformFile = $(throw "-TransformFile is mandatory, please provide a value."),
    [ValidateNotNullOrEmpty()]
    [string] $LibraryPath = $(throw "-LibraryPath is mandatory, please provide a value.")
)

function Main() 
{
    $CurrentScriptVersion = "1.0"

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": START =================="

    # Log input variables passed in
    Log-Variables
    Write-Host

    if (!$ConfigurationFile -or !(Test-Path -path $ConfigurationFile -PathType Leaf)) {
        throw "File not found. $ConfigurationFile";
        Exit 1
    }
    if (!$TransformFile -or !(Test-Path -path $TransformFile -PathType Leaf)) {
        throw "File not found. $TransformFile";
        Exit 1
    }

    try {

        Add-Type -LiteralPath "$LibraryPath\Microsoft.Web.XmlTransform.dll"
        $xml = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
        $xml.PreserveWhitespace = $true
        $xml.Load($ConfigurationFile);

        $xmlTransform = New-Object Microsoft.Web.XmlTransform.XmlTransformation($TransformFile);
        if ($xmlTransform.Apply($xml) -eq $false)
        {
            throw "Transformation failed."
        }
        $xml.Save($ConfigurationFile)
    } 
    catch [System.Exception] {
        Write-Output $_
        Exit 1
    }

    Write-Host "================== Xdt Config Transform - Version"$CurrentScriptVersion": END =================="
}

function Log-Variables
{
    Write-Host "ConfigurationFile: " $ConfigurationFile
    Write-Host "TransformFile: " $TransformFile
    Write-Host "LibraryPath: " $LibraryPath
    Write-Host "Computername:" (gc env:computername)
}

Main

用法

XdtConfigTransform.ps1 "D:\MyPath\app.config" "D:\MyPath\app.transform.config" "%teamcity.build.workingDir%\Library"

注意:最后一个参数是包含Microsoft.Web.XmlTransform.dll

的目录路径

Github 存储库 - teamcity-config-transform

希望对您有所帮助

您可以使用 File Content Replacer 构建功能在构建之前在文本文件中执行正则表达式替换。构建完成后,将文件内容恢复到原来的状态。

您可以选择使用 nuget 包 id="SlowCheetah"。这为 app.config 添加了转换。 在构建时它会转换,因此不需要额外的脚本或 dll。