DEV Community

Abdelkader Boudih
Abdelkader Boudih

Posted on

Rails Version Management with Rails AppVersion

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
Enter fullscreen mode Exit fullscreen mode

Common Use Cases

Error Tracking:

Sentry.init do |config|
  config.release = Rails.application.version.to_s
  config.environment = Rails.application.env
end
Enter fullscreen mode Exit fullscreen mode

Cache Management:

def index
  Rails.cache.fetch("index-page-#{Rails.application.version.to_cache_key}") do
    render :index
  end
end
Enter fullscreen mode Exit fullscreen mode

Version Headers:

# config/app_version.yml
staging:
  middleware:
    enabled: true
    options:
      version_header: X-Staging-Version
      environment_header: X-Staging-Environment
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Version Management Options

  1. VERSION File (Recommended):
1.2.3
Enter fullscreen mode Exit fullscreen mode
  1. 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') %>
Enter fullscreen mode Exit fullscreen mode

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)"
Enter fullscreen mode Exit fullscreen mode

Environment Management:

Rails.application.env               # => "staging"
Rails.application.env.production?   # => false
Rails.application.env.staging?      # => true
Enter fullscreen mode Exit fullscreen mode

The gem is available at https://github.com/seuros/rails_app_version under the MIT License.

Top comments (0)