Rails AppVersion provides a standard way to handle version and environment information in Rails applications. It eliminates the need for custom version management solutions while providing useful conventions for error tracking, caching, and deployment verification.
Key Features
- Version information through
Rails.application.version
- Environment management via
Rails.application.env
- Version-aware response headers
- Cache key generation
- Console environment information
Basic Setup
# Gemfile gem "rails_app_version" # Terminal bundle install rails app:version:config echo "1.0.0" > VERSION
Common Use Cases
Error Tracking:
Sentry.init do |config| config.release = Rails.application.version.to_s config.environment = Rails.application.env end
Cache Management:
def index Rails.cache.fetch("index-page-#{Rails.application.version.to_cache_key}") do render :index end end
Version Headers:
# config/app_version.yml staging: middleware: enabled: true options: version_header: X-Staging-Version environment_header: X-Staging-Environment
Testing:
class VersionTest < ActiveSupport::TestCase test "version information" do assert_equal "1.2.3", Rails.application.version.to_s assert_equal "1-2-3", Rails.application.version.to_cache_key end end
Version Management Options
- VERSION File (Recommended):
1.2.3
- YAML Configuration:
shared: version: <%= Rails.root.join('VERSION').read.strip rescue '0.0.0' %> revision: <%= Rails.root.join('REVISION').read.strip rescue (`git rev-parse HEAD`.strip rescue '0') %>
Accessing Version Details:
Rails.application.version.major # => 1 Rails.application.version.minor # => 2 Rails.application.version.patch # => 3 Rails.application.version.full # => "1.2.3 (abc123de)"
Environment Management:
Rails.application.env # => "staging" Rails.application.env.production? # => false Rails.application.env.staging? # => true
The gem is available at https://github.com/seuros/rails_app_version under the MIT License.
Top comments (0)