2

I like to use the build number or some other computed version number in my CICD pipeline for NuGet packages. Essentially, I like the developer to control the major and minor version numbers but increment the build automatically.

With the nuget.exe I could use the -Version switch to override the version in the final package. However with a .NET Core 2.0 library project this fails, and I have to use dotnet pack, see:

https://github.com/NuGet/Home/issues/4491

The is only a --version-prefix CLI argument for dotnet pack so how do I override the version completely?

1 Answer 1

6

You can use the barely-documented /p option:

dotnet pack /p:PackageVersion=2.1.0

See

https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-pack?tabs=netcore2x

Obviously, you can combine this as you like in your CICD tool, like for VSTS:

/p:Version=$(Build.BuildNumber)

Note VSTS has a few options for automatic versioning within the .NET Core build task when "pack" is the verb (in preview as of 5th Oct 2017).

The final solution for me on VSTS was to use the .NET Core dotnet pack task with the Use the build number setting and the following as the build number format set in the Options for the build definition:

2.0.$(Build.BuildId)

The engineer will need to alter the version upon making large or breaking changes. I'd prefer this in the code; one day I'll write some script to hoist the number from the .csproj and into the build system.

Sign up to request clarification or add additional context in comments.

3 Comments

btw you can also define /p:BuildNumber=$(Build.BuildId) and then in the csproj have an <BuildNumer Condition="'$(BuildNumber)'==''">0</…> and <Version>2.0.$(BuildNumber)</…> so you can set a part of the version in the csproj
I tried it and it does not seem to do anything. Nor does it throw any errors.
Thank you @MartinUllrich. That worked very well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.