2

I work on an ASP.NET MVC web project, and we're developing several features concurrently. Each feature is on its own branch, and to test the site we publish it to a staging area.

However, after it's published I often forget from which branch we published.

Is there any way to inject the git branch name into the HTML somewhere, so the page will display <span>feature/coolNewFeature1</span> or <span>develop</span> as a reminder for which codebase is currently on the staging area?

RamblerToning
  • 926
  • 2
  • 13
  • 28
  • Where will this info will come from? do you generate the html on the server side? – CodeWizard Jan 15 '16 at 14:33
  • 1
    Well, that's what I don't know. I can add scripts into the build routine, but I don't know how I'd obtain the current git branch name in order to inject it. If I knew how to return the git branch name given the repo and write that to a text file or something, I could probably work out how to replace the markup from there. – RamblerToning Jan 15 '16 at 14:37
  • To get the branch is the easy thing. to echo it to the html – CodeWizard Jan 15 '16 at 14:40

2 Answers2

0
  1. Go to Project Properties and add a "pre-build event command line" script like:

git.exe rev-parse --abbrev-ref HEAD > "$(ProjectDir)\Resources\BuildInfo.txt"

  1. Build the project, mark the "BuildInfo.txt" file as an Embedded Resource

  2. In your footer cshtml, add code like:

        @{
            var buildInfo = Cache["buildInfo"];
            if (buildInfo == null)
            {
                using (var r = new StreamReader(typeof(HomeController).Assembly
                    .GetManifestResourceStream("My.Module.Web.Resources.BuildInfo.txt")))
                {
                    buildInfo = Cache["buildInfo"] = r.ReadToEnd();
                }
            }
        }
        @buildInfo
    
Rich
  • 15,048
  • 2
  • 66
  • 119
-1

I am not super comfortable with ASP .NET but I can think of the following sequence of steps (I assume that you will select a git branch, compile and then deploy - and there is a script that possibly is automating this):

  1. Create a small Javascript file (e.g. version.js) that declares

    var version_branch = "<your-branch>";
    
  2. Run a shell script (or it's equivalent on your platform)

    $ t=$(git branch | grep "*" | cut -d' ' -f2) && echo "var version = '"$t"';" > /path/to/js/file 
    
  3. Include this in the Javascript files you are serving - like you might be doing for other Javascript. Remember to include a <script> tag for this file on your main template page.

  4. When the page loads you can use jquery to inject this text in the footer as the accepted answer on this question suggests - Using CSS to insert text.

Hope this helps!

Community
  • 1
  • 1
subbu
  • 61
  • 7