Friday, June 16, 2017

TeamCity - Update app.config after build step

Background

A new testing initiative required that UI tests run nightly against staging environment. This is in addition to the UI tests already configured to run on each push to the branches. This requirement posed a challenge where the UI tests now required to be pointed to staging URL instead of localhost. Complicating the problem was the fact that VSTest does not allow passing parameters through command-line. Even though there is a way available to supply test run parameters through run settings file, but this was not practical in this case.

Luckily the UI tests were relying on application settings (app.config) to retrieve the base URL. However, the challenge of updating this URL for a specific build configuration inside TeamCity still existed.


The Quick Solution

Include a build step in configuration immediately after the build step to run a PowerShell script to update config file at path \TargetPathIncludingReleaseConfiguration\ProjectAssembly.dll.config. 
The update should replace the existing application setting with the new value.


The Nitty Gritty

The TeamCity server's build steps below:

The AppSettingReplace.ps1 is a modified version of the original script found here: http://stackoverflow.com/questions/37201731/change-value-in-app-config-within-teamcity/37204969 provided by Evolve Software Ltd.

The modified source code is below, 

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."),
 [ValidateNotNullOrEmpty()]
    [string] $ProjectSettingsNameSpace = $(throw "-ProjectSettingsNameSpace is mandatory, please provide a value.")
)

function Main() 
{
#sample value for $ProjectSettingsNameSpace = [ProjectNamespace].Properties.Settings
    $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.applicationSettings[$ProjectSettingsNameSpace]
  
        $conf.setting | foreach {
   if ($_.name -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

2 comments:

  1. I'm expecting you have just introduced JetBrains TeamCity and you are hoping to redesign a current establishment. You can check what variant of TeamCity you are running by looking in the footer of the web interface.

    ReplyDelete