Bundle and rake commands are not recognized with Capistrano 3 - ruby-on-rails

I have installed Capistrano 3 and I'm not able to get my app to call bundle install and precompile my assets on deploy.
I've seen that I have to configure my environment for RVM there http://rvm.io/deployment/capistrano#environment
But I was wondering, I have a dev computer A, and a deployment computer B, which both have RVM user-installed.
Should I configure Capistrano to use RVM on my dev computer or on the deployment computer?

Does your Capfile have the following?
require 'capistrano/rails'
require 'capistrano/rvm'
capistrano/rails includes dependencies for bundler, assets and migrations.
https://github.com/capistrano/rails/blob/master/lib/capistrano/rails.rb
https://github.com/capistrano/rvm
Also make sure to read the readme on capistrano/rvm, as you need to have the correct capistrano/bundler version.

Related

Multiple Rails apps on different Ruby versions on the same server

In RubyMine I am able to select the target Ruby version for project and run it.
Now about a production.
We have two variants to build a host for Rails apps:
Apache + Phusion Passenger
Nginx + Unicorn
How to run on each of these configurations two Rails applications built for different Ruby versions?
Requirements: both apps should run on the same server, on different virtual hosts.
RVM is installed.
An easy way to select a different Ruby version when deploying apps is by using Capistrano for deployment. After capifying your application it is time to add some lines to your Capfile
Your Capfile should look something like this
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/rvm'
This way you load special RVM options into Capistrano so you can specify the ruby version to use.
To specify a specific Ruby version to use, you can add a line to your deploy.rb for a system wide deployment option or to your production.rb or staging.rb if you want different ruby version per server.
By adding the following line you can specify your desired Ruby version:
set :rvm_ruby_version, '2.0.0-p247'
After you have deployed your application you still have to specify which ruby version Passenger will use. You can specify this by editing the apache config file specific to your site or subdomain. When editing your Apache config file.
<VirtualHost *:80>
PassengerRuby /home/someuser/.rvm/wrappers/<ruby-version-here>/ruby
******
</VirtualHost>
You can see which ruby version you need by first executing rvm use <ruby version> and then executing which ruby which should give you the path to enter in your VirtualHost file.
Hope that works for you

Set environment to development in ruby on rails 4

How can I set my Rails environment to development?
According to this question: How do I set my rails 3 app to development mode?, you add ENV['RAILS_ENV'] = 'development' to config/environment.rb.
I did this, but when I try to bundle install, it still tries to install gems for 'production'. I've placed the environment variable line at the start, middle, and end of file.
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Grafly::Application.initialize!
ENV['RAILS_ENV'] = 'development'
This is normal. Bundler is a general-purpose dependency manager for Ruby. It has no idea that Rails exists. The group directives are exposing Bundler's groups feature, not a function of Rails.
If you don't instruct Bundler otherwise, it will install every gem from every group. It doesn't know what groups you do and don't want installed; it just knows that you defined some groups.
If you don't want to install all of your gems (or can't install all of your gems), you can skip production:
bundle install --without production
Similarly, you can skip development and testing gems when you deploy:
bundle install --without development test
(This is how, for example, Heroku and Cloud66 install only the gems you need for production.)

Rails 4, Capistrano 3.0.0, cannot load such file -- deploy

I just ran bundle update and capistrano got updated to 3.0.0 but now when I run cap deploy I get an error and can't figure out how to fix this. I have been updating my server every day without problem until this update.
cap aborted!
cannot load such file -- deploy
/home/mark/rails_apps/myapp/Capfile:1:in `load'
/home/mark/rails_apps/myapp/Capfile:1:in `<top (required)>'
capfile
load 'deploy'
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
I had to gem uninstall capistrano and selected version 3.0.0
(i.e. downgraded the gem to 2.x)
I had to run
gem uninstall capistrano
then update the gemfile with
gem 'capistrano', '~> 2.15'
and then run to reinstall the correct version again with
bundle update capistrano
Make sure you are using bundle exec (most likely you have multiple gem versions of capistrano)
i.e.
bundle exec cap -T
Instead of downgrading to Capistrano 2 use the new configuration from the current version.
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
See also this nice posting, which summarises the differences between Capistrano 2 and 3.
Add the related gems to your Gemfile
i.e. for
gem 'capistrano-bundler' # for capistrano/bundler
gem 'capistrano-rails' # for capistrano/rails/*
Do not downgrade to 2.x for this.
I had this problem today and pastullo's solution above fixed it except that I had to run gem uninstall capistrano (as markhorrocks answered) not bundle uninstall capistrano.
I also found this blog on bundler very useful: http://viget.com/extend/bundler-best-practices
Thanks for sharing this as it saved me heaps of time. x
The fastest way to fix this I have found is to backup the cap files (Capfile, config/deploy.rb, and config/deploy/*.rb) and then re capify (it's no longer called "capify"):
bundle exec cap install STAGES=staging,production
Then recreate your cap files from your backup. It will take you 5 minutes to do this and you'll be over the major Capistrano upgrade hump.
I used
bundle exec cap production deploy
instead of just cap production deploy
in my case I have changed my project ruby version. may be bundle also work here.
but I changed it to back what it was in previously.
ex:
rbenv local 2.4.1

Platform specific gems for autotest with bundler

In the rails project I'm working on I inserted support for rspec, cucumber and autotest with this Gemfile (partial)
gem 'rspec-rails'
gem 'cucumber-rails'
gem 'autotest-standalone'
gem 'autotest-rails-pure'
gem 'zentest-without-autotest'
however in order to run tests with autotest i need to execute bundle exec autotest otherwise it fails with this message
$ autotest
loading autotest/cucumber_rails_rspec_rspec2
Error loading Autotest style autotest/cucumber_rails_rspec_rspec2 (no such file to load -- autotest/cucumber_rails_rspec_rspec2). Aborting.
Now I'm developing on a Mac and I'd like to enable autotest-growl and autotest-fsevents gem, but if I insert those lines in my ~/.autotest
require 'autotest/growl'
require 'autotest/fsevent'
then I need to insert the corresponding gems in the Gemfile and everything works, but it breaks builds on my CI server (which is on Linux)
How to solve this without maintaining a different Gemfile for local and CI environments?
EDIT:
For the moment I solved with these lines in Gemfile
if RUBY_PLATFORM.downcase.include?("darwin") # I'm on Mac
gem 'autotest-fsevent'
gem 'autotest-growl'
end
It works both locally and on the CI server, I don't know if it mess something, for the moment it seems to work flawlessly.
Any cleaner way to do that is still welcome.
EDIT2:
I switched to groups solutions. While the previous monkeypatch works pretty well both in development and for continuous integration, it will gives you an error in production if you use capistrano bundler tasks for deployments or if you use bundle install --deployment option (which is advised in production)
When using the if RUBY_PLATFORM.downcase.include?("darwin") line you'll get this error on deploy.
# bundle install --deployment --without development test
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.
You have deleted from the Gemfile:
* autotest-fsevent
* autotest-growl
So my final solution to this problem is to include platform specific gems in a given group, say osx, and then in production and on CI server exclude it using bundle.
If you use capistrano to deploy put this in your config.rb
set :bundle_without, [:development, :test, :osx]
# capistrano bundler task
require "bundler/capistrano"
You might want to use groups in your gemfile, something like:
group :development do
gem "autotest-growl"
gem "autotest-fsevents"
end
and on the server you use: $ bundle install --without development
You can handle this by taking advantage of the different Gemfile environments (testing, development, production).
Your local box can be development while the CI server is your "production" environment.
With this in mind you can edit your Gemfile to use the appropriate gems depending on the environment.
Edit: Sorry, I think I scanned your post too quickly. But you can add your ~/.autotest to .gitignore so it wont be included on your CI server.

How does Bundler know what environment to use?

Here's probably a very "newbieish" question on Bundler, but I'm wondering how bundle install knows what environment to use or how to set it? Or do I even need to? My problem is that I've grouped my gems (in Gemfile) by environments and when deploying now I only want production gems to be installed.
At the top of the application.rb file you can see
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
When Rails is booted, Bundler automatically loads all the dependencies for the :default group and current environment.
Please note that when you run bundle install, Bundler resolves and install dependencies for all the environments, unless you specify a --without option
$ bundle install --without staging development test
In production, you might also want to add the --deployment flag.
More info about bundle install.
You can use the "group" option in the gem deependency declaration. Check this ASCIICast: http://asciicasts.com/episodes/201-bundler

Resources