Deploy Rails app with Capistrano
Deploy Rails app with Capistrano
-
What is Capistrano
Capistrano is a framework written in Ruby language that provides automated deployment. Similar to Rake tasks in Ruby language, it runs on the remote servers via SSH. It can be configured on the local or remote server. It performs the task like copy the code from the remote repository (git..), asset precompilation, migrating the database, cron jobs, restarting the Rails server and many more.
-
Why we need to deploy rail app by Capistrano
As we deploy our latest changes on the staging/production server by taking a pull of the latest code, restarting prerequisite commands like (cron, server..). This is an old school process, but now there are multiple tool or framework are available that minimize our time. Like “cap production deploy” command makes everything instead of repeating the whole process on each deployment.
There is some important key features of Capistrano:
- Manage your app code
- Installing new gem
- Restart sidekiq worker, system commands of the OS, Compile assets file and many more
- On the deployment process, if some error occurs it rollback to the previous deployment
-
How to install and configure with a rails app
Hope you have Ruby and RubyGems should already be installed and configured on your system.
#Add gem in your Gemfile: group :development do gem "capistrano" gem "capistrano-rails" end #Run: $bundle install #Capify your project: $cap install
This will create multiple files for the configuration requirement of the Capistrano.
Capfile in root directory deploy.rb file inside the config directory deploy directory inside the config directory create multiple stage file like (staging.rb and production.rb) deploy.rb
In this, we can configure our app name, repository URL, deploy path etc.
We can also set the password but I would not suggest. So instead of that use, RSA keys here is the link(https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys–2)set :scm, :git set :application, 'my-application' set :repo_url, 'git@git-server.com:my-github-username/my-application.git' capfile By default, there are multiple requirements to load the tasks needed to launch the app. Remove the unnecessary require which you don't require for your application. Below is the "require" command which you need to add in your project.
# Load DSL and set up stages require "capistrano/setup" # Include default deployment tasks require "capistrano/deploy" # This will add tasks to your deploy process require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' # Loads custom tasks from `lib/capistrano/tasks' Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r } production.rb (stage file)
Capistrano provide multistage deployment like staging/Production inside the config/deploy directory.
In this, we can add rails environment, branch and different login user role like (web, app, db).
#Add the following lines config/deploy/production.rb file to configure the production environment. server '<server_IP>', user: '<LOGIN_USER>', roles: %w{web app} set :rails_env, , :production set :branch, master deploy.rb Added the sequence of the task in config/deploy.rb:
We can add a list of task inside the deploy namespace, we can also add another namespace if we want to organize or in what manner the task should be executed.
For Example: desc 'Description of task' task :task_name do on roles(:app) # Command end end Bundle install task namespace :deploy do desc 'Bundle install' task :bundle do on primary :app do within release_path do execute :bundle, 'install' end end end end
-
How to deploy
#Execute the process on the remote server and display the log on the terminal. $ cap production deploy
-
Important command related to Capistrano
# list all available tasks $ cap -T # deploy to the staging environment $ cap staging deploy # deploy to the production environment $ cap production deploy # simulate deploying to the production environment # does not actually do anything $ cap production deploy --dry-run # list task dependencies $ cap production deploy --prereqs # trace through task invocations $ cap production deploy --trace # lists all config variable before deployment tasks $ cap production deploy --print-config-variables
-
Conclusion
Now we can deploy our app by using a single command. It is like bread and butter for the developer.