Using GitVersion for AssemblyInfo.cs update

2.7k Views Asked by At

According to the documentation when dotnet gitversion is called with /updateassemblyinfo parameter it will recursively search for all AssemblyInfo.cs files in the git repo and update them. It will update the following assembly attributes:

  • AssemblyVersion will be set to the AssemblySemVer variable.
  • AssemblyFileVersion will be set to the MajorMinorPatch variable with an appended .0.
  • AssemblyInformationalVersion will be set to the InformationalVersion variable.

These variables currently have the following values according to the output from dotnet gitversion called without any parameters (copied from the entire JSON):

  • AssemblySemVer: 1.2.0.0
  • MajorMinorPatch: 1.2.0
  • InformationalVersion: 1.2.0-branch-name.1+104.Branch.branch-name.Sha.f00e3376da35703ab5e5527e44e942e7bd98s098

The question: is it possible to alter values in these variables in the predefined GitVersion.yml or somehow "on the fly" via parameters to dotnet gitversion?

The reason being my current project doesn't follow semver scheme historically and there is a need to set 3rd part of the version quartet to specific value every time regardless of branches, tags or any other git-specifics. For example our products versions looks like v1.2.50.10 or v02.04.30.01.

But it would be nice to use dotnet gitversion build-in ability to update AssemblyInfo.cs files even if we would not use it's full potential of semver compliant versioning.

1

There are 1 best solutions below

1
Geo On

You can use environment variables to construct your version number or also GitVesrsion variables: https://gitversion.net/docs/reference/variables https://gitversion.net/docs/reference/configuration

For example, If you want to set the 3 the variable, in the informational version, with an environment variable and strictly increment the last one by using the CommitsSinceVersionSource GitVersion variable, you should add the following in you GitVersion.yml:

assembly-informational-format: '{Major}.{Minor}.{env:MY_NUMBER}.{CommitsSinceVersionSource}'