【问题标题】:Deploy Rails 5.1 / Webpacker app with Capistrano使用 Capistrano 部署 Rails 5.1 / Webpacker 应用程序
【发布时间】:2017-10-10 05:33:39
【问题描述】:

我有一个 Ubuntu 服务器来部署我的 Rails 项目。在我的 Ubuntu 服务器中,我有 RVM。

现在我想用 Rails 5.1 和 webpacker 部署新项目。为了部署这个项目,我在我的 Ubuntu 服务器上安装了 NVM、npm 和 yarn。

在我的 Rails 5.1 / Webpacker 项目中,我有以下用于 capistrano 部署的 gem:

宝石文件

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

在 deploy.rb 中,我为 capistrano nvm 和 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, {}

我还在linked_dirs中添加了node_modules。

deploy.rb

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

当我在 assets:precompile 步骤中执行 cap deploy 时出现问题。接下来是错误日志。

终端日志

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/

提前致谢!

【问题讨论】:

    标签: ruby-on-rails ubuntu webpack capistrano ruby-on-rails-5.1


    【解决方案1】:

    对我有用的是确保 NVM 包含在 PATH 中。默认情况下,在 bash 终端中,NVM 会将其环境变量添加到 ~/.bashrc 的末尾,但该文件的大部分内容通常不会在非交互式终端(Capistrano 运行在其中)中执行。我发现this comment 很有帮助:

    将您的 NVM 源脚本放入您的 .bashrc 中,即使在非交互式 SSH 会话期间仍会对其进行评估。只需确保将声明放在顶部,在 case 语句之前:

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
    
    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    

    【讨论】:

    • 这很有帮助,谢谢。尽管在本地编译资产可能是一种很好的技术,如上所述,但这个答案是解决 capistrano 部署设置问题的一个很好的提示在远程服务器上使用 nvm,如问题中所述。
    【解决方案2】:

    首先 - 安装 Yarn

    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    sudo apt update && sudo apt install yarn
    

    第二次 - 安装 NodeJS(现在 14 是最新的):

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    【讨论】:

      【解决方案3】:

      我更喜欢在本地编译资产,然后使用 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'
      

      在您的 Capfile 中,您需要包含所有 capistrano 任务:

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

      这消除了在生产服务器上安装节点和纱线的需要。

      当然你需要在本地安装 node 和 yarn

      【讨论】:

      • 您好,您能告诉我如何使用此代码吗?我是 Capistrano 的新手,不知道该放在哪里。谢谢
      • @HoangDo 我用文件名更新了答案
      • 非常感谢!
      • 并非所有英雄都穿着斗篷。
      【解决方案4】:

      该错误几乎可以告诉您出了什么问题。在服务器上找不到 Yarn 或 Node。您的安装可能不正确。

      按照说明在此处安装两者:

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

      这里:

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

      然后确保您可以调用:

      yarn
      node
      

      在服务器上。如果没有,您可能需要将可执行文件的路径添加到 PATH 变量中

      【讨论】:

      • 我面临同样的问题......但解决方案对我不起作用:(
      猜你喜欢
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2013-09-29
      相关资源
      最近更新 更多