Deploy Rails 5.1 / Webpacker app with Capistrano - ruby-on-rails

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!

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

What worked for me was making sure NVM was included in the PATH. By default, in a bash terminal, NVM adds its environment variables to the end of ~/.bashrc, but much of that file is not usually executed in a non-interactive terminal (which Capistrano runs in). I found this comment helpful:
Put your NVM source script in your .bashrc which is still evaluated even during an non-interactive SSH session. Just make sure you place the declarations at the top, before the case statement:
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

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

First - Install 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
Second - Install NodeJS(now 14 is newest):
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

Related

Capistrano bower rails deployment issue

I have installed bower in the app server and I am able to run bundle exec rake bower:install in any release or in current directory. But when I'm trying the same while cap production deploy, the following error is repeated.
Command:
cd /home/deploy/apps/xxxx/releases/20150910064532 && ~/.rvm/bin/rvm default do bundle exec rake bower:install CI=true
Bower not found! You can install Bower using Node and npm:
DEBUG [68147ae5] $ npm install bower -g
DEBUG [68147ae5] For more info see http://bower.io/
config in deploy.rb
namespace :bower do
desc 'Install bower'
task :install do
on roles(:web) do
within release_path do
execute :rake, 'bower:install CI=true'
end
end
end
end
before 'deploy:compile_assets', 'bower:install'

Protractor tests failing on Codeship due to 405 API response

