#author("2016-04-22T13:13:49+09:00","default:wikiwriter","wikiwriter")
#author("2020-04-29T03:30:07+00:00","default:wikiwriter","wikiwriter")
&tag(Capistrano3);
*目次 [#h2c8944f]
#contents
*関連ページ [#p990e6ae]
-[[Capistrano]]
-[[./アップグレード]]
-[[./基本]]
-[[capistrano-git-copy]]
-[[./deploy_via_copyする]]
-[[./Tips]]
-[[./トラブルシューティング]]

*参考情報 [#i3b863a7]
-[[capistrano/CHANGELOG.md at master · capistrano/capistrano:https://github.com/capistrano/capistrano/blob/master/CHANGELOG.md]]
-[[Upgrading from v2.x.x:http://capistranorb.com/documentation/upgrading/]]
-[[capistrano 3.x系を使ってrailsをデプロイ | iii ThreeTreesLight:http://threetreeslight.com/post/68344998681/capistrano-3-x-rails]]
-[[capistrano をバージョン 3 にアップデートして時代の流れに乗る - けんごのお屋敷:http://tkengo.github.io/blog/2013/12/12/version-up-capistrano-v3/]] … v2との違いなど
-[[入門 Capistrano 3 ~ 全ての手作業を生まれる前に消し去りたい | GREE Engineers' Blog:http://labs.gree.jp/blog/2013/12/10084/]] … 丁寧な解説。
-Rails用のデプロイツールとしてminaというものもある。しかし依然としてCapistrano3のほうがメジャーな模様。


*Tips [#b1c4e757]

**初回データベースやユーザーの生成をどうするか [#m9c90091]
-[[Capistrano3で快適デプロイ生活!! - Less is Best:http://yss44.hatenablog.com/entry/2013/12/01/150215]]のように別タスクとして保存しておけばいいかもしれない。
#pre{{
    #> dbサーバーのデータベースを生成するタスク。
    #> デプロイ前に実行する必要がある。
    task :db_create do
      on roles(:db) do |host|
        q1 = 'CREATE DATABASE IF NOT EXISTS <app_name>;'
        q2 = 'GRANT ALL ON <app_name>.* TO <app_user>@localhost IDENTIFIED BY "<app_passsword>";'
        q3 = "FLUSH PRIVILEGES;"
        sql = "#{q1}#{q2}#{q3}"
        execute "mysql --user=<mysql_user> --password=<msql_password> -e '#{sql}' "
      end
    end
}}
**passengerを再起動したい [#kc27af13]
-apache passengerはtmp/restart.txtを見て再起動してくれる。
-そこで以下のような設定をdeploy.rbに追加しておくといいらしい。
#pre{{
  namespace :deploy do
    after :restart, :restart_passenger do
      on roles(:web), in: :groups, limit: 3, wait: 10 do
	within release_path do
          execute :touch, 'tmp/restart.txt'
	end
      end
    end
    after :finishing, 'deploy:restart_passenger'
  end
end
}}
-passengerは再起動したあとtmp/restart.txtを削除するという説としないという説がある。
-再起動したかどうかログにも書き出されずよくわからない。

**staging環境で使用したい [#i1307ad0]
-staging環境にdeployしたい場合、config/deploy/staging.rbを指定するだけではだめで、Rails側でstaging環境を設定しておかないといけない。[[Capistrano3を最後にもう一度だけ懇切丁寧にまとめてみる - そのねこが学ぶとき:http://chroju89.hatenablog.jp/entry/2014/04/12/215628]]
-database.ymlにstaging環境を作成する。
-config/environments/staging.rbを作成。production.rbをコピーして作成する。
-stating.rbでrails_envを指定する
 set :rails_env, "staging"
-Gemfileにgroup:stagingを作成(?)
**sharedへのリンク [#mc134145]
-deploy.rbでコメントアウトされているlinkd_dirsをコメントアウトする。
 set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/uploads')
*FAQ [#v91a0669]
**ロールってなに? [#p9dcdd23]
-サーバーごとに役割を設定するもの。[[Rails - はじめてのCapistrano - Qiita:http://qiita.com/mosson/items/1a4cfd01cb538f2d8f0e]]の説明がわかりやすいかも。Capistrano3もCapistrano2も考え方に大差はない。
-Capistranoではタスクは、デフォルトでは全ロール、全サーバーに対して実行できる。タスクに対し例えばwebロールを割り当てると、webロールのサーバーに対してしか処理が行われなくなる。

**設定ファイルの、role、serverの意味ってなに? [#j2801aa6]
-cap installで以下のような設定ファイルが作成される。
#pre{{
role :app, %w{deploy@example.com}
role :web, %w{deploy@example.com}
role :db,  %w{deploy@example.com}


# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.

server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value

}}
-コメントによると、roleは、ロールとサーバーの簡易同時設定。serverはサーバーの詳細設定(既存ロール使用)といったイメージか。

*トラブルシューティング [#h9a9b424]
**デプロイ後にassetsがプリコンパイルされない [#ce91609e]
-Capfileでrequire 'capistrano/rails/assets'が有効になっているかどうか確認する。

**デプロイ後ExecJS and could not find a JavaScript runtimeエラーが発生 [#ta14004a]
-[[ruby on rails 3.1 - ExecJS and could not find a JavaScript runtime - Stack Overflow:http://stackoverflow.com/questions/6282307/execjs-and-could-not-find-a-javascript-runtime]]によると、サーバー環境にNode.jsをインストールする。
#pre{{
sudo apt-get install nodejs
sudo yum install nodejs
}}

**デプロイ後「fatal: Not a git repository (or any of the parent directories): .git」と言われてしまう [#e2931787]
-Gemfileにローカルフォルダで管理しているgemファイルを追加していて、そのgemspecで以下のようにgitを使っているから。
 gem.files         = `git ls-files`.split($\)
-deploy先に.gitフォルダが存在しないためこのような減少が発生する模様。
-ローカルフォルダでgemを管理するのはやめたほうが良いかもしれない。
-もしくはgemspecでgit ls-filesを使わないようにするか。 [[`git ls-files` in gemspec template is bad practice. &#183; Issue #2287 &#183; bundler/bundler:https://github.com/bundler/bundler/issues/2287]]

**「Command: [ -d /var/www/myapp/releases/20140211033611/public/assets ]」のようなわけのわからないエラーが発生 [#ve91d55a]
-[[ruby on rails - Capistrano 3 process failing - Stack Overflow:http://stackoverflow.com/questions/21692601/capistrano-3-process-failing]]によるととりあえず気にしなくても良いのだろうか。


**「Command : [ -L ...]」が exit status 1 (failed).で失敗する [#te9478f2]
-[[ruby - Capistrano 3 deploy failed messages - exit status 1 (failed) - Stack Overflow:http://stackoverflow.com/questions/33128623/capistrano-3-deploy-failed-messages-exit-status-1-failed]]
-致命的エラーならばCapistranoが止まるはずなので気にしなくてよい(のか?)
** Too many authentication failuresが発生 [#af93b281]
-sshをコマンドラインから使うと問題ない。SSHKitの問題?とりあえず以下で一時的に解決できる
 ssh-add -D


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS