ruby-on-railsruby-on-rails-3githubcapistranobuild-numbers

How to add build number and date from github in ruby code?


How can you automatically import the latest github commit number from github?

The goal would be to have that number visible on your webpage footer like SO does with date.

Whats the structure:

I have a production branch which is deployed using Capistrano. I want to show the latest github commit number, with the date when it was deployed.


Solution

  • Assuming you use gem settingslogic for app settings, put this in your initializers:

    git_log = `git log -1 --pretty="format:%H %ci"`
    if git_log =~ /^([\d\w]+?)\s(.+)$/
      Settings[:git_revision] = $1
      Settings[:git_update]   = $2.strip
    end
    

    You will have last git commit SHA in Settings.git_revision and commit date in Settings.git_update.

    Additionally you can get last tag:

    git_tag = `git describe --tags --abbrev=0`
    Settings[:git_tag] = git_tag.strip if git_tag
    

    It will be available in Settings.git_tag.

    Update:

    I released a small ruby gem git-revision. With it you can simply do:

    "commit: #{Git::Revision.commit} date: #{Git::Revision.date}"