Access current git commit number from within Heroku app

前端 未结 3 1195
一向
一向 2021-01-14 03:55

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 run

3条回答
  •  别那么骄傲
    2021-01-14 04:28

    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.

提交回复
热议问题