LoginSignup
40
41

More than 5 years have passed since last update.

Capistrano3でデプロイしたリモートサーバのRailsアプリをローカルでconsoleする

Posted at

Capistrano3でUnicorn+Nginxな環境にRailsをデプロイする:初心者向けで、デプロイ自体はできるものの、デプロイ先でrails consoleしたいときってどうすりゃいいんだ…と思ったら、capistrano-rails-consoleという便利なgemがあった。

設定

Gemfile
# 環境変数管理
gem 'dotenv-rails'

gem 'rb-readline'

group :development, :test do
  # デプロイ周り
  gem 'capistrano', '~> 3.2.1'
  gem 'capistrano-rails'
  gem 'capistrano-rbenv'
  gem 'capistrano-bundler'
  gem 'capistrano3-unicorn'
  gem 'capistrano-rails-console' # 手元の環境からデプロイ先のconsoleを使う
end

これでひと通りのデプロイに必要なgemが揃うはず。

コンソール起動

command
$ bundle exec cap staging rails:console

これでstagingサーバのrails consoleが起動するはず。手元でリモートサーバのコンソールいじれるので、ステージングサーバの確認とか、テストデータ挿入とか超便利。

コンソールの起動に失敗する場合

`require': cannot load such file -- readline (LoadError)

こんな感じのエラーが出ていたら、readlineがないのでGemfileにrb-readlineがあるかどうかを確認する。

Looks like your app's ./bin/rails is a stub that was generated by Bundler.

In Rails 4, your app's bin/ directory contains executables that are versioned
like any other source code, rather than stubs that are generated on demand.

Here's how to upgrade:

  bundle config --delete bin    # Turn off Bundler's stub generator
  rake rails:update:bin         # Use the new Rails 4 executables
  git add bin                   # Add bin/ to source control

You may need to remove bin/ from your .gitignore as well.

When you install a gem whose executable you want to use in your app,
generate it and add it to source control:

  bundle binstubs some-gem-name
  git add bin/new-executable

こんな感じのエラーが出ていたら

config/deploy.rb
set :bundle_binstubs, nil # 追記

# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets bundle public/system public/assets}
# ↓に変更(binを抜く)
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets bundle public/system public/assets}

これでいけるはず。

40
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
41