Skip to content

Easy Rails deployment with rsync

Starting a new Rails app? I find one of the most annoying thing with writing code is the initial deployment. I just want to get my little app online! The fastest way to deploy Rails (or any) code to production is probably rsync.

rsync is actually a tool used to keep files synchronized, by copying/deleting the files that changed. Turns out, it’s a pretty handy way to deploy code too!

 

Deployment steps:
(in your localhost, cd’d into your development branch)

$ cd my-rails-app

 

Compile your assets locally. You can choose to run this on your production server as well, but I prefer to do it here for small applications using a weak production server.

$ bundle exec rake assets:precompile

 

Run rsync to copy your local files over to your production machine

$ rsync -a --delete --stats --progress --exclude-from='./.rsync_exclude' ./ your@domain.com:~/your-rails-folder/

This will copy all files from ./ to the remote ~/your-rails-folder/

The --exclude-from flag is useful to exclude extra files your production server doesn’t need, ESPECIALLY the .bundle file

Here’s my .rsync_exclude:

// my-rails-app/.rsync_exclude
.bundle
tmp
log

Now, if you have new gems or migrations to run, ssh into your prod server:

ssh your@domain.com

 

And execute:

$ cd ~/your-rails-folder
// (if you use rvm, make sure to switch to the right rvm version)
$ rvm use ruby-2.2.2

 

Run bundle and rake

$ bundle install --without development test
$ rake db:migrate RAILS_ENV="production"

 

If you haven’t already precompiled your assets earlier in your local machine, do it now:

$ RAILS_ENV=production bundle exec rake assets:precompile

 

Finally, restart your webserver. Depending on what you’re using, this step could be different.

$ killall -USR1 dispatch.fcgi

 

Voila, forget installing Capistrano or writing complicated deployment scripts. This will get you up and running with a production server in no time.

Gist: https://gist.github.com/c177e377f044d1bb1c0fd678056224ae

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*