How to enter rails console on production via capistrano?

前端 未结 7 1513
借酒劲吻你
借酒劲吻你 2021-02-02 11:25

I want to enter the rails console on production server from my local machine via capistrano. I found some gists, e.g. https://gist.github.com/813291 and when I enter console via

7条回答
  •  花落未央
    2021-02-02 11:56

    For Capistrano 3 you can add these lines in your config/deploy:

    namespace :rails do
      desc 'Open a rails console `cap [staging] rails:console [server_index default: 0]`'
      task :console do    
        server = roles(:app)[ARGV[2].to_i]
    
        puts "Opening a console on: #{server.hostname}...."
    
        cmd = "ssh #{server.user}@#{server.hostname} -t 'cd #{fetch(:deploy_to)}/current && RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console'"
    
        puts cmd
    
        exec cmd
      end
    end
    

    To open the first server in the servers list:

    cap [staging] rails:console 
    

    To open the second server in the servers list:

    cap [staging] rails:console 1 
    

    Reference: Opening a Rails console with Capistrano 3

    exec is needed to replace the current process, otherwise you will not be able to interact with the rails console.

提交回复
热议问题