Let\'s say I have a Rails application deployed on Heroku. How can I display these pieces of information in my views?
There is grit installed on Heroku. So you can open the repository there using it.
repo = Repo.new(Rails.root + '.git')
last_commit = repo.commits.first
p last_commit.id
p last_commit.authored_date
Heroku sets an environment variable with the commit hash ENV['COMMIT_HASH']
.
As for the timestamp, you could hit the github api with the hash if you host your code there. Looks like the ruby-github gem can help you with this, or you could do it yourself with HTTParty.
The reason for this is because when your app is deployed onto the dyno grid to serve requests, it's compiled into a "slug" for fast deployment, and this slug doesn't have the git repo with it anymore.
I think you need to config.gem 'grit' into your Rails app in order to be able to create the Repo object.
You can read about grit here http://github.com/mojombo/grit/
Another way to do it is to deploy with a rake task that gets the version info you want from the local repo and updates an environment variable on the Heroku side. Then you can use a tag, or a commit hash, or anything else, without having to rely on behaviors on the Heroku side.
For example, if you wanted to use the latest tag, in your rake task:
def app_version
%x[git describe --tags --abbrev=0].strip
end
Then in the body of your task:
run "git push blah:blah blah"
run "heroku config:add APP_VERSION=#{app_version}"
I would like to be able to get that info straight from git on Heroku, rather than sneaking it in indirectly, but I've never been able to figure out how to do that.