Mastering Automated Deployments: A Step-by-Step Guide to Deploying with Capistrano
- A little bit about Capistrano
- Installation and Setup
- Deploying different stages
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.
- an open-source tool,
- a server management tool,
- and most importantly an application deployment tool.
Capistrano in action
Watch the video below to see Capistrano in action when deploying this very blog.
RVM (Ruby Version Manager)
In order not to use the outdated system version of Ruby we will need
RVMor Ruby Version Manager to install the latest Ruby version.
Install GPG keys:
gpg2 --keyserver keyserver.ubuntu.com --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
\curl -sSL https://get.rvm.io | bash -s stable
Install Ruby with RVM
After you install RVM to install Ruby use the following command.
rvm install 3.3.0
rvm --default use 3.3.0
Check for Ruby
Install Ruby before Capistrano installation
Test the Ruby version.
The version should be something current like
3.3.0 or higher.
Installation and Setup
Capistrano is packaged as a Ruby gem so the installation is really simple. Use the following command.
gem install capistrano
Make your project directory where I will generate the project files.
Generate all needed files with this command:
This is the project tree that Capistrano generates.
To customize the generation of stages use the following command.
cap install STAGES=production,development
This will generate
production.rb files as seen below.
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
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.
Default value for
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.
To deploy, must be at the root of the project
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.
ssh-copy-id -i ~/.ssh/id_rsa.pub blog_user@server_deploy_blog
Test if the connection works
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
Get the key from the remote server where I will deploy the app to
su blog_user -l
Copy and add the key to the git repository.
Deploying different stages
To deploy the production stage use the command below.
cap production deploy
To deploy the development stage use the command below.
cap development deploy