6

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?

2
  • 4
    git 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? Commented 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. Commented Jun 25, 2021 at 17:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to do the following:

  1. 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
  2. The script needs to write the commit ID somewhere.
  3. Your C# app needs to read the commit ID somehow. EXAMPLE:

2 Comments

For me the best is to generate a file compiled in the application.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.