How to deploy a custom Gem using capistrano? - ruby-on-rails

I'm new to RoR. My gem does some background processing and loads a thin server so it can be checked from time to time. I need to put this on my web server using Capistrano.
Do people normally deploy gems to their servers or should the app be written in some other way?
Also is Capistrano the correct way to deploy gems?

Have you checked out Bundler before? That works very well with capistrano deployments and you can use to package your gems with your app on deployments.

Or you can use the gem Jeweler : https://github.com/technicalpickles/jeweler

I found out deploying a custom Gem is no different to any other project type. I just needed to add a new Capistrano task to my deploy.rb file so that the gem could get installed after the files get downloaded onto the server by Capsitrano. This is all I had to do.
desc "Install this gem"
task :setup_install, roles: :app do
run "cd #{release_path} && gem build zoe.gemspec"
run "cd #{release_path} && gem install YOUR-GEM-NAME.gem --quiet"
end
after "deploy:finalize_update", "deploy:setup_install"
desc "Uninstall this gem"
task :setup_uninstall, roles: :app do
run "gem uninstall -x YOUR-GEM-NAME"
end
before "deploy:setup_install", "deploy:setup_uninstall"

Related

How to use bundle with local gems during Mina deployment of Rails app?

I'm deploying a Rails app with Mina mina:deploy which clones from a git repo and Bundler installs the gems.
# /config/deploy.rb
# ...
task :deploy => :environment do
deploy do
invoke :'git:clone'
invoke :'bundle:install'
# ...
end
end
However, unlike when I bundle install manually, mina is installing each gem anew. With a healthy number of gems, this takes roughly 10 minutes to complete. How can I deploy while pointing bundler to use any locally available (already installed) gems where possible?
I've also tried replacing invoke :'bundle:install' with queue! "bundle install --local" with no change in behavior.
For that you need to use a local copy of the gems that you have without checking rubygems so after installing gems you run bundle package to create a cache of the gems used and instead of running bundle install you should run bundle install --local to use only the cached copy of gems without checking the rubygems.com .

Capistrano and sitemap_generator - cannot load such file -- capistrano/sitemap_generator

I have been using capistrano for a while and I also use sitemap_generator. But now I wanted to put the sitemap_generator into the deployment process. According to: Github sitemap generator I only need to require it in the capfile.
But once I run capistrano it cannot load the file.
Thx for help
The capistrano tasks included with the sitemap_generator gem are for capistrano 3.0.
Assuming you're probably on 2.x, you can create a task that calls the rake sitemap refresh task for you through bundler.
# recipes/sitemap.rb
namespace :sitemap do
desc "Generate sitemap.xml.gz"
task :generate, roles: :web do
run "cd #{deploy_to}/current && /usr/bin/env bundle exec rake sitemap:refresh RAILS_ENV=#{rails_env}"
end
after "deploy:restart", "sitemap:generate"
end
This example regenerates the sitemap after the deploy:restart task, but the task can be called directly.

capistrano won't run bundle install

I'm using chef and capistrano to create a server and later deploy my code. As I'm new to capistrano I took a tutorial from here and made some minor changes in order to get it going for me.
Whatever I do, I just cant get a bundler install to run on the remote server. Because of that there is no rake and the process stops at the assets:precompile. A github repo containing the code.
Am I trying something that I shouldn't do?
I fixed it with an additional task that explicitly runs bundle install. After that I still got an exception: stdout: Nothing written. I needed to add , raise_on_non_zero_exit: false
to be able to continue. Is anybody has a real test() to see if bundle install ran successfully I would be interested.
namespace :bundle do
desc "run bundle install and ensure all gem requirements are met"
task :install do
on roles(:app) do
execute "cd #{release_path} && RAILS_ENV=#{fetch(:stage)} bundle install --without=test", raise_on_non_zero_exit: false
end
end
end

Prevent whenenver gem from running --clear-crontab before gems installed

I am using capistrano, and the whenever gem, on a fresh deploy to a server without the whenever gem installed, capistrano attempts to run
whenever --clear-crontab
BEFORE the rake gems:install command has been run, its clear (from this) that this command runs after deploy_code but so does my command that installs the gems (below)..
after "deploy:update_code", "deploy:symlink_config"
deploy.task :symlink_config, :roles => :app do
# create a symlink to the database.yml file located in the shared_path
run "ln -nsf #{shared_path}/config/database.yml #{current_release}/config"
# install any missing gems
run "cd #{current_release} && sudo rake gems:install --trace RAILS_ENV=#{rails_env}"
# migrate the database
run "cd #{current_release} && rake db:migrate --trace RAILS_ENV=#{rails_env}"
end
Is there a way to order these tasks, because on a cold deploy I always get whenever: not found and have to manually install the whenever gem on the remote server
What I ended up doing is removing the require "whenever/capistrano" from the config\deploy.rb to avoid the "automatic" deploy. Instead I have added a task that executes the --clear-crontab and --update-crontab. This works as it will execute in the sequence I set it to.
I have based it off this post, which deals with a slightly different problem but has the same solution - not to use the "automatic" integration with capistrano.

How do I manage bundled gems in a capistrano deployment?

Currently, I'm just running the following:
after 'deploy:update_code', 'deploy:bundle'
namespace :deploy do
task :bundle do
run "cd #{release_path} && bundle install --deployment --without development test staging"
end
end
Now this works fine, but the bundler ends up installing all the gems to vendor/gems every time. While I understand the merit in isolating the gems to each release (as opposed to using the shared folder, which (?) might result in errors on rollback and such), surely there is a better way to do this that doesn't take as much time to deploy and saves on disk space.
Things have changed after the release of bundler 1.0
You don't need to create a custom task from deploy.
just use require 'bundler/capistrano'
take a look at this http://blog.josephholsten.com/2010/09/deploying-with-bundler-and-capistrano/ for more details

Resources