Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add build number to package version with `dotnet pack` in VSTS Build process

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?

like image 697
Quango Avatar asked May 01 '18 10:05

Quango


People also ask

Does dotnet pack build?

By default, dotnet pack builds the project first. If you wish to avoid this behavior, pass the --no-build option. This option is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built.

How do I change NuGet package version?

Right-click the Packages folder in the project, and select Update. This will update the NuGet package to the latest version. You can double-click the Add packages and choose the specific version.

Does NuGet use SemVer?

NuGet 4.3. 0+ supports SemVer 2.0. 0, which supports pre-release numbers with dot notation, as in 1.0.

What is the difference between the dotnet pack and dotnet publish commands?

dotnet pack : The output is a package that is meant to be reused by other projects. dotnet publish : The output is mean to be deployed / "shipped" - it is not a single "package file" but a directory with all the project's output.


1 Answers

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.

like image 123
PatrickLu-MSFT Avatar answered Oct 18 '22 19:10

PatrickLu-MSFT