[ bin/setup ] [{"RAILS_ENV"=>"test"}, "bin/rails db:reset"] succeeded
[ bin/setup ] Dropping & recreating the test database
[ bin/setup ] Executing [{"RAILS_ENV"=>"production"}, "bin/rails db:reset"]
rails aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit` (ArgumentError)
raise ArgumentError, "Missing `secret_key_base` for '#{Rails.env}' environment, set this string with `bin/rails credentials:edit`"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/home/mustdos/Ruby Stuff/MUPSA/config/environment.rb:5:in `<main>'
Tasks: TOP => db:reset => db:drop => db:load_config => environment
(See full trace by running task with --trace)
[ bin/setup ] [{"RAILS_ENV"=>"production"}, "bin/rails db:reset"] failed
Hello all, I tried using dotenv with .env.testing .env.development and .env.production but I can't seem to get the .env.production running.
How should I solve this issue?
According to the Ruby on Rails documentation
In test and development applications get a secret_key_base derived from the app name. Other environments must use a random key present in config/credentials.yml.enc.
So as your error message suggests, we need to set a secret_key_base
.
securerandom
. You can run this to get a random string of 64 characters.require 'securerandom'
SecureRandom.hex(64)
EDITOR="code --wait" rails credentials:edit --environment production
That will open the decrypted state of the encrypted file that contains your secrets. It should be empty since you haven't set any before.
Set your secret_key_base
like so:
secret_key_base: xxxxxxxx
other_secret: 123
production.key
which the key that is used to decrypt your secrets encrypted file. That file contains sensible data and should not be public (not pushed to your Git repository). If you deploy your application somewhere, this should be set as an environment variable called RAILS_MASTER_KEY
. This should be added to your .gitignore
file.production.yml.enc
which is the secrets encrypted file.Now, you should be able to access your secrets like so in your code:
Rails.application.credentials.secret_key_base
Rails.application.credentials.other_secret
Rails.application.credentials.{secret_key}
production
environment operation such as rails console
or rails db:migrate
.PS: In your development
environment, it is actually using a secret_key_base
that is automatically generated in tmp/development_secret.txt
if you didn't set any with the steps above.
Reference is here.