Skip to content

Instantly share code, notes, and snippets.

@stadniklksndr
Last active June 16, 2017 12:35
Show Gist options
  • Save stadniklksndr/13fa12e1c0fdb713d2ea60b26dac4e26 to your computer and use it in GitHub Desktop.
Save stadniklksndr/13fa12e1c0fdb713d2ea60b26dac4e26 to your computer and use it in GitHub Desktop.

ADD TO Gemfile

group :development do
  gem 'capistrano', '~> 3.8', '>= 3.8.1'
  gem 'capistrano-rvm', '~> 0.1.2'
  gem 'capistrano-rails', '~> 1.2', '>= 1.2.3'
  gem 'capistrano-rails-db'
  gem 'capistrano3-puma', github: 'seuros/capistrano-puma'
  gem 'capistrano-nginx', '~> 1.0'
  gem 'capistrano-upload-config', '~> 0.7.0'
  gem 'sshkit-sudo', '~> 0.1.0'
end
  • bundle install
  • cap install

Fill Capfile

# Load DSL and set up stages

require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/scm/git"

install_plugin Capistrano::SCM::Git

require 'capistrano/rvm'
require 'capistrano/rails'
require 'capistrano/rails/db'
require 'capistrano/puma'

install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Nginx
install_plugin Capistrano::Puma::Jungle

require 'capistrano/nginx'
require 'capistrano/upload-config'
require 'sshkit/sudo'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

Fill config/deploy.rb

lock '3.8.1'

set :user, 'deployer'
set :rails_env, 'production'
set :application, 'APP_NAME'
set :repo_url, "git@gitlab.com:username/#{fetch(:application)}.git"

server 'SERVER_IP_ADDRESS', user: "#{fetch(:user)}", roles: %w{app db web}, primary: true

set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :pty, true

append :linked_files, 'config/database.yml', 'config/secrets.yml', 'config/puma.rb'
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads'

set :config_files, %w{config/database.yml config/secrets.yml}
set :puma_conf, "#{shared_path}/config/puma.rb"

namespace :deploy do
    before 'check:linked_files', 'config:push'
    before 'check:linked_files', 'puma:jungle:setup'
    before 'check:linked_files', 'puma:nginx_config'
    before 'deploy:migrate', 'deploy:db:create'
    after 'puma:smart_restart', 'nginx:restart'
end

Generate config/database.production.yml (see example below)

production:
    adapter: postgresql
    database: database_name_production
    user: database_user_name
    password: password
    encoding: unicode
    host: localhost
    pool: 5
    timeout: 5000
  • echo '/config/database.production.yml' >> .gitignore

Generate config/secrets.production.yml (see example below)

development:
    secret_key_base: key

test:
    secret_key_base: key

production:
    secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  • echo '/config/secrets.production.yml' >> .gitignore

Generate puma-nginx configs (Files will be added to config/deploy/templates)

  • rails g capistrano:nginx_puma:config

Deploy app by command below

  • cap production deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment