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?
-
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– nvoigt2021-06-25 16:52:04 +00:00Commented Jun 25, 2021 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– Gopinath D2021-06-25 17:00:03 +00:00Commented Jun 25, 2021 at 17:00
Add a comment
|
2 Answers
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.
Comments
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
2 Comments
Philippe
For me the best is to generate a file compiled in the application.
paulsm4
The 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, 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.