Monday, March 11, 2013

Getting TeamCity Build Number with MSBuild

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Version>$(BUILD_NUMBER)</Version>
    <SourceXMLFile>..\src\UNNAMED.Web\web.config</SourceXMLFile>
    <DestXMLFile>..\src\UNNAMED.Web\web.config</DestXMLFile>
</PropertyGroup>

<ItemGroup>
    <XmlConfigUpdates Include="ConfigUpdates-WebConfig">
        <XPath>/configuration/appSettings/add[@key='BuildNumber']/@value</XPath>
        <NewValue>$(Version)</NewValue>
    </XmlConfigUpdates>
    <BuildArtifacts Include=".\buildartifacts\"/>
    <SolutionFile Include="..\src\UNNAMED.sln"/>
</ItemGroup>

<Target Name="UpdateWebConfig">
    <Message Text="Updating Web.config"/>
    <Copy SourceFiles="$(SourceXMLFile)" DestinationFiles="$(DestXMLFile)"/>
    <XmlPoke XmlInputPath="$(DestXMLFile)" Query="%(XmlConfigUpdates.XPath)" Value="%(XmlConfigUpdates.NewValue)"/>
</Target>

<Target Name="Clean">
    <RemoveDir Directories="@(BuildArtifacts)"/>
</Target>

<Target Name="Init" DependsOnTargets="Clean">
    <MakeDir Directories="@(BuildArtifacts)"/>
</Target>

<Target Name="Compile" DependsOnTargets="Init; UpdateWebConfig">
    <MSBuild Projects="@(SolutionFile)" Properties="OutDir=%(BuildArtifacts.FullPath);Configuration=$(Configuration)"/>
</Target>

No comments: