Access current git commit number from within Heroku app
Asked Answered
U

3

6

I know the slug compiler removes the .git directory when creating a heroku slug, but is there any way to configure Heroku so that I can access the currently running git commit number from within my scripts?

I'd like to be able to have a small link on my sinatra app (run within Heroku) which says "running version e72fb274a0" (or something similar). How can I retrieve this, or force the slug compiler to add it to an environment variable?

PROGRESS:

I reckon the best way to do this is to make a custom buildpack which writes the git commit version number to the heroku slug before the .git directory is deleted.

I've tried to do this (see my fork of the ruby buildpack) but the line I've added – line 23 – doesn't seem to be doing the job. Heroku sees & uses the new buildpack, but doesn't seem to write the file to the slug.

Anyone have any idea why my custom buildpack isn't working as expected?

Thanks,

JP

Unbelief answered 31/8, 2012 at 14:41 Comment(0)
S
1

You can run a script before deploy that store this information (maybe on a YAML)

using these a = `ls` (note that is not ' "apostrophe" sign is ` "inverse accute" sign)
the a variable will have the result of this bash command,
so you can do git = `git log` and then find the information you want it and store it. So you will be able to retrieve it later.

Did this helped ?

Sexpartite answered 31/8, 2012 at 15:6 Comment(2)
That's most of the way there - thanks! There's a command which would create that file (git show --pretty=%H | head -n 1 > commit.txt) but I don't know how to create a heroku pre-hook or similar to put it into the slug - do you?Unbelief
I don't know, I would look for 'git prereceive hook' but I heard that a lot of people have problems with Heroku and prereceive hooks together, so check this out!Sexpartite
D
7

A couple of options...

SOURCE_VERSION environment variable (build-time)

Since 1st April 2015, there's a SOURCE_VERSION environment variable available to builds running on Heroku. For git-pushed builds, this is the git commit SHA-1 of the source being built:

https://devcenter.heroku.com/changelog-items/630

(thanks to @srtech for pointing that out!)

An example of me using that variable in a build - if you look at the HTML served by the deployed app, you'll see the commit id is coming though in an HTML comment near the very bottom: https://gu-who.herokuapp.com/

/etc/heroku/dyno metadata file (run-time)

Heroku have beta functionality to write out a /etc/heroku/dyno metadata file onto your running dyno. If you email support you can probably get added to the beta. Here's a place where Heroku themselves are using it:

https://github.com/heroku/fix/blob/6c8ab7a/lib/heroku_dyno_metadata.rb

The contents look like this:

{
   "dyno":{
      "physical_id":"161bfad9-9e83-40b7-b385-78305db2f168",
      "size":1,
      "name":"run.7145"
   },
   "app":{
      "id":null
   },
   "release":{
      "id":50,
      "commit":"2c3a0b24069af49b3de35b8e8c26765c1dba9ff0",
      "description":null
   }
}

..so release.commit is the field you're after. I used to use this method until the SOURCE_VERSION variable became available.

Dekameter answered 25/2, 2015 at 12:20 Comment(0)
M
5

In 2018 this is what you want: https://devcenter.heroku.com/articles/dyno-metadata

heroku labs:enable runtime-dyno-metadata -a <app name>
Mulligrubs answered 2/3, 2018 at 16:50 Comment(0)
S
1

You can run a script before deploy that store this information (maybe on a YAML)

using these a = `ls` (note that is not ' "apostrophe" sign is ` "inverse accute" sign)
the a variable will have the result of this bash command,
so you can do git = `git log` and then find the information you want it and store it. So you will be able to retrieve it later.

Did this helped ?

Sexpartite answered 31/8, 2012 at 15:6 Comment(2)
That's most of the way there - thanks! There's a command which would create that file (git show --pretty=%H | head -n 1 > commit.txt) but I don't know how to create a heroku pre-hook or similar to put it into the slug - do you?Unbelief
I don't know, I would look for 'git prereceive hook' but I heard that a lot of people have problems with Heroku and prereceive hooks together, so check this out!Sexpartite

© 2022 - 2024 — McMap. All rights reserved.