11

With a .NET Framework library you could specify a version with a wildcard and NUGET pack command would append the build date and version automatically when running a NUGET Build Task in VSTS.

[assembly: AssemblyVersion("1.0.*")]

NUGET PACK would generate a NUPKG file with a version like 1.0.6604.1234 appending the date number and a build ID.

NET Standard issues

In .NET Core and .NET standard the new .csproj format does not support this wildcard format.

We can't package with Nuget.exe (reason: this issue) but we can use dotnet pack except I need to auto-increment the build numbers. The dotnet Build Task in VSTS allows me to wholly replace the version number, but I want to retain the version in the csproj file, and just append a build number (as I used to).

I found that using <VersionPrefix>x.y</VersionPrefix> in the csproj file would work with nuget pack and I could then add the additional parameter VersionSuffix=$(Build.BuildNumber) to the pack task.

All looked good until the first dev updated the project version in the project properties dialog. Visual Studio ignored the VersionPrefix and set the <Version> tag - and the build number fix is ignored because a Version tag exists.

Is there a way to read the Version from the csproj? If so I could set the build property to Version=$(ProjectVersion).$(Build.BuildNumber) ?

Or are there alternative ways to handle auto-incrementing the build version when packaging?

Quango
  • 12,338
  • 6
  • 48
  • 83

2 Answers2

9

First you can select Use an environment variable for Automatic package versioning, use your defined variable such as temp ($(build.buildNumber)) as Environment variable.

enter image description here enter image description here

More details take a look at this link: Dotnet pack automatic package versioning build number clarification

Another way is using the "arguments" field in the dotnet CLI task, you can pass additional arguments to the dotnet cli.

Using --version-suffix $(Build.BuildNumber) will pass the build number as version suffix. Make sure you don't have a <version> element set in your csproj, but rather a <versionprefix> element. The built version will look like versionprefix-versionsuffix, so for example, if you have <versionprefix>1.2.3</versionprefix> and build number 201805002, the built version will be 1.2.3-201805002. In this case do not select the automatic package versioning.

PatrickLu-MSFT
  • 49,478
  • 5
  • 35
  • 62
  • Thanks for the response. I did know about the option to use environment var, but this doesn't allow me to use the Version from the csproj, only the VersionPrefix - which as I mentioned is easily overlooked by staff. – Quango May 02 '18 at 12:19
  • 1
    @Quango Have you tried this solution: Edit .csproj and set `1.0.0$(VersionSuffix)` and `dotnet pack --version-suffix -XXX` The logic is defined [here](https://github.com/dotnet/sdk/blob/94a3f2856cb09e66ee7472820b4e26fb576b4686/src/Tasks/Microsoft.NET.Build.Tasks/build/Microsoft.NET.DefaultAssemblyInfo.targets#L18-L22). Details please refer this question in github: [Unable to use in .csproj with --version-suffix during dotnet pack](https://github.com/dotnet/cli/issues/5681). – PatrickLu-MSFT May 02 '18 at 15:01
  • @Quango Also suggest you take a look at this thread: [dotnet build --version-suffix not working?](https://github.com/dotnet/cli/issues/3778) – PatrickLu-MSFT May 02 '18 at 15:03
5

Thanks to @patricklu-msft for his suggestions.

There is it appears no built-in way to emulate the wildcard behaviour we previously had NUGET pack with dotnet pack, nor was there way to get the <Version> tag out of the project file.

So I've created a new VSTS Build task that does this: VersionTaskReader in the MarketPlace.

This extension can be pointed to a .csproj or .vbproj and will set an environment variable VERSION, and VERSION_BUILD which has the BUILDID appended. You can optionally add a prefix to make each instance different if needed.

For example, if your project contains <Version>1.1</Version> then the VERSION_BUILD would be something like 1.1.8680

Then the dotnet pack task can use the environment variable VERSION_BUILD in the versioning options screen, so that the build number automatically increments.

Quango
  • 12,338
  • 6
  • 48
  • 83