Deploy Rails 5.1 / Webpacker app with Capistrano

牧云@^-^@ 提交于 2020-01-01 05:14:26

问题


I have an Ubuntu server to deploy my Rails projects. In my Ubuntu server I had RVM.

Now I want to deploy new projects with Rails 5.1 and webpacker. To deploy this projects, I've installed NVM, npm and yarn in my Ubuntu server.

In my Rails 5.1 / Webpacker project I have following gems for capistrano deployment:

Gemfile

group :development do
  gem 'capistrano-rails'
  gem 'capistrano-rvm'
  gem 'capistrano-passenger'
  gem 'capistrano-nvm', require: false
  gem 'capistrano-yarn'
end

In deploy.rb I've added some configurations for capistrano nvm and capistrano yarn.

deploy.rb

set :nvm_type, :user # or :system, depends on your nvm setup
set :nvm_node, 'v7.10.0'
set :nvm_map_bins, %w{node npm yarn}

set :yarn_target_path, -> { release_path.join('client') } #
set :yarn_flags, '--production --silent --no-progress'    # default
set :yarn_roles, :all                                     # default
set :yarn_env_variables, {}

Also I've added node_modules in linked_dirs.

deploy.rb

set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system node_modules client/node_modules}

The problem comes when I execute cap deploy in assets:precompile step. Next you have the error log.

terminal log

00:10 deploy:assets:precompile
  01 /usr/local/rvm/bin/rvm 2.4.1@project do bundle exec rake assets:precompile
  01 Yarn executable was not detected in the system.
  01 Download Yarn at https://yarnpkg.com/en/docs/install
  01 /home/deploy/rails/241/project/shared/bundle/ruby/2.4.0/bin/rake: No such file or directory - node
  01 Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host xxx.xxx.xxx.xxx: rake exit status: 1
rake stdout: Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
/home/deploy/rails/241/project/shared/bundle/ruby/2.4.0/bin/rake: No such file or directory - node
Node.js not installed. Please download and install Node.js 
https://nodejs.org/en/download/
rake stderr: Nothing written

SSHKit::Command::Failed: rake exit status: 1
rake stdout: Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
/home/deploy/rails/241/project/shared/bundle/ruby/2.4.0/bin/rake: No such file or directory - node
Node.js not installed. Please download and install Node.js 
https://nodejs.org/en/download/
rake stderr: Nothing written

Tasks: TOP => deploy:assets:precompile
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing on host xxx.xxx.xxx.xxx: rake exit status: 1
rake stdout: Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
/home/deploy/rails/241/project/shared/bundle/ruby/2.4.0/bin/rake: No such file or directory - node
Node.js not installed. Please download and install Node.js 
https://nodejs.org/en/download/
rake stderr: Nothing written



** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:


 DEBUG [016276ab]    * spring (2.0.1)

 DEBUG [016276ab]    * spring-watcher-listen (2.0.1)

 DEBUG [016276ab]    * web-console (3.5.0)

 DEBUG [016276ab]   Install missing gems with `bundle install`

 DEBUG [016276ab] Finished in 0.677 seconds with exit status 1 (failed).

  INFO [86e74b01] Running /usr/local/rvm/bin/rvm 2.4.1@project do bundle install --path /home/deploy/rails/241/project/shared/bundle --without development test --deployment --quiet on xxx.xxx.xxx.xxx

 DEBUG [86e74b01] Command: cd /home/deploy/rails/241/project/releases/20170511083021 && ( export NODE_VERSION="v7.10.0" ; /usr/local/rvm/bin/rvm 2.4.1@project do bundle install --path /home/deploy/rails/241/project/shared/bundle --without development test --deployment --quiet )

 DEBUG [86e74b01]   Warning, new version of rvm available '1.29.1', you are using older version '1.26.11'.

You can disable this warning with:    echo rvm_autoupdate_flag=0 >> ~/.rvmrc

You can enable  auto-update  with:    echo rvm_autoupdate_flag=2 >> ~/.rvmrc

  INFO [86e74b01] Finished in 3.209 seconds with exit status 0 (successful).

 DEBUG [4a428031] Running if test ! -d /home/deploy/rails/241/project/releases/20170511083021; then echo "Directory does not exist '/home/deploy/rails/241/project/releases/20170511083021'" 1>&2; false; fi on xxx.xxx.xxx.xxx

 DEBUG [4a428031] Command: if test ! -d /home/deploy/rails/241/project/releases/20170511083021; then echo "Directory does not exist '/home/deploy/rails/241/project/releases/20170511083021'" 1>&2; false; fi

 DEBUG [4a428031] Finished in 0.066 seconds with exit status 0 (successful).

  INFO [d225a8b5] Running /usr/local/rvm/bin/rvm 2.4.1@project do bundle exec rake assets:precompile on xxx.xxx.xxx.xxx

 DEBUG [d225a8b5] Command: cd /home/deploy/rails/241/project/releases/20170511083021 && ( export NODE_VERSION="v7.10.0" RAILS_ENV="production" ; /usr/local/rvm/bin/rvm 2.4.1@project do bundle exec rake assets:precompile )

 DEBUG [d225a8b5]   Yarn executable was not detected in the system.

Download Yarn at https://yarnpkg.com/en/docs/install

 DEBUG [d225a8b5]   /home/deploy/rails/241/project/shared/bundle/ruby/2.4.0/bin/rake: No such file or directory - node

 DEBUG [d225a8b5]   Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/

Thanks in advance!


回答1:


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




回答2:


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




回答3:


many thanks!!

i tested it manually and it is the soulution for me. unfortunately the script doesnt run

what may be?

00:12 assets:precompile
      01 ~/.rvm/bin/rvm default do bundle exec rake assets:precompile
      01 /Users/dev/.rvm/gems/ruby-2.5.7/bin/rake: fork: Resource temporarily unavailable
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Command::Failed: bundle exit status: 32768
bundle stdout: Nothing written
bundle stderr: /Users/dev/.rvm/gems/ruby-2.5.7/bin/rake: fork: Resource temporarily unavailable
/Users/dev/projects/rockstar-jobs/rockstar-rails/lib/capistrano/tasks/precompile.rake:9:in `block (4 levels) in <top (required)>'
/Users/dev/projects/rockstar-jobs/rockstar-rails/lib/capistrano/tasks/precompile.rake:8:in `block (3 levels) in <top (required)>'
/Users/dev/projects/rockstar-jobs/rockstar-rails/lib/capistrano/tasks/precompile.rake:7:in `block (2 levels) in <top (required)>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)
The deploy has failed with an error: bundle exit status: 32768
bundle stdout: Nothing written
bundle stderr: /Users/dev/.rvm/gems/ruby-2.5.7/bin/rake: fork: Resource temporarily unavailable


** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:


来源:https://stackoverflow.com/questions/43911610/deploy-rails-5-1-webpacker-app-with-capistrano

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