In the lifetime of every application the time comes for it to be presented to everyone. That’s why we have to put our application on a special server which is designed for this purpose. In one word, we need to deploy our application. In this post you will see how to deploy app with Capistrano 3.
Capistrano is a great developers tool that is used to automatically deploy projects to remote server.
I will assume you already have a server set up and an application ready to be deployed remotely.
We will use gem ‘capistrano-rails’, so we need to add this gems to Gemfile:
group :development do gem 'capistrano', '~> 3.5' gem 'capistrano-rails', '~> 1.1.6' end
and install gems with $ bundle install
.
Then run the following command to create configuration files:
$ bundle exec cap install
This command creates all the necessary configuration files and directory structure with two stages, staging and production:
Capfile config/deploy.rb config/deploy/production.rb config/deploy/staging.rb lib/capistrano/tasks
Open the Capfile
and add or uncomment this lines:
require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
The capistrano-rbenv gem provides rbenv support for Capistrano 3.
Add this line to the Gemfile:
group :development do gem 'capistrano', '~> 3.5' gem 'capistrano-rails', '~> 1.1.6' gem 'capistrano-rbenv', '~> 2.0', require: false end
And require this gem in Capfile require 'capistrano/rbenv'
.
The capistrano-passenger gem adds a task to restart your application after deployment via Capistrano 3.
group :development do ... gem 'capistrano-rbenv', '~> 2.0', require: false gem 'capistrano-passenger', '~> 0.2.0' end
And require this gem in Capfile require 'capistrano/passenger'
config/deploy.rb
and add options for deployment:set :application, 'app-name' # application name set :deploy_user, 'user-name' # name of user who is set on server set :repo_url, 'git@github.com:nickname/repo_name.git' # your repository url from github set :branch, ENV.fetch('BRANCH', 'master') # branch which you want to deploy fromset the path where you want to find your app on server, starting from server’s root
set :deploy_to, -> { "/path/to/app/#{fetch(:rails_env)}-#{fetch(:application)}" }
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml') set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :rbenv_type, :user set :rbenv_ruby, '2.2.2'
gem 'capistrano-passenger'
set :passenger_restart_with_touch, true
namespace :deploy do desc "Description of task" task :name_of_task do # do something end end
You need to tell Capistrano where to find your server.
This is an example of server’s settings for application where everything is on same machine (application, server, database).
config/deploy/staging
set:
server 'your.staging.server.com', user: fetch(:deploy_user), roles: %w{app db web}
set :rails_env, 'staging'
Also set the same configuration for production server.
In `config/deploy/production` add:
server 'your.production.server.com', user: fetch(:deploy_user), roles: %w{app db web} set :rails_env, 'production'
Just run deploy
task:
bundle exec cap staging deploy
or
bundle exec cap production deploy
and that is it, your app is live and you can visit it on server’s name url, in our example case your.staging.server.com.
Note: you can find complete documentation on Capistrano site.
Have a nice day!