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

前端 未结 4 1971
别那么骄傲
别那么骄傲 2021-02-08 17:59

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?

相关标签:
4条回答
  • 2021-02-08 18:50

    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
    
    0 讨论(0)
  • 2021-02-08 18:53

    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?

    0 讨论(0)
  • 2021-02-08 18:55

    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.

    0 讨论(0)
  • 2021-02-08 18:55

    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:

    • capistrano creates a configuration file (config/git_status.yml)
    • the initializers parses the configuration and sets variables

    I'm not sure, it's just an idea.

    0 讨论(0)
提交回复
热议问题