How to enter rails console on production via capistrano?

前端 未结 7 1515
借酒劲吻你
借酒劲吻你 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:55

    A simple Capistrano 3 solution may be:

    namespace :rails do
      desc "Run the console on a remote server."
      task :console do
        on roles(:app) do |h|
          execute_interactively "RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console", h.user
        end
      end
    
      def execute_interactively(command, user)
        info "Connecting with #{user}@#{host}"
        cmd = "ssh #{user}@#{host} -p 22 -t 'cd #{fetch(:deploy_to)}/current && #{command}'"
        exec cmd
      end
    end
    

    Then you can call it say, on staging, with: cap staging rails:console. Have fun!

提交回复
热议问题