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
We can use the git gem and create an initializer to set our application version using git describe
git gem to the development group.# Gemfile
# ...
group :development do
gem 'git'
# ...
end
Don't forget to run bundle.
# 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"