How do you extract a version number from nuspec into TeamCity?
Asked Answered
E

2

6

How do you extract a version number from nuspec into TeamCity?

We have a csproj file with this corresponding nuspec node:

1.3.2

In TeamCity I'd like to do a build of this project and create a NuGet package with version 1.3.2.%build.counter%. For example, 1.3.2.322. It is important the version number is coming from a file within source control (the NuSpec) and I don't want to duplicate this as a variable in TeamCity. Is there a way to extract 1.3.2 from the NuSpec file and use it as a TeamCity variable?

Extraditable answered 21/11, 2014 at 15:54 Comment(0)
S
4

A couple of approaches I can think of spring to mind, both using TeamCity service messages:

Use a PowerShell build step, something like this (apologies my PS isn't 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, bootstrap 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 would need to be added before any other step requiring the build number.

Something answered 23/11, 2014 at 22:52 Comment(1)
It's also worth noting that your generated version number will not be available for use in the AssemblyInfo build feature, as this will run when the server is fetching changes to build. This has tripped me up before; the only way around it is to chain an upstream build via a snapshot dependency that evaluates the version number.Something
P
6

This approach works with version 10+ of TeamCity and gets around 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']"
Pianoforte answered 29/9, 2016 at 13:49 Comment(1)
I'm also using this style with AppVeyor by replacing %build.counter% with $env:APPVEYOR_BUILD_NUMBER and calling the choco pack with --version $Version.$BuildCounter. Thanks for the awesome and concise syntax.Remorse
S
4

A couple of approaches I can think of spring to mind, both using TeamCity service messages:

Use a PowerShell build step, something like this (apologies my PS isn't 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, bootstrap 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 would need to be added before any other step requiring the build number.

Something answered 23/11, 2014 at 22:52 Comment(1)
It's also worth noting that your generated version number will not be available for use in the AssemblyInfo build feature, as this will run when the server is fetching changes to build. This has tripped me up before; the only way around it is to chain an upstream build via a snapshot dependency that evaluates the version number.Something

© 2022 - 2024 — McMap. All rights reserved.