I'm working for a .NET Core web api application. My requirement is to show repository's (Bitbucket) latest local git commit id of in the home controller. Can someone help me on this please?
Asked
Active
Viewed 2,770 times
4
-
4git commit id of what? The controller does not know that it's source was under version control. Maybe you can have your build pipeline write it to a configuration or source file before compiling? – nvoigt Jun 25 '21 at 16:52
-
Below is the local git commit id "b5519c35cd99cab61e9e8b6357a76ad05d272ca8" which I'm able to see in the git console using the comment "git describe --long". I need this id need to get in api. – Gopinath D Jun 25 '21 at 17:00
2 Answers
3
You also have solutions like :
that populate your AssemblyInfo.cs
file with some data linked to git and that you can use in your code and so display it.
The other solution is indeed to get it with a git command line and, maybe the more convenient, generate a partial class and add the generated file in the .gitignore
file.
And then, here again, you could use the value.

Philippe
- 28,207
- 6
- 54
- 78
2
You need to do the following:
- Modify your build script to query the Git commit ID
- There are many different options, depending on how your building your app.
- EXAMPLE:
MSVS > Project > Properties > Build Events > Pre-build event command line
- The script needs to write the commit ID somewhere.
- Your C# app needs to read the commit ID somehow. EXAMPLE:
- Write to file
git-info.txt
- Check your project properties and ensure that the git-info.txt file is copied in your app's build
- Write the code to read and display it at runtime: MSDN: How to read from a text file (C# Programming Guide)
- Write to file

paulsm4
- 114,292
- 17
- 138
- 190
-
-
1The key issue is that you need *something* in your "build pipeline" to 1) read the value from Git, and then 2) write that value somewhere for C#. I suggested "MSVS Build Events" as one possibility. If happen to be using [BitBucket CI/CD pipelines](https://bitbucket.org/product/features/pipelines), you have many other options. You can easily have a C# .cs source file with the text `@@GIT_ID@@` in it, and then have your "build script" (whatever you decide) do a text substitution with the "real" Git commit ID. – paulsm4 Jun 25 '21 at 21:48