Philip Atz
Philip Atz

Reputation: 938

TeamCity Build Configuration to populate environment variable from XML

When editing the settings for a Build Configuration in TeamCity, is there a way of parsing an XML file to generate an environment variable that will later be used in the Build Steps? The XML file I want to parse (let's say version.xml) contains the version number that is going to be used later:

<Version>
    <Major>2015</Major> 
    <Minor>2</Minor> 
</Version>

And I want to be able to use %env.VersionNumber% later to get "2015.2"

Upvotes: 1

Views: 1945

Answers (2)

Philip Atz
Philip Atz

Reputation: 938

I ended up using a Powershell script for the parsing:

$Source = @" 
using System;
using System.Xml.Linq;

namespace VersionTools
{ 
    public static class XmlVersionParser
    { 
        public static string GetVersion(string xml)
        { 
            var doc = XDocument.Load(xml);
            var major = doc.Root.Element("Major").Value;
            var minor = doc.Root.Element("Minor").Value;
            return string.Format("{0}.{1}", major, minor);
        } 
    } 
} 
"@ 

Add-Type -ReferencedAssemblies ("System.Xml", "System.Xml.Linq") -TypeDefinition $Source -Language CSharpVersion3

$xmlVersion = [VersionTools.XmlVersionParser]::GetVersion(".\version.xml")
Write-Host "##teamcity[setParameter name='env.XmlVersion' value='$xmlVersion']"

Then, on the last line, I used Biswajit_86's recommendation using ##teamcity

Upvotes: 2

Biswajit_86
Biswajit_86

Reputation: 3739

Yes, you can. You can parse the xml that you want to and set the properties in the 1st build step. You can read Teamcity's confluence page for more details.

##teamcity[setParameter name='env.build.version' value='xml.parsed.value']

You can use the property that you set in any of the subsequent build steps.You can even pass it around to subsequent builds.

You can set as many properties as you want , for ex in bash script

echo "##teamcity[setParameter name='env.build.version.major' value='$xml.parsed.value.major']"
echo "##teamcity[setParameter name='env.build.version.minor' value='$xml.parsed.value.minor']"
echo "##teamcity[setParameter name='env.build.version' value='${xml.parsed.value.major}.${$xml.parsed.value.minor}']"

Upvotes: 1

Related Questions