#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 -32 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. 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


16. 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/


17. 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!


18. Harry Dec 10, 2008 at 21:43

@Javan, you want #{previous_release}.


19. 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


20. Hrisya May 21, 2009 at 06:59

I don't underastand anything :(


21. Kirunya Jun 11, 2009 at 00:11

very very good


22. 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?


23. 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.


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

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


25. Nastena Feb 22, 2010 at 09:35

Thanks, this helped tremendously.


26. Artemidka Feb 24, 2010 at 07:06

Super/


27. louie vuitton Apr 10, 2010 at 05:15

Thank you


28. Daniela Jun 02, 2010 at 14:31

Fantastic, thanks. Have spent all day trying to work out how I could reconcile the fact that my apps have very large files and my shared hosting account has very small processor allocation. git seems to be a hungry beast and before total despair I found this railscast. hope this will resolve the issue!! thanks so much.
this is the most helpful thing i have come across for understanding capistrano. 8 mins instead of rereading the capistrano site and whatever else I could find 100 times.


29. Yuval Jun 21, 2010 at 02:06

@Sean - the shared folder gets created when you run cap deploy:setup. I'm not sure how the database.yml file got there though. If there isn't an automatic way to upload files in the shared folder, my guess is that ryan uploaded it there (via ftp or the like).


30. nike shox r4 Jun 23, 2010 at 02:43

good


31. Cheap clothes Jul 20, 2010 at 12:14

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


32. polo ralph lauren Jul 20, 2010 at 12:15

That assets trick was great, thanks for sharing Ryan


33. True Religion Jeans Jul 20, 2010 at 12:15

Happy people make people more happy


34. Buy Clothes online cheap Jul 20, 2010 at 12:16

 Buy Clothes online cheap


38. <a href="http://www.baidu.com">baidu</a> <a href="http://www.google.com">google</a> Aug 06, 2010 at 20:36

<a href="http://www.baidu.com">baidu</a>
<a href="http://www.google.com">google</a>


38. <a href="http://www.baidu.com">baidu</a> <a href="http://www.google.com">google</a> Aug 06, 2010 at 20:39

baidu google
[url=http://www.baidu.com]baidu[/url]
[url=http://www.google.com]google[/url]


39. Shirley Aug 11, 2010 at 03:02

I am a seller from www.kickswiki.com. i also really want to know how it is going on. Thanks!


40. free directory list Aug 11, 2010 at 22:31

you for sharing.Nice post


41. wariror Aug 18, 2010 at 21:51

thanks admin


42. Air Jordan Force 4 Aug 19, 2010 at 22:40

I've already started building in the separate asset uploads. Thanks for the tips. Thanks for this excellent and simple way to use presenter objects.


43. wholesale new era caps Aug 20, 2010 at 20:55

That was a great piece of information., I enjoyed reading it..,


44. medyum Aug 22, 2010 at 05:26

The information you provided was very useful. Because of your help, thank you


45. fivefingers online Aug 23, 2010 at 18:34

http://www.barefivefingers.com It is a wonderful experience. I am really enjoying it.


46. jimmy choo pumps Aug 25, 2010 at 00:32

it's a good application. Thanks


47. louis vuitton shoes Aug 26, 2010 at 21:04

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


48. Wholesale Electronics Aug 27, 2010 at 00:29

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


49. XIXI Aug 30, 2010 at 00:46

<p>Are you still confusing about choosing presents for your friends?Do you know where we can buy


50. xixi Aug 30, 2010 at 00:46

a href="http://www.linkreplica.com/replica-watches-hublot-watches-c-409_443.html">Replica Hublot</a>


51. snow boots Aug 30, 2010 at 21:22

in the case that you want to copy something from the last release to the new release?


52. discount babyliss hair straighteners Aug 31, 2010 at 00:12

http://www.haircareessence.com  Very great and informative post. Thanks a lot for sharing it


53. vuitton Aug 31, 2010 at 19:56

This website is awesome. I constantly come across something new & different right here. Thank you for that data.


54. levis belts Sep 01, 2010 at 20:58

Good article! Thank you so much for sharing this post.Your views truly open my mind.

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