LoginSignup
41
41

More than 5 years have passed since last update.

capistrano使い方まとめ

Posted at

初めてcapistranoを使ったので、デプロイまでをまとめ。
アプリはRailsで、ステージング環境をデプロイする。

事前準備(Rails)

Railsにはstaging環境がデフォルトでは用意されていないので、staging用の設定を事前に行いコミットしておく。

  • staging用secret_key_baseの作成
$ RAILS_ENV=staging rake secret

secrets.ymlにstaging用のsecret_key_baseを追加する。

config/secrets.yml
staging:
  secret_key_base: 【rake secretで出力された値】
  • staging用DBを設定

database.ymlにstaging用のDBの設定を追加する。

config/database.yml
staging:
  <<: *default
  database: DB名_staging
  • config/environments にstaging用ファイルを作成

config/environments/production.rbをコピーしてstaging.rbを作成する。

$ cd config/environments
$ cp production.rb staging.rb

事前準備が完了したらcapistranoの設定を行う。

Gemfile作成

デプロイを実行サーバの任意のディレクトリでGemfileを作成

Gemfile
source 'https://rubygems.org'
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rbenv'

gemをインストール。

$ bundle install

capistranoのデフォルトファイル作成

capコマンドでデフォルトファイルを作成する。

$ bundle exec cap install
# 以下のディレクトリ・ファイルが作成される
mkdir -p config/deploy
create config/deploy.rb
create config/deploy/staging.rb
create config/deploy/production.rb
mkdir -p lib/capistrano/tasks
create Capfile
Capified

Capfile

capistrano設定ファイル。
今回はrbenvを利用してrubyがインストールされている想定で設定を行う。

Capfile
# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
#   https://github.com/capistrano/rvm
#   https://github.com/capistrano/rbenv
#   https://github.com/capistrano/chruby
#   https://github.com/capistrano/bundler
#   https://github.com/capistrano/rails
#   https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
require 'capistrano/rbenv'

# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# require 'capistrano/passenger'

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

staging.rb/development.rb

デプロイ先の環境ごとの設定ファイル。
ステージング環境をデプロイするのでstaging.rbを修正する。

config/deploy/staging.rb
set :stage, :staging #環境名
#各サーバの役割を記述
role :app, %w{サーバ名} #アプリケーションサーバ
role :web, %w{サーバ名} #webサーバ
role :db,  %w{サーバ名} #DBサーバ
#サーバ情報記述
server 'サーバ名', #サーバ名
user: 'ユーザ名', #実行ユーザ
roles: %w{web app db}, # サーバの役割
ssh_options: {
    keys: %w(SSH接続用の秘密鍵ファイルパス),
    auth_methods: %w(publickey),
    password: 'パスワード'
}

deploy.rb

デプロイ内容を定義するファイル。

config/deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'アプリ名'
set :repo_url, 'gitリポジトリURL'
set :deploy_to, 'デプロイ先パス'
set :pty, true # タスク内でsudoするために必要
set :keep_releases, 3 # 世代数

# デプロイ先サーバのrbenvの設定を追加
set :rbenv_ruby, '2.2.2'
set :rbenv_path, '~/.rbenv'

# nokogiriのインストールで失敗するためサーバインストール済みnokogiriを使用
set :bundle_env_variables, { nokogiri_use_system_libraries: 1 }

デプロイ実行

capコマンドでデプロイを実行する。

$ bundle exec cap staging deploy

デプロイ先のサーバでRails起動コマンドを実行し、ブラウザでアクセスできればデプロイ完了

# デプロイ先ディレクトリのcurrent内で実行
$ bundle exec rails s -e staging -b 0.0.0.0
41
41
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41
41