About Capistrano

Tram Ho

You all know that, to deploy an application to the server, we have to go through a lot of steps. Writing each command every time we need to deploy, update an application makes us tired, time-consuming, labor-intensive, as well as the potential for unnecessary errors.

So today I will introduce to everyone about Capistrano to deploy the application to the server. This is a very famous tool, we can deploy an application through 1 command!

Note: I will default when everyone reads this article that already knows about how to deploy a Ruby application. Capistrano only works when everyone understands about deploying, but if you do not know about deploying, please read through this tutorial !

A few basic concepts

Introduced via

Capistrano is a tool that allows you to automatically execute commands on a remote server using SSH. Capistrano works on your personal computer (not on the server) and then access one or several servers (we have configured) via SSH to execute the commands we have set. . Capistrano uses Ruby as a language to write scripts.

Capistrano will require us to provide at least the following:

  1. A Capistrano script, which includes the Capistrano commands that must be executed, as well as which server needs to execute those statements. This script file uses Ruby to write.
  2. One or several configuration files to provide information about the server, as well as how to log in to those servers.

Capistrano can also run commands simultaneously on different servers, which is beneficial when you deploy applications to many different servers.

Workflow

  1. First, of course, we need to install and edit Capistrano on our personal computers (note that it’s a personal computer, not a server).
  2. Every time we need to deploy a new version of the application, we need to:
  • Commit and push the whole code to git repo (which we provided in Capistrano)
  • Run the Capistrano deploy command

Recipes:

Capistrano is very useful, but its true strength lies in the recipes that the community builds for it.

For example, if we use capistrano/deploy recipe, it will contain commands that help us automatically git clone , git pull , as well as support for rollback very easily. All we need to do is give Capistrano the URL of git repo.

Or if you want to deploy a rails application, capistrano-rails will help you to run bundle install , compile Rails assets, run db migrate, etc.

Directory structure of Capistrano:

After deploying the app on the server using Capistrano, the directory will have the following structure:

  1. releases contains all versions of the app, marked with timestamped folders. Every time you tell Capistrano to deploy a new version, Capistrano will clone from the Git repo and save it in a subdirectory of releases .
  2. current a simple current it is a symlink pointing to the latest version contained in releases (based on the timestamp). This symlink only updates when the deploy process is complete, if the deploy process fails at any step, this symlink still points to the previous version.
  3. repo stores a copy of the GIt repo, so that the next time we can pull it back faster.
  4. shared here is a great folder of Capistrano, let me give you an example:

Because Capistrano works by cloning applications from Git, so only files we upload to Git can be cloned. So for example with a Rails application, there are files that we can only keep in the local environment (database.yml, secrets.yml, …), how can we use this? And Capistrano offers a solution, that is, users can configure files, even folders, that can be shared directly between development and production environments (personal computers and servers). These files will always exist no matter how many versions we put up.

To configure shared files and folders, Capistrano provides two configuration options, linked_files and linked_dirs

Install Capistrano

Initialization

First we need to install Capistrano on the Ruby application we are working on. Open Gemfile and add:

Then run 2 commands:

Edit Capfile:

Capfile is the first place that Capistrano reads, in this file it will define which recipes you want to use.

When you first open Capfile, it will look like this:

It can be seen that the most popular recipes have been commented back here, when we need to use any recipe, just uncomment is done.

Edit the file config / deploy.rb

When opened, this file looks like this:

  • First, people will notice in the file that there are many commands that start with set , the set here is the configuration values ​​of Capistrano, used to adjust the settings for Capistrano. It can be seen in the default file that there are many sample examples, explaining the meaning of the command line, you can try uncomment to see the effect of those set lines, or can learn more here .
  • The second thing we need to keep in mind is the namespace block inside this file. We can use this block to create more commands to handle during deploy. For simple applications, just follow the recipe mentioned above, but for complex applications, we have to add the commands ourselves is completely possible. When the application is deployed, it will have to run through the steps defined in the recipe capistrano/deploy , the steps are as follows:
  1. Clone code about release directory
  2. Install linked file and directory
  3. Run commands like bundle install, db migrate, …
  4. Convert current symlink
  5. Restart the server

As in the example that exists in the file, it means we will create another task with the name :clear_cache after the application runs the restart step (step 5). In this block we can write whatever we want, it’s the same as the callback functions we usually write in the model.

Edit the file config / deploy / production.rb

This file is used to indicate which servers that Capistrano will run the command to deploy to. As you can see in the file, there are two main ways to specify servers as well as how to log into those servers.

  • The first is to use 1 global config for all servers. This means that our servers all use the same rsa code to log in via ssh from the local machine.
  • The second way is if any server uses a different rsa code from the rest of the servers, we can define that server separately.

References

https://www.phusionpassenger.com/library/deploy/standalone/automating_app_updates/ruby/

Share the news now

Source : Viblo