Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created February 21, 2014 08:52
Show Gist options
  • Select an option

  • Save stevenyap/9130882 to your computer and use it in GitHub Desktop.

Select an option

Save stevenyap/9130882 to your computer and use it in GitHub Desktop.

Revisions

  1. stevenyap renamed this gist Feb 21, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. stevenyap created this gist Feb 21, 2014.
    123 changes: 123 additions & 0 deletions Capistrano3.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    This guide explains the way to setup a production server using Capistrano.

    ## Setup Capistrano on LOCAL
    - Capistrano is a development gem which assist the developer to run commands on the production server (something like a Heroku toolbelt)
    - Hence, it is installed and configured on developer's computer

    ```ruby
    # Gemfile

    # Use Capistrano for deployment
    gem 'capistrano', '~> 3.0.1'
    gem 'capistrano-rails', '~> 1.1.0'
    gem 'capistrano-rvm', '~> 0.1.0'
    gem 'capistrano-bundler'
    ```
    - Run `bundle install`
    - Run `cap install STAGES=staging,production`
    - Edit the file `Capfile`

    ```ruby
    # Capfile
    # Make sure the below is uncommented
    require 'capistrano/rvm'
    require 'capistrano/bundler'
    require 'capistrano/rails/assets'
    require 'capistrano/rails/migrations'
    ```

    - Edit the file `config/deploy/staging.rb` with your own user, domain and application:

    ```ruby
    set :stage, :staging
    role :app, %w{localhost}
    role :web, %w{localhost}
    role :db, %w{localhost}

    set :application, 'depot'
    set :repo_url, 'ssh://deploy@localhost:2222/~/git/depot.git'
    set :branch, 'master'
    set :deploy_to, '/var/www/stapi/staging'

    set :ssh_options, {
    forward_agent: false,
    user: 'root',
    port: 2222
    }
    ```

    ## Push from local to server
    - This assumes your local is already git initizated (Why shouldn't you!??)
    - After you have capify and edited all the Capistrano config, you should push it to the server:

    ```sh
    git remote add staging ssh://user@host/~/git/depot.git
    git push staging master
    ```

    ## Deploy Capistrano

    ```sh
    cap staging deploy:check # make sure everything is okay

    # then finally...
    cap staging deploy

    # or for production
    cap production deploy
    ```

    ## Seed database

    ```ruby
    # Add this in config/deploy.rb
    # and run 'cap production deploy:seed' to seed your database
    desc 'Runs rake db:seed'
    task :seed => [:set_rails_env] do
    on primary fetch(:migration_role) do
    within release_path do
    with rails_env: fetch(:rails_env) do
    execute :rake, "db:seed"
    end
    end
    end
    end
    ```

    ## Restart Apache

    ```ruby
    # This is found in /config/deploy.rb
    # This works on apache/passenger
    # To run it, use cap production deploy:restart

    namespace :deploy do
    desc 'Restart application'
    task :restart do
    on roles(:app), in: :sequence, wait: 5 do
    # Your restart mechanism here, for example:
    execute :touch, release_path.join('tmp/restart.txt')
    end
    end
    end
    ```

    ## Inform you when deployment is completed
    - A fun task that works only on Mac to say a message when deployment is completed

    ```ruby
    # /config/deploy.rb

    namespace :deploy do
    desc 'Says a message when deployment is completed'
    task :say do
    system("\\say Capistrano Deployment Completed! Good Job!")
    end
    end

    after :finished, 'deploy:say'
    ```

    ## Issues

    - Nokogiri gem installation error: run `yum install libxml2 libxml2-devel libxslt libxslt-devel`