capistranoのインストール+実行
インストール
そのままインストールする場合
$gem install capistrano $cap -V Capistrano Version: 3.3.5 (Rake Version: 10.4.2)
bundle installする場合はこっち
$rails new capdemo -d mysql $cd capdemo $bundle install --path vendor/bundler
$vim Gemfile ---------------------- gem 'capistrano' ---------------------- $bundle install --path vendor/bundler
設定ファイルの生成
$cap install / $bundle exec cap install ---------------------- # 下記ファイルが生成される # Capfile # config/deploy.rb # config/deploy/ ----------------------
developmentの設定ファイルを作成する
$vim config/deploy/development.rb ---------------------- server 'remote.server.name', user: 'deploy', roles: %w{web} ----------------------
テスト用のtaskを記述してみる
vim config/deploy.rb ---------------------- #全部コメントアウトして下記を追加 desc "do test task" task :test do on roles(:web) do execute "pwd" end end ----------------------
テストタスクの実行
cap -T で実行されるタスクに追加されていることを確認 $cap -T ---------------------- cap test # do test task ---------------------- $cap development test ---------------------- #下記のように出力されれば成功! INFO Finished in 6.986 seconds with exit status 0 (successful). ----------------------
githubからコードをチェックアウトするタスクの作成(最低限の設定ファイルを記述)
#vim config/deploy.rb #全部コメントアウトして下記だけ記述でOK set :application, 'capdemo' set :repo_url, 'git@github.com:user_name/repo_name.git' set :deploy_to, '/var/www/capdemo/' set :pty, true #タスク内でsudoするために必要
実行の確認
$cap -T
実行
$cap development deploy
指定したサーバー側にディレクトリ
current/ releases/ repo/ revisions.log shared/
が構築され、最新のアプリはcurrentからのシンボリックリンクでreleasesに紐づけられている。
ロールバックを試してみる
//1回実行 $cap development deploy $ll (remote側で実行) current -> /var/www/capdemo/releases/20150201000001 releases repo shared //2回目実行 $cap development deploy $ll current -> /var/www/capdemo/releases/20150201000002 releases repo shared //ロールバックする $cap development deploy:rollback $ll (remote側で実行) current -> /var/www/capdemo/releases/20150201000001 releases repo shared rolled-back-release-20150201000002.tar.gz <-戻したファイルが圧縮されている
Gemへ追加
vim Gemfile group :development do gem "capistrano", "3.1.0 gem "capistrano-rails" gem "capistrano-bundler gem "capistrano3-unicorn" end
Capの設定ファイルに追加する
vim Capfile require 'capistrano/bundler' require 'capistrano/rails/assets' require 'capistrano/rails/migrations' require 'capistrano3/unicorn'
rbenv関連のコマンドが動かせるように変更する
vim config/deploy.rb set :default_env,{ rbenv_root: "/usr/local/rbenv", path: "/usr/local/rbenv/shim:/usr/local/rbenv/bin:$PATH" }
config/deploy/development.rbに追記
set :rails_env, :development
unicornの再起動を設定
githubとの接続でerrorになった場合
>cap aborted! >SSHKit::Runner::ExecuteError: Exception while executing as hoge@192.168.0.1: >git exit status: 128 //githubへの設定がOKか確認する $ssh-add -l >Could not open a connection to your authentication agent. //agentを指定 $eval `ssh-agent` $ssh-add id_rsaへのフルパス ($ssh-add ~/.ssh/id_rsa) //githubに接続できるか確認する ssh -T git@github.com >Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.