Web制作メモ -トップへ-

Capistrano2の設定

Rails環境再構築2016.02.24 23:56

バージョン3が出ていますが、手持ちの本がバージョン2なので2を使っています。

Gemfileでインストールする

gem 'capistrano', '~> 2.0'
gem 'capistrano-unicorn', require: false

バンドルインストール

$ cd [プロジェクト名]
$ bundle install

インストールされたバージョン

$ cap -V
Capistrano v2.15.7

設定ファイルを作成する

$ capify .

Capfileを修正

$ vi Capfile
load 'deploy'
load 'deploy/assets'
load 'config/deploy'

config/deploy.rbを修正

require "bundler/capistrano"
default_run_options[:pty] = true

set :application, "[プロジェクト名]"
set :rails_env, "production"
set :keep_releases, 3

server "[デプロイ先のホスト名].[ドメイン]", :web, :app, :db, primary: true

# リポジトリの設定
set :repository, "git@bitbucket.org:[ユーザー名]/[リポジトリ名].git"
set :scm, :git
set :branch, "master"

# 本番環境への接続設定
set :user, "[ユーザー名]" # 本番環境へのログイン名
set :use_sudo, false #deployコマンドをsudo付きで実行するか

# デプロイ方法の設定
set :deploy_to, "/[プロジェクト名]"
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true

# その都度クリーンアップ
after "deploy", :except => { :no_release => true } do
  deploy.cleanup
end

# デプロイ後にサービスを再起動
after "deploy", roles: :app do
  run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:migrate:reset"
  run "cd #{current_path} && rake RAILS_ENV=#{rails_env} db:reset"
  run "sudo service nginx restart"
  run "sudo service unicorn restart"
end

# バンドルインストール
desc "Bundle install"
task :install do
  run "cd #{current_path} && bundle install"
end

デプロイ準備

$ cap deploy:setup

デプロイ実行

$ cap deploy:update

タスクの一覧を確認しておく

$ cap -vT
cap bundle:install                    # Install the current Bundler environment.
cap deploy                            # Deploys your project.
cap deploy:assets:clean               # Run the asset clean rake task.
cap deploy:assets:clean_expired       # Clean up any assets that haven't been deployed for more than :...cap deploy:assets:precompile          # Run the asset precompilation rake task.
cap deploy:assets:rollback            # to shared/assets/manifest, and finally recompiling or regenera...cap deploy:assets:symlink             # [internal] This task will set up a symlink to the shared direc...cap deploy:assets:update_asset_mtimes # [internal] Updates the mtimes for assets that are required by ...cap deploy:check                      # Test deployment dependencies.
cap deploy:cleanup                    # Clean up old releases.
cap deploy:cold                       # Deploys and starts a `cold' application.
cap deploy:create_symlink             # Updates the symlink to the most recently deployed version.
cap deploy:finalize_update            # [internal] Touches up the released code.
cap deploy:migrate                    # Run the migrate rake task.
cap deploy:migrations                 # Deploy and run pending migrations.
cap deploy:pending                    # Displays the commits since your last deploy.
cap deploy:pending:diff               # Displays the `diff' since your last deploy.
cap deploy:restart                    # Blank task exists as a hook into which to install your own env...cap deploy:rollback                   # Rolls back to a previous version and restarts.
cap deploy:rollback:cleanup           # [internal] Removes the most recently deployed release.
cap deploy:rollback:code              # Rolls back to the previously deployed version.
cap deploy:rollback:revision          # [internal] Points the current symlink at the previous revision.
cap deploy:setup                      # Prepares one or more servers for deployment.
cap deploy:start                      # Blank task exists as a hook into which to install your own env...cap deploy:stop                       # Blank task exists as a hook into which to install your own env...cap deploy:symlink                    # Deprecated API.
cap deploy:update                     # Copies your project and updates the symlink.
cap deploy:update_code                # Copies your project to the remote servers.
cap deploy:upload                     # Copy files to the currently deployed version.
cap deploy:web:disable                # Present a maintenance page to visitors.
cap deploy:web:enable                 # Makes the application web-accessible again.
cap invoke                            # Invoke a single command on the remote servers.
cap shell                             # Begin an interactive Capistrano session.

Extended help may be available for these tasks.
Type `cap -e taskname' to view it.