e.preventDefault(); // Prevent form submission input.blur(); // remove focus input.value = ""; // optional: clear the input

Deploy Rails app with Capistrano

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

  2. 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
  3. 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
  4. How to deploy

    #Execute the process on the remote server and display the log on the terminal.
    $ cap production deploy
  5. 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
    
  6. Conclusion

    Now we can deploy our app by using a single command. It is like bread and butter for the developer.

Related Posts

Latest Posts

  • All Posts
  • Generative AI
  • manufacturing
  • News
  • Portfolio
    •   Back
    • Android
    • iOS
    • Java
    • PHP
    • MEAN
    • Ruby
    • DotNet
    • IoT
    • Cloud
    • Testing
    • Roku
    • CMS
    • Python

India

Plot No. 11/2, Phase 3, Hinjewadi Rajiv Gandhi Infotech Park, Pune, India – 411057.
info@tudip.com
+91-96-8990-0537

United States

1999 S. Bascom Ave Suite 700, Campbell CA. 95008, USA.
info@tudip.com
+1-408-216-8162

Canada

64 Caracas Road North York, Toronto Ontario M2K 1B1, Canada.
info@tudip.com

Mexico

Calle Amado Nervo #785 Interior B Colonia Ladron De Guevara 44600 Guadalajara, Jalisco, Mexico.
info@tudip.com

Colombia

Cra. 9 # 113-53 Of. 1405 Bogotá D.C., Colombia.
info@tudip.com

UAE

Tudip Information Technologies L.L.C Office No 109, ABU HAIL BUILDING 13, Abu Hail, Dubai, UAE.
info@tudip.com

Nigeria

22 Kumasi Crescent, Wuse 2, Abuja, Nigeria.
info@tudip.com