Monday, July 6, 2015

Cap Deploy [Capistrano]

Capistrano

Ref:
http://capistranorb.com/
https://github.com/capistrano/ # Shows list of all capistrano repos
https://github.com/capistrano/capistrano
https://github.com/capistrano/rails
https://github.com/capistrano/passenger



Gemfile


gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-passenger'
gem 'capistrano-rvm'  #if your preference is RVM



$> bundle install
$> bundle exec cap install #Make sure files are created
$> bundle exec cap install STAGES=production  #add additional env seperated by ','

Limited gem installation [selected env]
$> bundle install --without development test


Capfile


require 'capistrano/rails'

#or
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/passenger'

#For rbenv
require 'capistrano/rbenv'
require 'capistrano/chruby'



Ref: http://capistranorb.com/documentation/getting-started/configuration/

config/deploy.rb


set :application, '<app_name>'
set :repo_url, 'git@.... <git repo>'

set :linked_files, fetch(:linked_files, []).push('config/database.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

set :keep_releases, 5



config/deploy/production.rb

server '<dest_server>', user: '<dest_server_user>', roles: %w{web db app}
set :deploy_to, "/var/www/html/<dir>"

set :passenger_restart_command, 'passenger-config restart-app'

set :passenger_restart_options, -> { "#{deploy_to} --ignore-app-not-running" }

Adding tasks

Ref: http://capistranorb.com/documentation/getting-started/cold-start/

in create lib/capistrano/tasks/access_check.rake file, and add

  namespace :check do
    desc "Check that we can access everything"
    task :write_permissions do
      on roles(:all) do |host|
        if test("[ -w #{fetch(:deploy_to)} ]")
          info "#{fetch(:deploy_to)} is writable on #{host}"
        else
          error "#{fetch(:deploy_to)} is not writable on #{host}"
        end
      end
    end
  end

$> bundle exec cap -T #to check the task existence
$> bundle exec cap production check:write_permissions #to verify the write permission


[Still working on Cap Scripting]

Basic Commands

cap production deploy:check
cap production deploy 
cap production deploy:rollback



Advance Comments [from capistrano Git page]


# list all available tasks
$ bundle exec cap -T

# deploy to the staging environment
$ bundle exec cap staging deploy

# deploy to the production environment
$ bundle exec cap production deploy

# simulate deploying to the production environment
# does not actually do anything
$ bundle exec cap production deploy --dry-run

# list task dependencies
$ bundle exec cap production deploy --prereqs

# trace through task invocations

$ bundle exec cap production deploy --trace


Advance Learning

https://github.com/capistrano/sshkit


Deploy issue with git

When you get: git stdout: permission denied (publickey).

$> eval $(ssh-agent)
$> ssh-add



No comments:

Post a Comment