How we Deploy with Capistrano

Introduction

We love to automate things and we want to have fun while we deploy, therefore we use Capistrano. It is a fun, smooth and geeky way to deploy apps and even web sites. We deploy and build our blog this way. Interested? Keep on reading.

Little bit about Capistrano

Capistrano is a Ruby program, and we love Ruby, that gives you a set of advanced tools to deploy web applications to your servers.

Capistrano is:

  • an open source tool,
  • a server management tool,
  • and most importantly an application deployment tool.

Demonstration

Capistrano in action

Watch the video below to see Capistrano in action when deploying this very blog.

Requirements

Check for Ruby

Install Ruby before Capistrano installation

Test the Ruby version.

ruby -v

The version should be something current like 2.5.1 or higher.

Installation and Setup

Capistrano installation

Capistrano is packaged as a Ruby gem so the istallation is really simple. Use the following command.

gem install capistrano

Project setup

Make your project directory where we will generate the project files.

mkdir ~/project
cd ~/project

Generate all needed files with this command:

cap install

This is the project tree that Capistrano generates.

├── Capfile
├── config
│   ├── deploy
│   │   ├── production.rb
│   │   └── staging.rb
│   └── deploy.rb
└── lib
    └── capistrano
        └── tasks

Customize stages

To customize the generation of stages use the following command.

cap install STAGES=production,development

This will generate development.rb and production.rb files as seen below.

config/deploy
├── development.rb
└── production.rb

Basic info on generated files.

  • config/deploy.rb - This file is where shared setting for all stages go
  • config/deploy/ - This directory holds stage specific settings like branch names etc.

Edit generated files

config/deploy.rb

This file is used to setup shared setting between different stages. For example both production and development stage can share the application name or git repository url but not the branch name.

Add values to file

Config valid for current version and patch releases of Capistrano

lock "~> 3.11.0"

set :application, "app_name"
set :repo_url, "ssh://your_repo"

set :deploy_to, "location_on_server"

Default value for :format is :airbrussh

 set :format, :airbrussh

Edit file config/deploy/production.rb

set :stage, :production
set :branch, "master"
set :repo_url, "ssh://your_repo"

server 'server_name', user: 'user_name', roles: %w{web app}, port: port_number

To deploy, must be in root of project

cd ~/project

Setup Keys

Setup SSH based key auth

Key based ssh logins need to be setup in order to login to the server where we will deploy our app to.

Copy local user’s public key to remote server where we depoy our app.

ssh-copy-id -i ~/.ssh/id_rsa.pub inservio_blog@server_deploy_blog

Test if the connection work

ssh inservio_blog@server_deploy_blog

Setup Deploy Key

Deploy key needs to be setup for the git repository so that the server where we deploy the app to can access the git server.

Once we are logged on the the server where the app will be deployed we need to add the user’s public key to the git repository as the deploy key. In this case the user is inservio_blog.

Get the key from the remote server where we will deploy the app to

cat /home/inservio_blog/.ssh/id_rsa.pub

Copy and add the key to the git repository.

Deploying different stages

To deploy the production stage use command below.

cap production deploy

To deploy the development stage use the command below.

cap development deploy