3
votes

My Capistrano deployment does not set the RAILS_ENV variable when running the bundle command. I don't understand why.

My Gemfile is :

source 'https://rubygems.org'

ruby '2.1.3'

gem 'capistrano', '~> 3.2.1'
gem 'capistrano-rails', '~> 1.1.1'

My deploy.rb :

set :stage, :production
set :rails_env, 'production'

set :bundle_flags, '--deployment'
set :bundle_env_variables, { rails_env: "production" }

namespace :sphinx do
  desc "Index Sphinx"
  task :index do
    on roles(:app) do
      within release_path do
      execute :rake, "rake ts:index"
    end
  end
end

When running :

➜  ansible-sharetribe git:(master) ✗ bin/cap production sphinx:index --trace
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_rails_env
** Invoke sphinx:index (first_time)
** Execute sphinx:index
INFO[3ca70ab5] Running bundle exec rake rake ts:index on www.myapplication.com
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host www.myapplication.com: rake exit status: 1
rake stdout: Nothing written
rake stderr: Digest::Digest is deprecated; use Digest
rake aborted!
NameError: uninitialized constant Annotate

If I write :

execute :rake, "rake ts:index RAILS_ENV=production"

it works. Why is RAILS_ENV=production not set automatically?

2
Have you found the answer? Looks like I faced the same problem ... - KIR
I have the same problem. Ran across a lot of fixes, but none of them worked. - codenoob

2 Answers

1
votes

You can use this syntax in your task

execute :rake, "rake ts:index", "RAILS_ENV=#{fetch :rails_env}"
1
votes

I have the same problem. I ran rake secret and got "NameError: uninitialized constant Annotate" error. After i set RAILS_ENV=production, it worked like charm. I dont know why RAILS_ENV wasnt set but BTW thanks so much. Saved me a lot of time