Ruby on rails 为什么Capistrano部署在assets:precompile没有错误时失败?

Ruby on rails 为什么Capistrano部署在assets:precompile没有错误时失败?,ruby-on-rails,capistrano,yarnpkg,webpacker,Ruby On Rails,Capistrano,Yarnpkg,Webpacker,我已经尝试了我能想到的一切,但我无法让部署工作 应用程序需要部署到由Alwaysdata托管并运行ruby 2.6.2的VPS,使用Capistrano进行部署。 这是一个Rails 6.0.2.2应用程序,使用webpack for JS和Sprocket for legacy脚本、所有图像和CSS # On local machine (MacOS) $ bundle exec cap production deploy 00:00 git:wrapper 00:01 git:check

我已经尝试了我能想到的一切,但我无法让部署工作

应用程序需要部署到由Alwaysdata托管并运行ruby 2.6.2的VPS,使用Capistrano进行部署。 这是一个Rails 6.0.2.2应用程序,使用webpack for JS和Sprocket for legacy脚本、所有图像和CSS

# On local machine (MacOS)

$ bundle exec cap production deploy
00:00 git:wrapper
00:01 git:check
00:03 deploy:check:directories
00:03 deploy:check:linked_dirs
00:03 deploy:check:make_linked_dirs
00:05 git:clone
00:06 git:update
00:08 git:create_release
00:10 deploy:set_current_revision
00:10 deploy:symlink:linked_files
00:12 deploy:symlink:linked_dirs
00:18 deploy:config:bundler
00:20 bundler:install
00:20 deploy:assets:precompile
#<Thread:0x00007fb35ba959f0@/Users/Goulven/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
/Users/Goulven/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/sshkit-1.21.0/lib/sshkit/command.rb:97:in `exit_status=': rake exit status: 1 (SSHKit::Command::Failed)
rake stdout: Nothing written
rake stderr: Nothing written

  INFO [3def24f1] Running bundle exec rake assets:precompile as vtcontrol@ssh-vtcontrol.alwaysdata.net

 DEBUG [3def24f1] Command: cd /home/www/app/releases/20200409174918 && ( export NODE_ENVIRONMENT="production" RAILS_ENV="production" RAILS_GROUPS="" ; bundle exec rake assets:precompile )
我想到的假设是:

  • 内存不足。这种情况主要发生在Digital Ocean上,提供商排除了这种可能性,而且无论如何,当sshing发送到服务器时,它不会编译
  • node
    vs
    nodejs
    的问题。这曾经困扰过我,但现在不再发生了,一开始是因为我只使用链轮并添加了
    mini_racer
    gem,它为ExecJS打包了节点,然后因为我使用webpacker而不是链轮包含引导,从而消除了对
    autoprefixer rails
    的依赖
  • Capistrano错误地解释了纱线/网页包装机故障警告。我将Bootstrap提取到它自己的app/javascripts/Bootstrap.js中,因为webpacker抱怨生成的js太大,所以可以排除这种情况。纱线抱怨未满足的对等依赖,但这些是开发依赖,我认为这不重要这可能是问题所在,但我如何测试它?
  • 还有什么我应该试试的吗?例如,使用网页包和链轮会导致生产冲突吗?它在开发中运行良好,生成的资产不应该相互覆盖

  • 以下是我的GEM文件的相关部分:

    # Gemfile
    
    # Use SCSS for stylesheets
    gem 'sass-rails', '>= 6'
    # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
    gem 'webpacker', '~> 4.0'
    
    group :production do
      # Used to load env vars from an .env file during deploy
      gem 'dotenv-rails'
    end
    
    group :development do
      gem 'capistrano', '~> 3.13.0', require: false
      gem 'capistrano-rails', '~> 1.4', require: false
      gem 'capistrano-bundler', '~> 1.6', require: false
      gem 'capistrano-rails-console', require: false
    end
    
    最后是我的
    Capfile
    deploy.rb
    的相关部分:

    # Capfile
    
    require 'capistrano/bundler'
    require 'capistrano/rails'
    require 'capistrano/rails/assets'
    require 'capistrano/rails/migrations'
    require 'capistrano/rails/console'
    require 'dotenv'
    Dotenv.load
    

    谢谢你的帮助

    好吧,在尝试了几天我能想到的一切之后,我尝试了一些我已经测试过的东西,但是这次用了正确的语法,我想,因为它现在可以工作了

    以下是我需要添加到Capistrano配置文件中的内容,以确保部署时纱线可用:

    deploy.rb中的
    #
    #添加或调整默认_env以将.npm包附加到$PATH:
    设置:默认环境{
    路径:'$HOME/.npm packages/bin/:$PATH',
    节点_环境:“生产”
    }
    

    说明:VPS用户可以“全局”安装像Thread这样的二进制文件(使用
    npm安装--global thread
    )。在引擎盖下,二进制文件安装在
    $HOME/.npm packages/bin
    中,该文件夹被添加到
    $PATH
    中,用于交互式/登录shell会话。由于Capistrano尽最大努力不接收此消息,我们必须在
    deploy.rb

    中强制输入更新的
    $PATH
    ,感谢您为我指明了正确的方向。我也犯了同样的错误,结果是内存问题。对于存在相同问题的任何人,请在编译期间使用
    free-m
    检查内存是否耗尽。如果内存用完,您将看到类似于
    -bash:fork:cannotallocate memory


    我正在使用Digitalocean,我的解决方案是添加交换空间:

    关于fsevents与Linux不兼容的警告,我在每次部署中都会看到这一点,但我的资产编译(链轮和webpacker)总是成功的。我不认为这是你的问题。谢谢@Leslongtingill,我想知道。显然,这是另一个问题,因为Capistrano的环境与普通SSH用户完全不同,这与VPS架构上安装工具的方式不符。在我使用的应用程序上,Capistrano使用我的开发机器的~/.SSH/config文件,与我通过SSH连接到服务器时所做的完全相同。至少对我来说,Capistrano SSH环境与我使用的相同。是什么让你认为它使用了不同的环境?由于我没有配置Cap的任何ssh选项,这似乎是默认行为。为了澄清,如果您在服务器上手动运行它,它会产生警告,但实际上它会预编译文件?因此,它可以通过基于终端的SSH工作,但不能通过capistrano?@2调用混乱是的,当我在生产服务器上手动运行命令时,一切正常,警告放在一边。但是,当我运行
    cap production deploy
    时,
    assets:precompile
    任务失败,并且除了上一个任务完成正常外,没有提供任何有用的信息。这很可能与主机提供商的特性不符…
    # Capfile
    
    require 'capistrano/bundler'
    require 'capistrano/rails'
    require 'capistrano/rails/assets'
    require 'capistrano/rails/migrations'
    require 'capistrano/rails/console'
    require 'dotenv'
    Dotenv.load
    
    # config/deploy.rb
    
    # Setup bundler
    # Unset capistrano/bundler default flags because Bundler has deprecated them
    set :bundle_flags,      '--quiet'
    set :bundle_path,       nil
    set :bundle_without,    nil
    before 'bundler:install', 'deploy:config:bundler'
    
    # Remove gems no longer used to reduce disk space used
    # This command requires loading capistrano/bundler in Capfile
    after 'deploy:published', 'bundler:clean'
    
    # Skip migration if files in db/migrate were not modified
    # This command requires loading capistrano/rails in Capfile
    set :conditionally_migrate, true
    
    # Rails app server manages the database
    set :migration_role, :app
    
    # Defaults to nil (no asset cleanup is performed)
    # If you use Rails 4+ and you'd like to clean up old assets after each deploy,
    # set this to the number of versions to keep
    set :keep_assets, 2