Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 app, How to get GIT version and update website?

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?

like image 302
Blankman Avatar asked Dec 01 '10 22:12

Blankman


3 Answers

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.

like image 111
rdlu Avatar answered Oct 28 '22 05:10

rdlu


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?

like image 41
sghael Avatar answered Oct 28 '22 04:10

sghael


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
like image 2
count0 Avatar answered Oct 28 '22 05:10

count0