Engineering August 3, 2017 3 min read

Deploy your Rails app with Capistrano

Deploy your Rails app with Capistrano

Are you building an awesome Rails web application? Do you want to deploy it in a remote machine so other users can use it? Do you want to deploy your web app using a simple command like: cap production deploy? If you answered yes to these questions this post is for you! In the post I will present you with a beginner guide explaining how to configure and use Capistrano.

How does Capistrano work?

Capistrano is a framework written in Ruby that provides automated deploy scripts. It connects to your web server via SSH and executes a bunch of tasks that will make your app ready to shine.

Capistrano requires your code to be under some version control system (e.g. Git), but nowadays that’s an accepted best practice that is followed by pretty much everyone. Also, the web server machine must have installed the binaries of the version control system you’re using, because the code will be copied from there.

Installation

Add the gem to your Gemfile:

Code
group :development do
  gem "capistrano"
end

Then run:

Code
bundle install

Then lets Capify your project:

Code
cap install

This will create a bunch of new files on your application, I’m going to talk about them now.

Capfile

This configuration file is located at the root of your project and aims to load the tasks needed to launch the app.

Each require will automatically add new tasks to your deploy process. For instance, capistrano/rails” will add the following tasks:

  • assets.rake, which is responsible for cleaning and compiling the assets of you app.
  • migrations.rake, which will check if there are any pending migrations and run them.

In order to add these tasks you should add the respective gem to your Gemfile:

So, in order to require capistrano/rails”, and use the tasks provided you should add “capistrano-rails” gem to your Gemfile and then run:

Code
bundle install

This process will also be applicable to other dependencies you might use in your application, such as:

config/deploy.rb

This file will have the common configurations to all environments. Here you can configure things like:

  • app name
  • github project path
  • the user used to login in the web machine
  • the password of the user (you can do that, but I would not recommend)
  • the path of deploy (the folder where your app will stay at the web server)

I will not recommend you to store your server password in a file that belongs to your repository. So, instead of doing that I recommend the use of RSA keys. Here is a tutorial explaining how to configure SSH keys.

config/deploy/production.rb

The folder config/deploy holds the configurations of each environment. If you have a staging and a production environment you will have two files at this folder. Some configurations will be different depending on the environment, such as:

  • the branch to deploy
  • The IP address of the machine where your code will be deployed

Test your configs

You’ve already configured everything you need in order to deploy your app, so now it’s time to test all the configurations. Capistrano provides a way to test them, with the command:

Code
cap production deploy:check

You should check if everything is working properly before proceed.

Deploy

Now you should be ready to do your first deploy! Run the following command to deploy your code in production environment:

Code
cap production deploy

This will copy your code from the branch you specified at production.rb file into the folder specified by “deploy_to” variable, it will also run the tasks that will launch your app.

Conclusion

Now you should be able to deploy your app on a remote machine and everyone will be able to have the benefit of it! Capistrano is an awesome framework and will decrease your work from now on, all your commands are now abridge on a single command!

I hope this beginner guide was helpful to you. I will appreciate your comments and to know how you’re going with Capistrano.

I’m a web developer at Runtime Revolution, I like to be challenged everyday, learn new stuff and feel that I’m valuable to the company and to our clients. I am also interested in automation, I believe that it helps a lot in your daily cycle, increasing your productivity.

Runtime RevolutionWe are Rails, mobile and product development experts. We can build your product or work with you on your project.www.runtime-revolution.com