I am deploying my rails 3 app using capistrano, and I want to get the git version (and date information) and update my website's footer with this.
How can I do this?
To people that doesn't use capistrano, a prettier one:
On config/initializers/git_info.rb:
GIT_BRANCH = `git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3`
GIT_COMMIT = `git log --pretty=format:'%h' -n 1`
GIT_COMMIT_TIMESTAMP = `git log --pretty=format:'%ct' -n 1`
The second one gives the sha in prettier form (ex: 4df21c0).
The third one returns the UNIX TIMESTAMP, that I format using Time/DateTime later.
Based on David's direction, I solved this by creating an initializer "git_info.rb". Place this file in your Rails initializers directory
The contents of the git_info.rb are:
GIT_BRANCH = `git status | sed -n 1p`.split(" ").last
GIT_COMMIT = `git log | sed -n 1p`.split(" ").last
Then in your footer, you can use this output (HAML syntax):
#rev_info
= "branch: #{GIT_BRANCH} | commit: #{GIT_COMMIT}"
You may want to set the font color of #rev_info the same as the background color, so the text is visible only when you highlight it with your cursor.
I just tried this, and while it works in development mode, it seems like the branch gets over-written with "deploy" post capistrano deploy. Capistrano must be creating it's own local branch called "deploy" on deploy?
Often one does not deploy into a git repository, but into a release which does not contain any repository information:
Capistrano stores a revision file with each successful deployment which is the git commit sha. This file is called REVISION
and can be found in the root of the deployment directory.
In config/initializers of your Rails app, create a file: deploy_version.rb with something like:
if File.exists?('REVISION')
APP_REVISION = `cat REVISION`
else
APP_REVISION = 'unknown'
end
Or if sure that outside of deployment there is always a git repository one can replace the last assignment with
if File.exists?('REVISION')
APP_REVISION = `cat REVISION`
else
APP_REVISION = `git describe --abbrev=6 --always --tags.`
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With