Ned
Ned Hello, I'm Nedim, a Cloud Engineer who enjoys writing about technology, particularly focusing on Linux and DevOps. Recently, I've been delving into topics like digital marketing, online presence, and startup culture.

Mastering Automated Deployments: A Step-by-Step Guide to Deploying with Capistrano

Mastering Automated Deployments: A Step-by-Step Guide to Deploying with Capistrano

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

A little bit about Capistrano

Capistrano is a Ruby program, and I love Ruby, which gives you a set of advanced tools to deploy 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

RVM (Ruby Version Manager)

In order not to use the outdated system version of Ruby we will need RVM.

Install RVM

I recommend RVM or Ruby Version Manager to install the latest Ruby version.

Install GPG keys:

1
gpg2 --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
1
\curl -sSL https://get.rvm.io | bash -s stable

Install Ruby with RVM

After you install RVM to install Ruby use the following command.

1
2
rvm install 3.3.0
rvm --default use 3.3.0

Check for Ruby

Install Ruby before Capistrano installation

Test the Ruby version.

1
ruby -v

The version should be something current like 3.3.0 or higher.

Installation and Setup

Capistrano installation

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

1
gem install capistrano

Project setup

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

1
2
mkdir ~/project
cd ~/project

Generate all needed files with this command:

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

1
cap install STAGES=production,development

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

1
2
3
config/deploy
├── development.rb
└── production.rb

Basic info on generated files.

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

Edit generated files

1
config/deploy.rb

This file is used to set up shared settings between different stages. For example, both the production and development stages can share the application name or git repository URL but not the branch name.

Add values to the file

Config is valid for the current version and patch releases of Capistrano.

lock "~> 3.18.0"

set :application, "my_app_name"
set :repo_url, "[email protected]:me/my_repo.git"

Default value for :formatis :airbrussh

 set :format, :airbrussh
Production stage

In the file config/deploy/production.rb we will set:

  • stage name,
  • git branch
  • git repository URL where from to pull the code,
  • server connection string.

In the following example server-based syntax is used to define the server connection.

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

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

To deploy, must be at the root of the project

1
cd ~/project

Authentication

Setup Keys

Setup SSH-based key auth

Key-based ssh logins need to be set up to log in to the server where I will deploy our app.

Copy the local user’s public key to a remote server where I deploy our app.

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

Test if the connection works

1
ssh blog_user@server_deploy_blog

Setup Deploy Key

The deploy key needs to be set up for the git repository so that the server where I deploy the app can access the git server.

Once I am logged on the the server where the app will be deployed I need to add the user’s public key to the git repository as the deploy key. In this case, the user is blog_user.

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

1
2
su blog_user -l
cat ~/.ssh/id_rsa.pub

Copy and add the key to the git repository.

Deploying different stages

To deploy the production stage use the command below.

1
cap production deploy

To deploy the development stage use the command below.

1
cap development deploy
Rating: