Gou*_*ven 3 capistrano ruby-on-rails yarnpkg webpacker
我已经尝试了我能想到的一切,但我无法让部署工作。
该应用程序需要部署到由Alwaysdata托管并运行ruby 2.6.2的VPS,使用Capistrano进行部署。这是一个 Rails 6.0.2.2 应用程序,使用 webpack 处理 JS,使用 sprocket 处理遗留脚本、所有图像和 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 )
Run Code Online (Sandbox Code Playgroud)
在本地或服务器上运行最后一个命令不会产生任何错误,只会产生警告:
# On production server:
$ cd /home/www/app/releases/20200409174918 && ( export NODE_ENVIRONMENT="production" RAILS_ENV="production" RAILS_GROUPS="" ; bundle exec rake assets:precompile )
yarn install v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.12: The platform "linux" is incompatible with this module.
info "fsevents@1.2.12" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > webpack-dev-server@3.10.3" has unmet peer dependency "webpack@^4.0.0 || ^5.0.0".
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has unmet peer dependency "webpack@^4.0.0".
success Already up-to-date.
Done in 0.62s.
Run Code Online (Sandbox Code Playgroud)
我想到的假设:
node与vs 的问题nodejs。这曾经让我烦恼,但它不再发生了,首先因为我只使用了 sprockets 并添加了 gem mini_racer,它为 ExecJS 打包节点,然后因为我使用 webpacker 而不是 Sprockets 包含了 Bootstrap,从而消除了autoprefixer-rails对在旧版本的nodejs 与node 一起存在。以下是我的 Gemfile 的相关部分:
# 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
Run Code Online (Sandbox Code Playgroud)
最后是我的相关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
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助!
好吧,经过几天的尝试,我能想到的一切,我尝试了一些我已经测试过的东西,但这次使用了正确的语法,我想,因为它现在可以工作了。
以下是我需要添加到 Capistrano 的配置文件中以确保 Yarn 在部署时可用的内容:
# In deploy.rb
# Add or adjust default_env to append .npm-packages to $PATH:
set :default_env, {
PATH: '$HOME/.npm-packages/bin/:$PATH',
NODE_ENVIRONMENT: 'production'
}
Run Code Online (Sandbox Code Playgroud)
说明:VPS 用户可以“全局”安装 Yarn 等二进制文件(使用npm install --global yarn)。在后台,二进制文件安装在 中$HOME/.npm-packages/bin,并且该文件夹被添加到$PATH交互式/登录 shell 会话中。由于 Capistrano 尽最大努力不接收此信息,因此我们必须强制提供更新$PATH的deploy.rb.