GitVersion vs dotnet pack on VSTS

Last night I run into a problem with GitVersion and dotnet pack command while running my builds on VSTS

The probem came up when I tried to specify a version number I’d like the package to have. There are answers on SO telling me how to do it and I’ve used it in the past. But this time it did not work for some reason, so I had to investigate.

Here is the way it worked for me.
To begin, edit your *.csproj file and make sure your package information is there:

    <Description>Some description</Description>
    <PackageReleaseNotes>This is a first release</PackageReleaseNotes>
    <Copyright>Copyright 2017 (c) Trailmax. All rights reserved.</Copyright>

Make sure that you don’t have any <Version> or <VersionPrefix> elements in there – these will break your stuff.

Then when you do your dotnet pack specify /p:Version=7.7.7 as a parameter to the command. i.e. dotnet pack --Configuration Release --Output d:/tmp /p:Version=7.7.7.

Assembly version number

You can specify assembly verion number in your *.csproj file or via old-school [assembly: AssemblyVersion("")] assembly attribute. Both will work in Core projects. However GitVersion does not know how to update version number in your csproj file but works pretty well with AssemblyInfo.cs file. By default Core projects do not come with AssemblyInfo.cs file and you need to create one yourself.

To save some effort for multiple projects in the same solution you can create AssemblyInfo.cs file next to your *.sln file and add this file as a reference to all your projects.

Here is a sample of AssemblyInfo.cs:

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("Trailmax")]
[assembly: System.Reflection.AssemblyProductAttribute("MyAwesome.Project")]
[assembly: System.Reflection.AssemblyTitleAttribute("MyAwesome.Project")]

[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("MyAwesome.Project")]

[assembly: AssemblyVersion("")]
[assembly: AssemblyInformationalVersion("1.2.3")]
[assembly: AssemblyFileVersion("")]

To add as link right click on your project: Add -> Existing Item -> select SolutionInfo.cs file -> Instead of Add select Add as Link.

Or you can add this to your csproj file:

    <Compile Include="..\SolutionInfo.cs" Link="SolutionInfo.cs" />

After that is done you can tell GitVersion to update this SolutionInfo.cs file with the new versions.

GitVersion pushes various formats for version numbers into envrionment variables and that can be used as part of dotnet pack command.

The Whole Thing

There is GitVersion addon for VSTS – install it and add it as a first step on your build pipeline. Point it to update your SolutionInfo.cs file.

Then when you need to create a package add another .Net Core build step. Put the command to be pack and add /p:VersionPrefix=$(GitVersion.NuGetVersion) to the arguments list.