Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Infrastructure for append git metadata

Combo of files that add git metadata to your dlls and nugets

  1. Put Directory.Build.props and Directory.Build.targets to root of your repository (or to where exist .sln file). These files are automatically added by msbuild to all projects (csprojs) that are in file system in folders under .targets and .props files.
  2. On your CI set required GitBranch msbuild property. This is required because most of CI do not checkout to branch, but to commit so you need explicitly add this to msbuild (dotnet cli). It is added to nuget metadata.
    1. [optional] (but recommended) - BuildNumber is used for build number for prerelease packages (it is used . instead of + because nuget do not support it). If is not filled, is used git SHA (with this is not working proper prerelease sorting in git) (i.e. 20201222.12 will be `1.0.0-prereleasefield..20201222.12)
    2. [optional] - PreReleaseField is used for specifying prerelease field. If is not filled it is created from git branch name without "/"

Git metadata in nugets (repository url, git branch, git commit)

  • examples of nuget versions
    • stable branch 1.0.0
    • feature branch 1.0.0-featurenewitem.20201222.11
    • stable dirty branch 1.0.0-dirty
    • feature dirty branch 1.0.7-featurenewitem.dirty.20201222.11
  • Add Git metadata to nugets (repository url, git branch, git commit)
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="">
    <authors>Paradajka Paprika</authors>
    <description>Package Description</description>
    <repository type="git" url="" branch="feature/newitem" commit="4d21db7d9389f9d94d03b820ae4f5a9970390249" />
      <group targetFramework=".NETStandard2.0" />

Git metadata in dlls

  • Add git metadata to all created dlls (via set InformationalVersion)
  • If is dll/nuget created with some nonstandard way (from dirty repo) add dirty field to dll InformationalVersion and nuget version.
  • Examples of InformationaVersion ("Product version") values
    • stable branch 1.0.7-sha+4d21db7d9389f9d94d03b820ae4f5a9970390249
    • feature branch 1.0.7-gitmetadatainfrastructure-sha+1552f2a0b01f42f73943e9b532c6fa175e21351c
    • stable dirty branch 1.0.7.dirty-sha+f2a475b3c352cb87368979bc25fa0db81b2c37c1
    • feature dirty branch 1.0.7-gitmetadatainfrastructure.dirty-sha+1552f2a0b01f42f73943e9b532c6fa175e21351c


  1. Add Directory.Build.props and Directory.Build.targets to root of you project (beside .sln)
  2. Set props in Directory.Build.props
  3. On you CI specify build props like
  projectToNuget: './GitMetadataInfrastructure/GitMetadataInfrastructure.csproj'
  # this just normalize branch name - i.e. from `refs/heads/master` do just `master`
  gitBranch:  ${{ replace(variables['Build.SourceBranch'],'refs/heads/','') }}
  buildConfiguration: Release

#restore step
#build step

# by cli this could be 
# dotnet pack ./GitMetadataInfrastructure/GitMetadataInfrastructure.csproj -c Release -p:BuildNumber=123 -p:GitBranch="mysuperfeature"

- task: DotNetCoreCLI@2
  displayName: "Pack ${{ parameters.projectToNuget}}"
    command: 'pack'
    packagesToPack: $(projectToNuget)
    versioningScheme: 'off'
    buildProperties: 'BuildNumber=$(Build.BuildNumber);GitBranch=$(gitBranch)'
    arguments: '--configuration $(buildConfiguration)'
<Copyright>© 2021 Paradajka Paprika</Copyright>
<Authors>Paradajka Paprika</Authors>
<Company>Paradajka Paprika</Company>
<PackageReference Include="GitInfo" Version="2.1.2" PrivateAssets ="All">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<!-- -->
<Target Name="AddGitMetadata" AfterTargets="GitVersion">
<!-- most CI do checkout to bare commit not branch, in this case set p:GitBranch while inkoking msbuild/dotnet cli -->
<BuildNumber Condition="$(BuildNumber) == ''">$(GitCommit)</BuildNumber>
<!-- pre-release versions of nugets-->
<PropertyGroup Condition="'$(GitBranch)' != '$(StableVersionBranch)'">
<!-- create prerelease field from git brachn if is not specified -->
<PreReleaseField Condition="'$(PreReleaseField)' == ''">$(GitBranch.Replace("/", ""))</PreReleaseField>
<!-- if is package/dll builded from dirty repo add reprerelease field dirty -->
<PackageVersion Condition="'$(GitIsDirty)' == '1'">$(PackageVersion).dirty</PackageVersion>
<!-- nuget do not support semversion 2 build "+" suffix, add "." instead -->
<!-- Add suffix prerelease "dirty" field if is builded frogom dirty StableVersionBranch -->
<PropertyGroup Condition="'$(GitIsDirty)' == '1' AND '$(GitBranch)' == '$(StableVersionBranch)'">
<!-- append +gitSHA to every dll-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment