Every Little Things Capistrano Does Is Magic

During all these years, Capistrano has been for me a magical gnome that I invoke by saying “cap deploy, my magical friend!” and then I close my eyes, sing a happy song in my head and when the gnome has finished his magic, I hit F5 to see if all went well. And of course, if it didn’t, I blame the damn elf.

I am proud of this introduction. I will take a sip of coffee and read it again, hold on. Yes this intro was good, I think I should write about magical little beings more often.

If you are like me, you never paid much attention to capistrano and the way it works. At the exception of the famous “cap deploy” command, you never really wanted to get involved in the process. For this reason you have copied and pasted the same old deployment recipe in all of your projects over the years, only changing some parameters like the application name and the emplacement of the git repository.

The fact that capistrano is doing magical things doesn’t really help in this regard. My goal in this post is to explain some of the not so obvious parts of capistrano. With minimal efforts we can have a better idea of what capistrano does and feel more in control when deploying an application.

Capistrano is magic

Like many things in the ruby world, there is a fair bit of magic involved in capistrano. For example, when you type “cap deploy”, there is a bunch of things that happen, but what things exactly? What tasks are being called? This is not obvious. First of all, “deploy” is not a task written in your own recipe file, it is a namespace located in the capistrano gem. Take a look:

[ruby]
namespace :deploy do
desc <<-DESC
Deploys your project. This calls both update' and restart’. Note that \
this will generally only work for applications that have already been deployed \
once. For a "cold" deploy, you’ll want to take a look at the `deploy:cold’ \
task, which handles the cold start specifically.
DESC
task :default do
update
restart
end
#…
[/ruby]

As you can see, the :default task is invoked when you type “cap deploy”, which in turns will call “update” and “restart”.

This is why you never got to see a call to the :restart task in your own recipe file. I know that during all these years there was a secret part of your brain that was¸wondering about this stuff, while the not-so-secret part really didn’t care.

Capistrano and Bundler

[ruby]
require ‘bundler/capistrano’
[/ruby]

This line adds some bundler magical stuff to your recipe. It will install your bundle (with the –deployment flag) when deploying, meaning that all your gems will be vendored to yourapp/shared/bundle.

Capistrano and the Asset Pipeline

[ruby]
load "deploy/assets"
[/ruby]

This line adds some magical stuff for compatibility with the Rails 3.1+ asset pipeline. More precisely, it will precompile your assets. This is an almost essential part when you use the asset pipeline in production. The other solution is to precompile locally and push the assets to your repository, not a good idea in my opinion. The sad part is that precompiling assets can slow down your deployment quite severly. If you use GIT, you might like this smart solution from Ben Curtis to skip assets precompilation.

Capistrano and RVM

[ruby]
$:.unshift(File.expand_path(‘./lib’, ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, ‘ruby-1.9.2-head@rails3.2′
set :rvm_type, :user
[/ruby]

This tells capistrano to switch to a specific ruby version + gemset when connected to the server. The gemset (i.e. @rails3.2) is the tricky part because it makes you think that, when capistrano will invoke bundle install, your gems will be installed in .rvm/gems/ruby-1.9.2-head@rails3.2/gems. This is not the case. Remember that we have just told bundler to install the gems in yourapp/shared/bundle/.

So why do we bother with the gemset anyway?

One reason is that the bundler gem itself will be invoked by capistrano on your server in the gemset specified in the rvm_ruby_string variable. It can be useful to specify a gemset if you want to use a specific version of bundler.

That's all folks!

  • Pingback: Every Little Things Capistrano Does Is Magic : Ruby Fleebie « turnings :: daniel berlinger()

  • Pingback: A Smattering of Selenium #82 « Official Selenium Blog()

  • http://shimetherubyist.com shime

    sir, your blog is awesome! keep up the good work.

  • http://www.rubyfleebie.com Frank

    Thanks Shime!

  • Pingback: Every Little Things Capistrano Does Is Magic : Ruby Fleebie | Nerdfront()

  • Patrick Costine

    Healthy hair, skin, and eyes are all contributory to selenium. Selenium works closely with antioxidants to help prevent an increase in free radicals in the system. It is also beneficial in regulating the thyroid and immune system. If you are feeling energized and healthy, you may have selenium to thank for it. Selenium produces antibodies, which basically forms a chain of protection in the body. This one feature of selenium is important because it means that you are protected from a variety of viral and toxic invaders that can result in serious illness. .

    Take a look at our new website too
    <,http://www.foodsupplementdigest.com/chia-seeds-side-effects/

  • http://wallinside.com/post.php?id=3955578 ceiling fan stores

    A comfortable relaxation in your bedroom or living room is all you get
    from Hunter fans. Many people have discovered that the living space they enjoy in
    their home should also include the outdoor areas.
    When you go with a bamboo ceiling fan, often you will find that the fan actually becomes the focal point in the
    room.

  • http://www2.ucy.ac.cy/~mariosh/test.php?a%5B%5D=Cold+Calling+Jobs+Tips+%28%3Ca+href%3Dhttp%3A%2F%2Fwiki.kemsu.ru%2Findex.php%3Ftitle%3D%25D0%25A3%25D1%2587%25D0%25B0%25D1%2581%25D1%2582%25D0%25BD%25D0 http://www2.ucy.ac.cy/

    I am in facct happy to read this webpage posts which consists
    of tons of usefhl facts, thanks for providing these kinds
    of information.

  • http://RUBYFLEEBIE Shareena

    Thank you for providing the useful information.