Deploy Rails 5.1 / Webpacker app with Capistrano

匆匆过客 提交于 2019-12-03 14:24:54

The error tells you pretty much what's wrong. Neither Yarn or Node can be found on the server. Your installation might be incorrect.

Follow instructions to install both here:

https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

and here:

https://yarnpkg.com/lang/en/docs/install/#linux-tab

Then make sure you can call:

yarn
node

On the server. If not, you might need to add paths to executables into your PATH variable

I prefer to compile assets locally and then copy to the production servers with rsync:

# lib/capistrano/tasks/precompile.rake
namespace :assets do
  desc 'Precompile assets locally and then rsync to web servers'
  task :precompile do
    run_locally do
      with rails_env: stage_of_env do
        execute :bundle, 'exec rake assets:precompile'
      end
    end

    on roles(:web), in: :parallel do |server|
      run_locally do
        execute :rsync,
          "-a --delete ./public/packs/ #{fetch(:user)}@#{server.hostname}:#{shared_path}/public/packs/"
        execute :rsync,
          "-a --delete ./public/assets/ #{fetch(:user)}@#{server.hostname}:#{shared_path}/public/assets/"
      end
    end

    run_locally do
      execute :rm, '-rf public/assets'
      execute :rm, '-rf public/packs'
    end
  end
end

# config/deploy.rb
after 'deploy:updated', 'assets:precompile'

In your Capfile you need to include all the capistrano tasks:

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/**/*.rake').each { |r| import r }

This eliminates the need to install node and yarn on the production servers.

Of course you need node and yarn installed locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!