Kamis, 12 Oktober 2017

Setup Capistrano Menggunakan Rbenv

Apa Itu Capistrano?

Capistrano adalah gem service yang dibuat untuk memindahkan aplikasi dari local ke server.

1.Buka terminal, buat project baru
$ rails new capistrano
$ rake db:create
2. Tambahkan gem capistrano pada gemfile
group :development do
  gem 'capistrano', '~> 3.7', '>= 3.7.1'
  gem 'capistrano-rails', '~> 1.2'
  gem 'capistrano-passenger', '~> 0.2.0'
  gem 'capistrano-rbenv', '~> 2.1'
end
3.Terminal jalankan command ini:
$ bundle install
$ cap install
Command diatas akan membuat beberapa file dan folder seperti berikut:
Capfile
config/deploy.rb
config/deploy/production.rb
config/deploy/staging.rb

5.Di file Capfile uncomment kode ini
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/rbenv'
6.Generate secret key base:
$ rake secret
$ d58c2bf6365e5758c5c6081a96d4b7b10fd826021e5415fe92451d19caa8063a75e84c5ed47cff7612e092afb220a4afddd8831ad7c8c200afdee42dd0c0cfa1
production:
  secret_key_base: d58c2bf6365e5758c5c6081a96d4b7b10fd826021e5415fe92451d19caa8063a75e84c5ed47cff7612e092afb220a4afddd8831ad7c8c200afdee42dd0c0cfa1
masukan kode diatas pada config/secrets.yml

7.Masuk ke servermasuk ke folder deploy/staging.rb lihat pada baris ke 2 contoh:
set :stage, :production
server '139.59.255.66', user: 'deploy', roles: %w{app db web}
$ ssh deploy@139.59.255.66
8.Masuk ke folder aplikasi pada server lalu setting database di server
$ nano app/nama_aplikasi/current/config/database.yml
production: adapter: mysql2 encoding: utf8 username: root pool: 5 password: password_anda database: nama_database
lalu save ctrl + x + y

10.Nyalakan database di server
$ sudo service mysql start
11.Setup file config/deploy.rb
 #config/deploy.rb
 set :application, "nutrilunch"
set :repo_url, "git@username/nama_repository"

set :deploy_to, '/home/deploy/app/nama_aplikasi'

set :keep_releases, 5
set :rake, 'bundle exec rake'

set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}


namespace :deploy do
  before :publishing, 'deploy:migrate'
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

12.Deploy ke server
$ cap staging deploy

untuk selebihnya mengenai capistrano bisa klik link dibawah ini:


selamat mencoba & happy coding :)
ersad@doterb.com | Web Developer at doterb solution