I have a development branch hosted on Heroku and we have a couple of people using this branch to look for bugs, it would be nice to show the SHA-1 Hash of the commit that’s the latest deployed on Heroku so that we know which bugs belong to which commit.
But I cannot at all find any way to find this information. nothing in the ENV variable in heroku run console. Though “heroku releases” does show a list of deployment info, including the first few character of the SHA-1 hash, that leads me to think that Heroku must store it somehwere, but I just cannot find where. Does anyone know?
I realize that I haven’t really put down the question as clear as I should: I meant to find the SHA-1 hash inside Rails on Heroku. Like I can do something like this:
<h1><%= ENV['REV']</h1>
Thank You!
Treat Heroku just like any other remote Git repo – you can use
git ls-remote:git ls-remote heroku(
herokuhere being the remote name)UPDATE:
Since the OP is actually looking to acquire the SHA in the Ruby env, one possible way would be to use a custom buildpack.
To get started, head over to Heroku’s Ruby Buildpack and fork it so you can make your own variations. Now clone your fork and take a look at
lib/language_pack/ruby.rb. Add a new method, something like:How you go about getting the SHA is up to you. You could execute a git command and use what’s returned:
git log -1 --format="%H"Or you could use @avaynshtok’s advice and use the Heroku gem to use the
releasesmethod.Then, once you have the SHA, set it as an ENV var.
Next, find the
compilemethod inruby.rb, and add theget_shamethod to the end of it:Push your changes back up to GitHub, and now head over to the command line. You’ll need to add a new config var to your Heroku app:
heroku config:add BUILDPACK_URL=git@github.com:<your GitHub username>/heroku-buildpack-ruby.gitNote that you’ll need to make sure you’ve replace
<your GitHub username>with…well, your GitHub username, so you are pointing at your forked repo.Finally, execute one last command that enables a Heroku labs feature that allows the slug compiler access to user vars:
heroku labs:enable user_env_compileNow you should be all set. So what exactly happens now? Well, when you push to Heroku, Heroku will receive the changes, and then see that you have a custom buildpack url set. So it’ll fetch your custom buildpack from GitHub, and then use that to create the slug. That means that once it runs through all of the default compile commands, it’ll end with your
get_shamethod, which should set the ENV varSHAto the appropriate SHA. Now you should have access to that ENV var from within Ruby, to do with what you will.