CJG
CJG

Reputation: 558

Capistrano Not recognizing secret_key_base in Rails production.enc

I'm using Capistrano to deploy my Rails 7.0.2 app with Ruby 3.1.1, when I run cap production deploy and then when Capistrano runs:

deploy:assets:precompile
$HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile

I receive the following error :

ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`

deploy.rb:

# config valid for current version and patch releases of Capistrano
lock '~> 3.17.0'

set :application, 'app'
set :repo_url, '[email protected]:foo/app.git'

# Deploy to the user's home directory
set :deploy_to, "/#{fetch :application}"

append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'

# Only keep the last 5 re

The problem is that I already have the secret_key_base defined in my productions encrypted credentials file so I should not be getting this error. I also tried putting the secret_key_base as a .rbenv-vars file and still got the same error. How do I properly use a secret_key_base in a production environment with Rails and Capistrano? Thanks again!

Upvotes: 6

Views: 2698

Answers (2)

Leonardo Vásquez
Leonardo Vásquez

Reputation: 69

Can you try adding to the deploy.rb file:

append :linked_files, 'config/credentials/production.key',

And copy the production.key (the one that is in charge of decrypting the production credentials) to the shared folder in your server, something like /var/www/{proyect_name}/shared/config/credentials/production.key you can leave the production.yml.enc in your GitHub.

And check that you have set the secret_key_base in your production secret keys with EDITOR=nano rails credentials:edit -e production

Upvotes: 6

CJG
CJG

Reputation: 558

Added the following to config/environments/production:

config.require_master_key = true

run EDITOR=vi rails credentials:edit --environment=production Save the production key generated to your server @ app/config/credentials/production.key

Changed set :deploy_to, "/#{fetch :application}" to set :deploy_to, "~/#{fetch :application}"

Upvotes: 1

Related Questions