Learn how to make Heroku, Rails, and Dotenv Vault work together. This tutorial assumes you have already created a .env
file and synced it.
You can find a complete example repo here.
Set up your Rails app to work with Heroku.
Example shows using environment variable example
// Add 'dotenv-vault-rails' to Gemfile
gem 'dotenv-vault-rails'
Run bundle install.
$ bundle install
Require dotenv-vault.
Require it as early as possible in your Rails application. For a Rails application require dotenv-vault/load in application.rb
// config/application.rb
require 'dotenv-vault/load'
On your terminal at root of your project run npx dotenv-vault build to build your encrypted .env.vault file.
$ npx dotenv-vault build
Run npx dotenv-vault keys production.
$ npx dotenv-vault keys production
remote: Listing .env.vault decryption keys... done
dotenv://:key_1234@dotenv.org/vault/.env.vault?environment=production
Visit your Heroku Project’s Environment Variables under settings of your heroku application
Set DOTENV_KEY to the value returned in step 4.
That’s it!
Commit those changes safely to code and deploy to Heroku.
When the build runs, it will recognize the DOTENV_KEY
, decrypt the .env.vault file, and load the production environment variables to ENV
. If a DOTENV_KEY
is not set (like during development on your local machine) it will fall back to regular dotenv.