I’ve blogged before about deploying Rails applications. Normally I opt for using capistrano, as it provides all the features I need and is pretty easy to customize if needed.
One of my previous strategies was to use capistrano to checkout a branch on a remote server, and
git fetch that branch upon a new deployment.
The problem with capistrano, however, is that it can be quite slow from time to time. It also has a lot of features I never or only rarely use, like multi-host deployments.
A few days ago someone pointed out git-deploy to me. I tried it out and it is fantastic. Let me explain that.
What could be the easiest way to deploy an app?
- SSH to the server
- Fetch code changes
Yes, you could write a script to do all that, but there’s an even easier way: git hooks.
Git hooks are triggered after certain events on a git repository. In this case, the flow becomes:
- Push code to server
In the git hooks, we still need to handle:
Now, here comes
git-deploy. What it does is add a few scripts to your project. Setup is rather easy:
# Install the gem, no need for Gemfile. gem install git-deploy # Add a git remote to your server git remote add production "firstname.lastname@example.org:/home/myapp/app" # Setup deployments git deploy setup -r production # Generate deploy scripts (locally) git deploy init
After this, you should find the following files in
The filenames are self explanatory, but here’s a quick run through:
This file is run after you’ve pushed your code to the remote server. It gathers some information about your changes, sets up the environment and kicks of the other two scripts.
Before you can restart your server, you will probably need to update some things. By default git-deploy will check if it needs to run migrations, perform a
bundle install and precompile your assets.
Does exactly what it says, it will restart your Rails app. By default it’s configured for Passenger, so it simply touches
Server setup, RVM and Sidekiq
What follows is a step-by-step guide of how I setup a new server. In this case for a project using ruby-2.0 with RVM and Sidekiq for background processing.
0. Set your RAILS_ENV
I always declare
/etc/environment. This file gets loaded everywhere and saves you the headache of adding this env variable with every rails command you execute later.
I also set my editor here, so I’m not surprised by nano when doing
RAILS_ENV=production RACK_ENV=production EDITOR=vi
1. Create a user for the app
2. Add user to sudoers
$ sudo vi /etc/sudoers # Add the following line: myapp ALL=(ALL) NOPASSWD: ALL
3. Setup SSH Keys
In this case all you need to do is add your own public key(s) to
4. Setup RVM for
I prefer to setup RVM for the specific user (instead of system-wide). The following command will directly install the latest ruby as well.
\curl -L https://get.rvm.io | bash -s stable --ruby
5. Apache + Passenger
Then I setup Apache with Passenger. It’s fine if you want to use Nginx instead.
sudo apt-get install libcurl4-openssl-dev apache2-mpm-worker libapr1-dev libaprutil1-dev apache2-threaded-dev gem install passenger passenger-install-apache2-module
Just follow the instructions of the Passenger install script. Depending on your system configuration, you may be asked to add some (temporary) swap space.
Passenger will spit out three lines that you should add to the bottom of
LoadModule passenger_module /home/myapp/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.21/buildout/apache2/mod_passenger.so PassengerRoot /home/myapp/.rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.21 PassengerDefaultRuby /home/myapp/.rvm/wrappers/ruby-2.0.0-p247/ruby
6. Configure Apache VirtualHost
Next, create and enable an Apache virtual host for your app:
<VirtualHost *:80> ServerName myapp.example.com DocumentRoot /home/myapp/app/public <Directory /home/myapp/app/public> AllowOverride all Options -MultiViews </Directory> </VirtualHost>
And enable it:
sudo ln -sf /etc/apache2/sites-available/myapp.example.com /etc/apache2/sites-enabled/010-myapp.example.com service apache2 restart
7. Install your database, tools, etc.
Install postgres, redis or whatever rocks your boat. Make sure to also install the proper tools (like imagemagick) and development headers.
Also make sure that you update
config/database.yml in your project and commit these changes and the files in
I’m assuming you also create a database now, as we won’t be running
rake db:create later on.
8. Add foreman for Sidekiq
I want to use foreman to manage starting/stopping sidekiq. Add foreman to your
gem 'foreman' # Optional, but works well with a Debian server gem 'foreman-export-initscript'
And add a line to
Procfile, of course, add any parameters you need for running sidekiq here.
worker: bundle exec sidekiq
9. Enable RVM for git-deploy
In order to use RVM and
rvmsudo properly, we need to set that up in
deploy/after_push. Add the following lines before the
run commands at the bottom.
export rvmsudo_secure_path=1 PATH=$PATH:$HOME/.rvm/bin
10. Sidekiq deploy configuration
There are two things we need to do. Firstly, we need to generate an initscript with foreman. We do this in
run "rvmsudo bundle exec foreman export initscript /etc/init.d -a myapp -u myapp -l /var/log/myapp" run "rvmsudo chmod +x /etc/init.d/myapp"
Secondly, we need to use this initscript to actually restart Sidekiq in
echo "restarting foreman" sudo /etc/init.d/myapp restart
11. Ready? Deploy it!
Now, deploying is rather easy:
git push production master
Well, from now on you can simply push your code with git and
git-deploy will take care of it all:
git push production master
A few things to keep in mind:
- You don’t need the
git-deploygem installed to deploy your project.
- Keep your
deploy/scripts up to date and under version control.
- Anyone with SSH access to your server can deploy the project.
I think that capistrano is a fine tool, but for many projects it’s overkill.
git-deploy is light weight and easy to use and gets the job of deploying your app done quickly. It’s also easily extendable, if you need it.
Give it a try. You can easily spin up a VPS over at Digital Ocean to play around or host your next awesome project. (Disclaimer: referral link). If you use Digital Ocean, you may also be interested in my iOS App: Binary Deep.