Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heroku console show the latest sha hash from git deployment

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!

like image 625
Nik So Avatar asked Jun 11 '12 00:06

Nik So


2 Answers

Treat Heroku just like any other remote Git repo - you can use git ls-remote:

git ls-remote heroku

(heroku here 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:

def get_SHA
    #get SHA
    #save SHA to ENV, ala: ENV['SHA'] = retrieved_sha
end

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 releases method.

Then, once you have the SHA, set it as an ENV var.

Next, find the compile method in ruby.rb, and add the get_sha method to the end of it:

def compile
    Dir.chdir(build_path)
    install_ruby
    setup_language_pack_environment
    allow_git do
        install_language_pack_gems
        build_bundler
        create_database_yml
        install_binaries
        run_assets_precompile_rake_task
        get_sha #your additional method
    end
end

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 [email protected]:<your GitHub username>/heroku-buildpack-ruby.git

Note 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_compile

Now 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_sha method, which should set the ENV var SHA to the appropriate SHA. Now you should have access to that ENV var from within Ruby, to do with what you will.

like image 96
redhotvengeance Avatar answered Oct 05 '22 23:10

redhotvengeance


You can get release info using the Heroku gem: https://github.com/heroku/heroku

c = Heroku::Client.new <LOGIN>, <PASSWORD>
c.releases(<APPNAME>).last['commit']

Alternatively, you can authenticate using your API key:

c = Heroku::Client.new '', <API_KEY>

The downside is that you'll need to store a login/password combo (or API key) somewhere (either in your app or Heroku env).

You can also use deploy hooks to get this info: https://devcenter.heroku.com/articles/deploy-hooks

like image 26
avaynshtok Avatar answered Oct 05 '22 23:10

avaynshtok