How do you extract the version number from nuspec in TeamCity?

How do you extract the version number from nuspec in TeamCity?

We have a csproj file with this corresponding nuspec node:

1.3.2

In TeamCity, I would like to build this project and create a NuGet package with version 1.3.2.% Build.counter%. For example, 1.3.2.322. It's important that the version number comes from the file in the original control (NuSpec) and I don't want to duplicate it as a variable in TeamCity. Is there a way to extract 1.3.2 from a NuSpec file and use it as a TeamCity variable?

+3


source to share


2 answers


Several approaches I can think of in spring, use TeamCity overhead messages :

Use a PowerShell build step, something like this (sorry my PS is not great):

$buildcounter = %build.counter%
$node = Select-Xml -XPath "/package/metadata/version" -Path /path/to/nuspec.nuspec
$version = $node.Node.InnerText
Write-Output "##teamcity[buildNumber '$version.$buildcounter']"

      



Or, similarly, download a tool like XmlStarlet :

$buildcounter = "1231"
$version = xml sel -T -t -v "/package/metadata/version" Package.nuspec
Write-Output "##teamcity[buildNumber '$version.$buildcounter']"

      

This step must be added before any other step that requires a build number.

+3


source


This approach works with version 10+ of TeamCity and addresses issues with Select-Xml

and namespaces.



$Version = ([xml](Get-Content .\MyProject.nuspec)).package.metadata.version
$BuildCounter = %build.counter%

echo "##teamcity[setParameter name='PackageVersion' value='$Version.$BuildCounter']"

      

+2


source







All Articles