[英]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\work\1ca1a73fe3dadf57\MySolution\
我知道代理工作文件夾中的物理布局,所以我需要知道的是:
有幾種方法可以解決這個問題。
在運行NUnit步驟之前,只需選擇以下腳本之一,將其添加到源代碼控制並在構建配置中設置PowerShell構建運行器,以運行傳遞所需參數的腳本。 如果選擇選項2,那么您還需要考慮轉換dll。
如果您只想更改單個值,可以使用一些簡單的PowerShell實現此目的,該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%”
另一種方法是使用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 Repository - teamcity-config-transform
希望這可以幫助
您可以使用文件內容替換器構建功能在構建之前在文本文件中執行正則表達式替換。 構建之后,它將文件內容恢復到原始狀態。
您可以選擇使用nuget package id =“SlowCheetah”。 這為app.config添加了轉換。 在構建它變換,所以不需要額外的腳本或dll。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.