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?
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
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?
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.
Long time ago I did this for subversion:
in an initializer I put a global
SVN_REVISION = `svn info | ruby -ne 'puts $1 if $_ =~ /^Revision: ([0-9]*)/'`
and in the application layout:
<div id='site_footer'>
...
<% unless SVN_REVISION.blank? -%>
<p id='version'>
v<%= SVN_REVISION %>
</p>
<% end -%>
</div>
with git you could change the global to something like:
git log -1 | ruby -ne 'puts $1 if $_ =~ %r{^Date: (.+) \+\d+$}'
Now thinking about it, it may be better to:
I'm not sure, it's just an idea.