Web Analytics Made Easy - Statcounter

Rails - Resolve .env conflicts between development and Kamal deploy

Problem

We often use the gem Dotenv to handle environment variables on development machine or gem Foreman also automatic load ENV from .env file (Forceman - Per Process Environment Variables)

The problems is Kamal-deploy use .env too, for the deployment on target service. The command `kamal envify` will   build the .env file from env.erb file, and use that .env as input for other command

Solution

Use  gem Dotenv in your development repo, then create an .env.development or .env.development.local file to store ENV for development, don't use .env file

Rails: Gem dotenv v3.1
group :development, :test do
  gem 'dotenv', '~> 3.1'
end
Dotenv file loading priority

Then in application.rb, add an config to ignore .env file

resolve conflict .env between development env and Kamal deploy
# application.rb
module Launchpad
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 8.0
    # ....

    Dotenv::Rails.files.delete('.env') if defined?(Dotenv::Rails)
  end
end
Ignore .env content with Dotenv

Dotenv will load variable from other .env file and ignore all content from .env.