Capistrano is a remote server automation open-source tool written in Ruby that gives a set of advanced tools to deploy web applications to the servers.
To perform the deploy functions Capistrano does not require any big set of additional applications; it just requires an ssh connection with a new user who has the permission to copy the code from the source control repository to the server and Ruby Gems, the package manager for the Ruby programming language.
Although Capistrano is written in Ruby, it can easily be used to deploy projects of any language.
The main advantage of Capistrano is that it’s also possible to deploy to multiple machines at once and can be deployed by writing just one console command cap and if something goes wrong the application can be rollback to the last release by using another one cap.
Capistrano decides on how to structure the scripts, where deployed files should be placed on the server, or how to perform common tasks.
Capistrano instance can be easily extended using the RubyGems package manager and can run each deployment task concurrently across those servers i.e the parallel execution of the task, that too without any downtime.
Main Features of Capistrano
- SSH connection
Capistrano does not require a big set of additional applications on the server. All it needs is an ssh connection, i.e. it depends on connecting to the server(s) with SSH using key-based (i.e. password-less) authentication.
- Multiple stages
Multiple stages are all about parallel execution of the task in a fleet of servers, as the Capistrano has the ability to run each task concurrently by giving specified roles to the participating servers.
- More roles for servers
Capistrano assigns each server with one or more roles to control the tasks that are executed, as the application may need different types of servers for database, application, web etc.
As Capistrano is a Ruby program, the RubyGems package manager helps to extend the Capistrano instance. The plugins in the Capistrano for different frameworks provide a set of tasks. This plugin is intended to be used before any deployment task.
Also, some of the Ruby projects comes with Capistrano tasks built-in.
The official Capistrano Plugins are listed below.
>> Ruby on Rails
>> Phusion Passenger
Capistrano can be installed as a standalone Gem or bundled into the application and as Capistrano is a Ruby package, we will need to install Ruby on our system and can be set by running ‘cap install’ in the project’s root directory.
When the cap is run, Capistrano connects to the servers via SSH and executes the steps necessary to deploy your project.
Below is the structure of the files and directories for a Capistrano-enabled project with two stages, namely staging and production.
The stages will have a set of servers that can have many roles.
├── Capfile ├── config │ ├── deploy │ │ ├── production.rb │ │ └── staging.rb │ └── deploy.rb └── lib └── capistrano └── tasks
The stage-specific configuration files are for introducing the Capistrano about the task on the servers.
The files that Capistrano creates are simply templates and we should make sure to edit the deploy.rb and stage files so that they contain values appropriate for the projects and the target servers.
There are two modes for the execution of commands in Capistrano, interactive and non-interactive modes.
The difference is that the interactive mode is used to get immediate feedback when we don’t know which command needed to be executed and the non-interactive mode is used when we know the command to be used and just want to see the result.
The latest released Capistrano v3 provides a default deploy flow and a rollback flow. The tasks involved are listed below.
deploy: starting – starts deployment
deploy: started – start hook for the custom tasks
deploy: updating – updates the servers to a new release
deploy: updated – update hook for the custom tasks
deploy: publishing – publishes the new release
deploy: published – publish hook for the custom tasks
deploy: finishing – finish the deployment
deploy: finished – finish hook for the custom tasks
The hooks are used for adding their own tasks.
Deploy: reverting- reverting the server to the previous version.
Deploy: reverted- reverted hook
Deploy: finishing_rollback – finish the rollback
Benefits of Capistrano
-> Can deploy the application by one console command.
cap <stage_name> deploy
-> Can rollback the application by another command.
cap <stage_name> rollback
-> Can simply SSH to a lot of servers and perform a bunch of actions
- Capistrano is much easy such that it makes the development so simple and will decrease the workflow.
- The power of Capistrano becomes more specified when we have many servers with distinct roles.