Access Current Git Commit Number from Within Heroku App

Access current git commit number from within Heroku app

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 ?

Heroku - Display hash of current commit in browser

Firstly, since heroku "remove[s] unused files, including the .git directory" during slug compilation, you won't be able to execute some git commands from inside your app's directory (on the heroku dyno). This includes things like git rev-parse HEAD, which is normally an easy way to get the current hash.

Secondly, trying to retrieve information with git ls-remote on the heroku dyno will invoke ssh, and you'll see messages that say The authenticity of host 'heroku.com (50.19.85.132)' can't be established, since the heroku public key is not installed on heroku dynos. You won't have permission to install the heroku public key.

You still have at least two options.

  1. Add a post-commit hook to update the hash value.

    a) Create or edit the file .git/hooks/post-commit
    b) Add some shell script code like this:

    hash_name=HEAD_HASH

    hash=$(git rev-parse HEAD)

    echo Setting $hash_name to $hash

    heroku config:set $hash_name=$hash --app yourappname

    (you can use whatever code you want for git hooks; this is just one option)

    Explanation:

    • HEAD_HASH is the name of the heroku environment variable. Call it whatever you want. You'll look this up in your main app and display it on the page.
    • git rev-parse HEAD grabs the hash of the current HEAD commit. Customize this line for whatever you want to display.


    Now when you make commits to git the HEAD_HASH env var will be updated each time. This works, but might be a bit slow, as you'll be waiting for heroku to set the env var each time you commit. If your network connection is out etc. the variable won't be updated. Rumour is that git 1.8.2 will allow a 'pre-push' hook where you could put this code instead.

  2. Use a script to push your code

    Instead of typing git push heroku master to push your code, you could write a shell script that contains the lines from option 1. and adds git push heroku master at the end. Then to deploy your code you run this shell script. This will update the HEAD_HASH only before pushing (instead of after each git commit), and it nicely keeps everything in one place. You'll probably want to add the script to your .slugignore file too.

Can the runtime of a Heroku app know it's commit id?

Three different options, best first...

SOURCE_VERSION environment variable (build-time)

From 1st April 2015, there's a SOURCE_VERSION environment variable available to builds running on Heroku. For git-push 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!)

/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.

SBT-specific: use sbt-heroku to build slug locally

My initial solution was to use the sbt-heroku plugin published by Heroku themselves. This means no longer doing deploys by git push (having the slug compiled on Heroku's own infrastructure), but instead compiling the slug locally and uploading that directly to Heroku. Because the slug is compiled locally, in my work repo, the Git information is all there and the build process can easily identify the commit id and embed it into the app's code.

The slug is substantially larger in size that a Git diff (90MB vs 0.5KB), but apart from that the solution works reasonably well. I'm actually using Travis to do continuous deployment, and so Travis was doing the sbt stage deployHeroku for me (the Git commit id is available in that environment while building), meaning I could git-push from my laptop on the move, and Travis will do the large slug upload to Heroku.

Heroku: Display Git Revision Hash and Timestamp in Views?

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.

How to view remote Git revision on Heroku

If you've just pushed and want to make sure you're up-to-date, then you can just run git remote show heroku and you'll see output similar to this:

* remote heroku
Fetch URL: git@heroku.com:XXX.git
Push URL: git@heroku.com:XXX.git
HEAD branch: master
Remote branch:
master tracked
Local ref configured for 'git push':
master pushes to master (up to date)

That (up to date) at the end will be replaced by (fast forwardable) if it is not up to date.

Or, if you're wanting to see the full commit log for the heroku remote, the only way I know how is to check it out first. git checkout heroku/master will give you the current commit hash and commit comment: HEAD is now at <short commit hash>... <commit comment>, and git log will give you the rest of the story.

How to automatically pull the latest commit from a git submodule on Heroku?

https://help.heroku.com/RR520244/why-don-t-git-submodules-work-with-heroku-pipelines-review-apps-or-github-sync

git submodules are not compatible with Heroku, see provided link.


You should solve this differently.

Possible approaches:

1. Write a script that periodically pulls the data and add them to your project.

git subtree pull --prefix=data --squash --message="update covid data" https://github.com/CSSEGISandData/COVID-19.git master
git push origin HEAD

git subtrees are compatible with Heroku. For this approach you need to have a VPS and add the script to cron. Cron is a powerful tool that allows you to define scripts that are run at certain time intervals periodically

2. On app startup download the zip or tar.gz, unpack it and then serve the data. You will need to create a startup.sh script that does that and the final command would be starting your program. Something like:

curl -L https://api.github.com/repos/CSSEGISandData/COVID-19/tarball > data.tar.gz
cd data && rm -r ./*
tar -xzvf ../data.tar.gz
cd ..
python main.py

I recommend the second approach. First approach is recommended if you want the data versioned.

How to show the last commit date in a web page in Heroku?

I'm recording an alternate answer we've received from elsewhere as it's been very useful.

This solution is to use Git hooks to set a Heroku config variable, which can then be read by the app.

If you add this to .git/hooks/pre-push:

date=$(git log -1 --format="%cd")
heroku config:set UPDATED_DATE="$date" --app your-app-name

then every push will update the UPDATED_DATE environment variable on Heroku

you can format the date differently as needed

In Node you can access this variable with this:

process.env.UPDATED_DATE


Related Topics



Leave a reply



Submit