Gopinath D
Gopinath D

Reputation: 177

Get git commit id for C# .Net Core Application

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?

Upvotes: 6

Views: 5060

Answers (2)

Philippe
Philippe

Reputation: 31137

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.

Upvotes: 3

paulsm4
paulsm4

Reputation: 121649

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:

Upvotes: 1

Related Questions