#133
Oct 27, 2008

Capistrano Tasks

Do you know how to make Capistrano tasks? See how to change default deployment behavior with custom tasks in this episode.
Download (11.1 MB, 8:49)
alternative download for iPod & Apple TV (10.1 MB, 8:49)

Resources

Directory Structure

  myapp/releases
  myapp/current -> releases/20081019001122
  myapp/shared


cap deploy

  deploy:update_code
  deploy:symlink_shared
  deploy:symlink
  deploy:restart
# deploy.rb
set :application, "railscasts.com"
role :app, application
role :web, application
role :db,  application, :primary => true

set :user, "deploy"
set :deploy_to, "/var/www/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git://github.com/ryanb/railscasts.git"
set :branch, "master"

namespace :deploy do
  desc "Tell Passenger to restart the app."
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end
  
  desc "Symlink shared configs and folders on each release."
  task :symlink_shared do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
    run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
  end
  
  desc "Sync the public/assets directory."
  task :assets do
    system "rsync -vr --exclude='.DS_Store' public/assets #{user}@#{application}:#{shared_path}/"
  end
end

after 'deploy:update_code', 'deploy:symlink_shared'

RSS Feed for Episode Comments 30 comments

1. Jose Oct 27, 2008 at 06:27

Great screencast ryan, thanks.

True that taskhelper...


2. Chris Saylor Oct 27, 2008 at 06:42

Capistrano also has built-in support for git submodules, which won't get checked out by default. This is handy if you are tracking plugins or rails as submodules. Turn it on with this setting:

set :git_enable_submodules, 1


3. Bill Oct 27, 2008 at 07:02

Excellent screencast! I was using an "after deploy" task to regen restart.txt and couldn't figure out how to get rid of the "missing pid" error. Moving it into the :deploy namespace was just what I needed.


4. Russ Jones Oct 27, 2008 at 08:13

That assets trick was great, thanks for sharing Ryan


5. Javan Oct 27, 2008 at 09:46

Is there an easy way to access something like previous_release_path in the case that you want to copy something from the last release to the new release?


6. Daryl Oct 27, 2008 at 10:13

You lost me on the last step... I snapped my fingers, but my code didn't auto-document. Am I missing a gem or something? ;-)

Thanks for this screencast.


7. Jacob Atzen Oct 27, 2008 at 14:51

@Javan: Usually you don't copy from release to release. Anything that needs to be shared between releases should be put in the shared directory and symlinked from the releases/current directories.


8. Michael Oct 27, 2008 at 16:05

Thanks for the screencast.

Maybe you can talk a little about webistrano next time. So you can use capistrano in a website so you can easily manage a bunch of websites and you can share symlinks.


9. Bharat Oct 27, 2008 at 17:21

Ryan,
Your screencasts are great. Like you, I had trouble understanding Capistrano at first. I found Ezra Zygmuntowicz's Deploying Rails Application book's Chapter 5 on Capistrano to be very helpful in getting the concepts straight, and after reading that last night had no trouble implementing a deploy script that I had been struggling with all week-end.
Bharat


10. Marcio Castilho Oct 27, 2008 at 20:17

One problem I am having is creating a Capistrano for Workling/Startling. I have created a task to start workling, everything executes properly but the process is never started.

Anybody has succefully created a Workling client start capistrano recipe ?


11. Ryan Bates Oct 28, 2008 at 08:09

@Marcio, it's hard to say what the problem is without knowing the error message or the code. I recommend making a thread on a forum or mailing list where you can go into these details. It may have to do with a missing gem or config file.


12. David Johnson Oct 28, 2008 at 10:08

Ryan,

Great screencast, one of many uses I have found for Capistrano is the ability to "automate" copying production database data to the development machine. Credit the code I re-used to http://marklunds.com/articles/one/339 although there are multiple other URLs with examples. It sure is useful to be able to automate repetitive tasks on the remote servers.


13. Forrest Zeisler Oct 30, 2008 at 10:11

This was a great post. Our app has a large number of tutorial flash videos in the public folder. I've already started building in the separate asset uploads. Thanks for the tips.


14. buggybunny Nov 02, 2008 at 11:18

Ryan,

although it has been said many times i have to say this one more time:

Your screencasts are absolutely outstanding and helped me A LOT.


15. David Trasbo Nov 03, 2008 at 23:42

Maybe you should consider some kind of moderation system for these comments. "cheap wow gold"? Hmm...


16. Scott Davis Nov 06, 2008 at 13:02

Ryan,

You should really check out passenger recipes for Capistrano its maturing nicely and is very functional.

http://github.com/jtrupiano/passenger-recipes/tree/master

Capistrano extensions which is also required

http://github.com/jtrupiano/capistrano-extensions/tree/master


17. Trevor Turk Nov 10, 2008 at 06:58

I did a little write-up about working with file assets and Capistrano deployments that people may be interested in:

http://almosteffortless.com/2007/03/25/working-with-attachment_fu/


18. Sean Dec 02, 2008 at 14:01

Ryan,

I don't see how /shared/config/database.yml gets created.

Did you create the /shared/config directory and place the database.yml file into that directory manually before running the cap script.

Thanks for the screencasts!


19. Harry Dec 10, 2008 at 21:43

@Javan, you want #{previous_release}.


20. Patrick Berkeley Dec 22, 2008 at 21:46

There's also a 'deploy:cleanup' task that is really useful for limiting the number of releases:

http://pastie.org/343030


21. Hrisya May 21, 2009 at 06:59

I don't underastand anything :(


22. Kirunya Jun 11, 2009 at 00:11

very very good


23. Nick Jun 18, 2009 at 12:53

I believe putting the rsync task in there will run it on the server machine, where you probably want to run that on your local machine to rsync files from the local machine to the server.

 Is there a way to run commands/tasks for deployment on the local machine?


24. jack Sep 03, 2009 at 12:58

Thanks, this helped tremendously.

Because of my total lack of knowledge about symlinks and Linux in general, I was a bit confused about something there.

In this example:

run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"

the latter path links *to* the former, right? I.e. you create a symlink *from* #{release_path}/public/assets *to* #{shared_path}/assets

So, anything being saved to /public/assets actually ends up in the shared/assets folder.

Please tell me if I'm wrong.


25. free seo tools Dec 18, 2009 at 21:47

well done .nice to see


26. javon Jan 25, 2010 at 22:32

well done .nice to see


27. free mp3 downloads legal Feb 07, 2010 at 04:03

Topics of interest but I could not understand the full ..

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source