Skip to the content.

Semantic Versioning

Semantic Versioning should be considered the default software product versioning strategy in any software company. I collected learning materials to build a learning path for .NET developers.

Software Versioning and Semantic Versioning may be implemented using GitVersion, which allows a consistent versioning strategy across build scripts, .NET projects, and Azure DevOps pipelines.

GitVersion is a powerful tool, but a good understanding of it may require a learning path.

  1. Semantic Versioning Given a version number MAJOR.MINOR.PATCH. Semantic Versioning provides version numbers and the way they change convey meaning about the underlying code and what has been modified from one version to the next.
  2. Control your GitHub releases with GitVersion and GitReleaseManager This video is related to GitHub but provides a good overview of GitVersion-related topics.
  3. Install and use a .NET local tool using the .NET CLI As a prerequisite for GitVersion, it is useful to have a basic understanding of .NET local tools.
  4. GitVersion site provides detailed documentation https://gitversion.net/
    1. GitVersion .NET local tool installation GitVersion’s command line interface can be installed and consumed in many different ways.
    2. MSBuild Task The MSBuild Task for GitVersion — GitVersion.MsBuild — is a simple solution if you want to version your assemblies without writing any command line scripts or modifying your build process.
    3. GitVersion Configuration GitVersion is mainly powered by configuration. In configuration you may bump the next version explicitly, set Semantic Version mode and many other settings to control versioning strategy.
    4. Version Variables Version variables are quite useful if you need different formats of the version number.
    5. Version Incrementing GitVersion works with several workflows. This page is split up into two sections, first is all about understanding the approach GitVersion uses by default, and the second is how you can manually increment the version.
    6. Versioning Modes Version supports a few different versioning modes. They are described in detail.

Query current GitVersion from git repository for build scripting.

A build script may query the current GitVersion JSON to generate build artifacts. Current state of GitVersion version variables after installation may be queried from command line. Initialize .NET Tools once in source code folder. dotnet tool restore Call GitVersion as local DotNet tool. dotnet tool run dotnet-gitversion GitVersion tool console output may be parsed and used by any build or utility scripting in F#, C# or Powershell script. F#5 interactive provides excellent scripting experience. F# may call .NET base class library or any Nuget package to parses Major, Minor, Patch and SemVer from GitVersion.

#r "nuget: FSharp.Json, 0.4.0"

open FSharp.Json
open System
open System.Diagnostics


type GitVersionResult = {
    Major: Int16
    Minor: Int16
    Patch: Int16
    SemVer: string
}

let getGitVersionResult(): GitVersionResult =
    let startInfo = 
        ProcessStartInfo(
            FileName = "dotnet", 
            Arguments = "tool run dotnet-gitversion",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true)
    use gitVersionToolProcess = new Process(StartInfo = startInfo)
    gitVersionToolProcess.Start() |> ignore 
    let gitVersionToolOutput = gitVersionToolProcess.StandardOutput.ReadToEnd()
    Json.deserialize gitVersionToolOutput

let gitVersion = getGitVersionResult()
Console.WriteLine("SemVer: " + gitVersion.SemVer)

Query current GitVersion from git repository for NPM and WebPack.

JS Single Page Application (SPA) developed with NPM package manager and WebPack bundler may get semantic version from GitVersion.

  1. Get GitVersion semantic version in webpack.config.js
    var child = require('child_process').execSync('dotnet tool run dotnet-gitversion');
    var gitVersionResult = JSON.parse(child.toString())
    console.log("DotNet-GitVersion-SemVer: " + gitVersionResult.SemVer)
    
  2. Add DefinePlugin to WebPack configuration.
    const config = {
     ...
     plugins: [
         ...
         new webpack.DefinePlugin({ __SemVer__: JSON.stringify(gitVersionResult.SemVer) })
     ]
     ...
    };
    
  3. Use defined global variable from application. ``` typescript declare var SemVer: string;

function MyComponent() { return {"SPA version: " + __SemVer__} }
```