Last active
August 12, 2022 19:06
-
-
Save ElanHasson/30384431651534b215e754a554b5b6ae to your computer and use it in GitHub Desktop.
Adding Git Commit Hash to your .NET project assemblies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project> | |
| <Choose> | |
| <When Condition="'$(CI)' == 'true'"> | |
| <!-- On non-official builds we don't burn in a git sha. In large part because it | |
| hurts our determinism efforts as binaries which should be the same between | |
| builds will not (due to developers building against different HEAD | |
| values --> | |
| <PropertyGroup> | |
| <GitHeadSha><developer build></GitHeadSha> | |
| </PropertyGroup> | |
| </When> | |
| <When Condition="'$(GITHUB_SHA)' != ''"> | |
| <PropertyGroup> | |
| <GitHeadSha>$(GITHUB_SHA)</GitHeadSha> | |
| </PropertyGroup> | |
| </When> | |
| <When Condition="'$(GitHeadSha)' == ''"> | |
| <PropertyGroup> | |
| <GitHeadSha>Not found</GitHeadSha> | |
| <DotGitDir>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory).git'))</DotGitDir> | |
| <HeadFileContent Condition="Exists('$(DotGitDir)/HEAD')">$([System.IO.File]::ReadAllText('$(DotGitDir)/HEAD').Trim())</HeadFileContent> | |
| <RefPath Condition="$(HeadFileContent.StartsWith('ref: '))">$(DotGitDir)/$(HeadFileContent.Substring(5))</RefPath> | |
| <GitHeadSha Condition="'$(RefPath)' != '' AND Exists('$(RefPath)')">$([System.IO.File]::ReadAllText('$(RefPath)').Trim())</GitHeadSha> | |
| <GitHeadSha Condition="'$(HeadFileContent)' != '' AND '$(RefPath)' == ''">$(HeadFileContent)</GitHeadSha> | |
| </PropertyGroup> | |
| </When> | |
| <Otherwise> | |
| <PropertyGroup> | |
| <GitHeadSha>Not found</GitHeadSha> | |
| </PropertyGroup> | |
| </Otherwise> | |
| </Choose> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project> | |
| <PropertyGroup> | |
| <InformationalVersion>$(GitHeadSha)</InformationalVersion> | |
| </PropertyGroup> | |
| </Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment