Skip to content

Support environment variables for credentials and upgrade Rails#331

Closed
yshmarov wants to merge 1 commit into
mainfrom
claude/upgrade-rails-credentials-BBGNy
Closed

Support environment variables for credentials and upgrade Rails#331
yshmarov wants to merge 1 commit into
mainfrom
claude/upgrade-rails-credentials-BBGNy

Conversation

@yshmarov
Copy link
Copy Markdown
Owner

@yshmarov yshmarov commented Feb 4, 2026

Summary

This PR introduces support for environment variables as an alternative to Rails encrypted credentials, implements a backport of Rails 8.2's combined credentials API (Rails.app.creds), and upgrades Rails to version 8.1.2.

Key Changes

  • Environment Variables Support: Added comprehensive .env.example with all supported configuration options organized by service (Stripe, OAuth providers, SMTP, S3, etc.). Environment variables now take precedence over encrypted credentials using double-underscore (__) as a separator for nested keys.

  • New Credentials Initializer: Created config/initializers/app_credentials.rb that backports Rails 8.2's Rails.app.creds API. This provides a unified interface that checks ENV variables first, then falls back to encrypted credentials.

  • Unified Credentials API: Replaced all instances of Rails.application.credentials.dig() with Rails.app.creds.option() throughout the codebase for consistency and to support both credential sources.

  • Rails Upgrade: Updated Gemfile to use Rails 8.1.2 (from 8.1.1).

  • Documentation Updates:

    • Enhanced .env.example with detailed comments explaining the ENV/credentials relationship
    • Updated config/storage.yml and lib/templates/rails/credentials/credentials.yml.tt with clarifying comments
    • Updated config/environments/production.rb comments to reference ENV variables

Implementation Details

The AppCreds class provides two methods:

  • option(*keys, default: nil): Returns ENV value, credential value, or default (non-raising)
  • require(*keys): Same as option but raises KeyError if value is missing from both sources

This approach maintains backward compatibility while enabling easier deployment via environment variables, which is particularly useful in containerized and cloud environments.

The backport will be automatically removed after upgrading to Rails 8.2+.

https://claude.ai/code/session_018vkGAjms65YpjwUidRYLkL

Backport Rails 8.2's unified credentials API (Rails.app.creds) that
checks ENV first, then falls back to encrypted credentials. This makes
.env and rails credentials interchangeable - developers can choose
either approach without code changes.

- Add config/initializers/app_credentials.rb polyfill (remove after Rails 8.2 upgrade)
- Replace all Rails.application.credentials.dig calls with Rails.app.creds.option
- Update .env.example to mirror all credential keys using double-underscore convention
- Update Gemfile to Rails ~> 8.1.2

https://claude.ai/code/session_018vkGAjms65YpjwUidRYLkL
@yshmarov
Copy link
Copy Markdown
Owner Author

yshmarov commented Feb 4, 2026

will wait for rails 8.2 release

@yshmarov yshmarov closed this Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants