Where do you store your Rails Application's version number?

前端 未结 8 1850
终归单人心
终归单人心 2021-01-30 05:41

We use the wonderful semantic versioning paradigm when versioning our rails app. One question I had was where is it best to store this number? I\'ve seen it stored in /l

8条回答
  •  渐次进展
    2021-01-30 05:55

    We can use the git gem and create an initializer to set our application version using git describe

    Add the git gem to the development group.

    # Gemfile
    # ...
    group :development do
      gem 'git'
      # ...
    end
    

    Don't forget to run bundle.

    Create a new initializer file.

    # config/initializers/version.rb
    
    if Rails.env.development?
      g = Git.open(Rails.root)
      version = g.describe
      puts "Setting application version to #{version}"
      File.write('config/VERSION', version)
    end
    
    module MyApp
      VERSION = File.read('config/VERSION').strip
    end
    

    Now we can access the version like so:

    ➤  rails c
    Setting application version to v2.1.3-7-gd5d8ea1
    Loading development environment (Rails 5.2.3)
    jruby-9.2.6.0 :001 > MyApp::VERSION
     => "v2.1.3-7-gd5d8ea1" 
    

提交回复
热议问题