How to display branch and commit information on your application's dashboard

By AKM
February 21, 2020
In a web application, there is often a need to display the currently deployed branch and the commit. It is helpful for QAs or PMs to know exactly which version they are dealing with. Here is a simply way to do with in your Rails app
If you are using Git for source control and Capistrano to deploy, note that Capistrano creates a file called revisions.log on the server which records every deployment. So, we will read that file in a controller and display the information in the right view.

In the relevant Controller-action:
if Rails.env != "development"
  rev = `tail -1 #{Rails.root + "../../revisions.log"}`
  if rev.present? && rev.match(/Branch (\S+) \(at (\S+)\) deployed as release (\S+) by (\S+)/)
    @branch = $1
    @commit = $2
    @timestamp = Time.parse($3).in_time_zone
    @deployer = $4
  end
end

Now, you have the information populated in a bunch of @ variables. Then, in the view, simply display them:

<p>
  <% if Rails.env != "development" %>
    <b>Branch</b>: <%= @branch %> (commit <%= @commit.try(:[], 0..7) %>)<br />
    <b>By</b>: <%= @deployer %><br />
    <b>At</b>:
    <% if @timestamp.present? %>
      <%= @timestamp.strftime('%a %d/%b/%Y %I:%M%p') %> (<%= time_ago_in_words(@timestamp) %> ago)<br />
    <% end %>
  <% else %>
    &mdash;
  <% end %>
</p>

Note that this solution does not display anything on development. Hopefully, you don't need it there since you have the terminal where you can run git status any time :)
/ / / /