The protractor configuration and scripts work as expected both on our local development environment and in a continuous integration environment (similar to Codeship).
The project structure is the following (I'm describing the Codeship env. status below):
The AngularJS app requests data from the REST API build with the Ruby on rails app, using AJAX requests.
In our Codeship configuration we set up the rails app, migrate and seed the database so that all necessary data is available.
The AngularJS app is also configured correctly since the login page is rendered as expected (I'm doing a print screen, and the main page is loaded correctly).
The usename and password are filled in by protractor using valid credentials (available in the mysql DB).
But when clicking the 'Log In' button the returned response from the AJAX call is a 405 Method Not Allowed.
As we never encountered this response in our other environments we believe it has something to do with the specific codeship setup.
Any thoughts why the API would return 405 only on Codeship?
The setup is below:
rvm use 2.2.0 --install
cp config/database.codeship.yml config/database.yml
bundle install
export RAILS_ENV="test"
bundle exec rake db:drop RAILS_ENV=test
bundle exec rake db:create RAILS_ENV=test
bundle exec rake db:test:prepare
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rake seed:migrate RAILS_ENV=test
cd frontend && npm install && cd ..
npm install -g grunt-cli
npm install -g http-server
cd frontend && npm install bower && cd ..
cd frontend && bower install && cd ..
cd frontend && grunt build && cd ..
cd frontend && webdriver-manager update --standalone && cd ..
export RAILS_ENV="development"
rake db:structure:load
rake seed:migrate
http-server public -a 127.0.0.1 -p 9000 > /dev/null &
http-server app > /dev/null &
bundle exec rake
cd frontend && grunt test && cd ..
Here is the part of the screenshot that shows the API response:
In the end, the solution for us was to use protractor-rails
- basically using the base url of the rails server instead of trying to run the app on a different URL through grunt. Our setup now looks like this:
rvm use 2.2.0 --install
cp config/database.codeship.yml config/database.yml
bundle install
# protractor tests
export RAILS_ENV="development"
rake db:structure:load
rake seed:migrate
npm install
webdriver-manager update
bundle exec rake protractor:init RAILS_ENV=development
bundle exec rake protractor:spec RAILS_ENV=development
# rails tests
bundle exec rake db:test:prepare
bundle exec rake db:migrate RAILS_ENV=test
bundle exec rake seed:migrate RAILS_ENV=test
bundle exec rake

Bundler fails for deploying using elastic beanstalk

I am getting following error when trying to deploy a rails 4.2.1 app on ec2 using elastic beanstalk
on't run Bundler as root. Bundler can ask for sudo if it is needed, and
installing your bundle as root will break this application for all non-root
users on this machine.
Fetching source index from https://rubygems.org/
Fetching git#github.com:bokmann/font-awesome-rails.git
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Retrying git clone 'git#github.com:bokmann/font-awesome-rails.git' "/var/app/ondeck/vendor/bundle/ruby/2.0/cache/bundler/git/font-awesome-rails-aa9211906101215f2656ef38ba0c26146ba4c6bc" --bare --no-hardlinks --quiet due to error (2/3): Bundler::Source::Git::GitCommandError Git error: command `git clone 'git#github.com:bokmann/font-awesome-rails.git' "/var/app/ondeck/vendor/bundle/ruby/2.0/cache/bundler/git/font-awesome-rails-aa9211906101215f2656ef38ba0c26146ba4c6bc" --bare --no-hardlinks --quiet` in directory /var/app/ondeck has failed.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Git error: command `git clone 'git#github.com:bokmann/font-awesome-rails.git'
"/var/app/ondeck/vendor/bundle/ruby/2.0/cache/bundler/git/font-awesome-rails-aa9211906101215f2656ef38ba0c26146ba4c6bc"
--bare --no-hardlinks --quiet` in directory /var/app/ondeck has failed.
2014-07-13 15:16:48,672 [ERROR] (4743 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh failed with returncode 11
and .ebextensions/ruby.config looks like this
# Install git in order to be able to bundle gems from git
packages:
yum:
git: []
patch: []
commands:
# Run rake with bundle exec to be sure you get the right version
add_bundle_exec:
test: test ! -f /opt/elasticbeanstalk/support/.post-provisioning-complete
cwd: /opt/elasticbeanstalk/hooks/appdeploy/pre
command: perl -pi -e 's/(rake)/bundle exec $1/' 11_asset_compilation.sh 12_db_migration.sh
# Bundle with --deployment as recommended by bundler docs
# cf. http://gembundler.com/v1.2/rationale.html under Deploying Your Application
add_deployment_flag:
test: test ! -f /opt/elasticbeanstalk/support/.post-provisioning-complete
cwd: /opt/elasticbeanstalk/hooks/appdeploy/pre
command: perl -pi -e 's/(bundle install)/$1 --deployment/' 10_bundle_install.sh
# Vendor gems to a persistent directory for speedy subsequent bundling
make_vendor_bundle_dir:
test: test ! -f /opt/elasticbeanstalk/support/.post-provisioning-complete
command: mkdir /var/app/support/vendor_bundle
# Store the location of vendored gems in a handy env var
set_vendor_bundle_var:
test: test ! -f /opt/elasticbeanstalk/support/.post-provisioning-complete
cwd: /opt/elasticbeanstalk/support
command: sed -i '12iexport
EB_CONFIG_APP_VENDOR_BUNDLE=$EB_CONFIG_APP_SUPPORT/vendor_bundle' envvars
# The --deployment flag tells bundler to install gems to vendor/bundle/, so
# symlink that to the persistent directory
symlink_vendor_bundle:
test: test ! -f /opt/elasticbeanstalk/support/.post-provisioning-complete
cwd: /opt/elasticbeanstalk/hooks/appdeploy/pre
command: sed -i 's/\(^cd $EB_CONFIG_APP_ONDECK\)/\1\nln -s $EB_CONFIG_APP_VENDOR_BUNDLE .\/vendor\/bundle/' 10_bundle_install.sh
# Don't run the above commands again on this instance
# cf. http://stackoverflow.com/a/16846429/283398
z_write_post_provisioning_complete_file:
cwd: /opt/elasticbeanstalk/support
command: touch .post-provisioning-complete%
I am using a 64 bit Amazon Linux server and trying to install using ruby-2.0.0 and passenger
I suspect this is failing because in your ebextensions you are running bundle install --deployment. Is there a reason you want to use the --deployment flag. Take a look at this answer: https://stackoverflow.com/a/3681411/161628.
If your usecase does not need the --deployment flag, I would suggest don't use it.
You can locally package your app dependencies using vendor/cache using the approach documented in this blog.
Can you update the question with the contents of your Gemfile. Are you are using git# URLs in your Gemfile? You might want to use URLs like "git://github.com:bokmann/font-awesome-rails.git" in case you have "git#github.com:bokmann/font-awesome-rails.git" in your Gemfile.
Take a look at this answer.

Still getting gem not found with capistrano

I've been trying to configure my deployment with capistrano for a few weeks now, and it still is not working properly. Every time I deploy to my server I get an error saying a gem is missing, like:
DEBUG [a0e618f0] /home/ec2-user/.rvm/gems/ruby-2.0.0-p247/gems/bundler-1.3.5/lib/bundler/spec_set.rb:92:in `block in materialize'
DEBUG [a0e618f0] :
DEBUG [a0e618f0] Could not find ansi-1.4.3 in any of the sources
DEBUG [a0e618f0] (
DEBUG [a0e618f0] Bundler::GemNotFound
DEBUG [a0e618f0] )
I know how to fix this, simply install the "ansi" gem, but there is a much bigger problem here: why didn't bundler work properly with capistrano??
When I look higher in the output I see:
INFO [1ee9a88e] Running ~/.rvm/bin/rvm ruby-2.0.0-p247 do bundle --gemfile /var/www/html/SparkMyInterest/releases/20131122204608/Gemfile --path /var/www/html/SparkMyInterest/shared/bundle --deployment --quiet --binstubs /var/www/html/SparkMyInterest/shared/bin --without development test on 54.200.196.1
DEBUG [1ee9a88e] Command: cd /var/www/html/SparkMyInterest/releases/20131122204608 && ( RAILS_ENV=production ~/.rvm/bin/rvm ruby-2.0.0-p247 do bundle --gemfile /var/www/html/SparkMyInterest/releases/20131122204608/Gemfile --path /var/www/html/SparkMyInterest/shared/bundle --deployment --quiet --binstubs /var/www/html/SparkMyInterest/shared/bin --without development test )
INFO [1ee9a88e] Finished in 20.132 seconds with exit status 0 (successful).
DEBUG [7d1a9e40] Running if test ! -d /var/www/html/SparkMyInterest/releases/20131122204608; then echo "Directory does not exist '/var/www/html/SparkMyInterest/releases/20131122204608'" 1>&2; false; fi on 54.200.196.1
DEBUG [7d1a9e40] Command: if test ! -d /var/www/html/SparkMyInterest/releases/20131122204608; then echo "Directory does not exist '/var/www/html/SparkMyInterest/releases/20131122204608'" 1>&2; false; fi
DEBUG [7d1a9e40] Finished in 1.291 seconds with exit status 0 (successful).
INFO [a0e618f0] Running ~/.rvm/bin/rvm ruby-2.0.0-p247 do rake assets:precompile on 54.200.196.1
DEBUG [a0e618f0] Command: cd /var/www/html/SparkMyInterest/releases/20131122204608 && ( RAILS_ENV=production ~/.rvm/bin/rvm ruby-2.0.0-p247 do rake assets:precompile )
DEBUG [a0e618f0] /home/ec2-user/.rvm/gems/ruby-2.0.0-p247/gems/bundler-1.3.5/lib/bundler/spec_set.rb:92:in `block in materialize'
DEBUG [a0e618f0] :
DEBUG [a0e618f0] Could not find ansi-1.4.3 in any of the sources
DEBUG [a0e618f0] (
DEBUG [a0e618f0] Bundler::GemNotFound
DEBUG [a0e618f0] )
Clearly it looks like bundler was run (and successful!). What gives?
Here's my cap file:
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
And in my production.rb (I'm deploying with production) at the top I have:
set :stage, :production
set :rails_env, "production"
Any ideas why bundler isn't working? And/ or why it never seems to install all the required gems?
Thank you a million
I've worked through similar issues with rvm involved (required rvm/capistrano) by providing some additional info to capitrano
set :bundle_cmd, '/path/to/project/rvm/bundle'
set :default_shell, :bash
set :rvm_type, :system
set :rvm_ruby_string, release_path
This code was in my stage-specific deploy (e.g., deploy/production.rb). The issue that I had to work around was that the bundle was installed but to the wrong place; could be what you're facing here as well.
FWIW, rvm/capsitrano also let me specify a shell to use when running certain commands within the deploy script. For example, I was using the whenever gem to update my crontab and needed the "rvm_shell" (my specific ruby with gemset) to make this happen from capistrano. The line looks like this:
run "cd #{release_path} && bundle exec whenever -w -s environment=production", shell: fetch(:rvm_shell)

Rails capistrano deployment cannot find rake

I am deploying my 1st Rails app using capistrano, unicorn, rbenv, nginx, linode, ubuntu 12.04. When I run
bin/cap deploy:cold
in my app root, I get the following error back:
* 2012-10-31 01:19:36 executing `bundle:install'
* executing "cd /home/mr_deployer/apps/prjct_mngr/releases/20121031001933 && bundle install --gemfile /home/mr_deployer/apps/prjct_mngr/releases/20121031001933/Gemfile --path /home/mr_deployer/apps/prjct_mngr/shared/bundle --deployment --quiet --without development test"
servers: ["xxxxxxxxxxxxx"]
[xxxxxxxxxxxxx] executing command
** [out :: xxxxxxxxxxxxx] Could not find rake-0.9.2.2 in any of the sources
** [out :: xxxxxxxxxxxxx] Run `bundle install` to install missing gems.
command finished in 1046ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/mr_deployer/apps/prjct_mngr/releases/20121031001933; true"
servers: ["xxxxxxxxxxxxx"]
[xxxxxxxxxxxxx] executing command
command finished in 625ms
failed: "sh -c 'cd /home/mr_deployer/apps/prjct_mngr/releases/20121031001933 && bundle install --gemfile /home/mr_deployer/apps/prjct_mngr/releases/20121031001933/Gemfile --path /home/mr_deployer/apps/prjct_mngr/shared/bundle --deployment --quiet --without development test'" on xxxxxxxxxxxxx
I have run bundle install --path vendor/bundle on my development machine, and gem rake is installed both on dev machine and on linode vps. Why cant it find rake?
UPDATE:
I have tried adding path of my rake gem on linode to both my bashrc on linode and to :default_envoronment in deploy.rb file. Still getting the same error...
Try to specify your path in capistrano receipe, for example:
default_environment["PATH"] = "/usr/local/bin:/usr/bin:/usr/local/rvm/bin/:/var/lib/gems/1.9.1/bin"
If you don't know how looks your path, connect to your server via ssh and run command
echo $PATH
Capistrano tries to run command cd /home/mr_deployer/apps/prjct_mngr/releases/20121031001933 && bundle install --gemfile /home/mr_deployer/apps/prjct_mngr/releases/20121031001933/Gemfile --path /home/mr_deployer/apps/prjct_mngr/shared/bundle --deployment --quiet --without development test on remote machine where you service is deployed to.
Looks like remote node doesn't have rake installed that bundle trying to use.
Maybe you should check that all necessary gems are installed on remote machine.
This error is not indicating that rake is not installed on the machine you are deploying to. This error means that Bundler, when it tried to install your gems, was unable to find Rake version 0.9.2.2 in the sources listed in your Gemfile. Does your Gemfile contain a line like source "http://rubygems.org"?

Resources