Search code examples
nuget.net-corenuget-packagedotnet-cli

Override the NuGet package version from CLI


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?


Solution

  • 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.