简体   繁体   中英

Heroku: Display Git Revision Hash and Timestamp in Views?

Let's say I have a Rails application deployed on Heroku . How can I display these pieces of information in my views?

  • The Git hash for the last revision
  • The Timestamp for the last revision

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 .

这样做的原因是因为当你的应用程序被部署到dyno网格上以服务请求时,它被编译成一个“slug”以便快速部署,而这个slug不再有git repo了。

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

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM