0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Capistranoを使ってRailsプロジェクトをデプロイ

Posted at

自動化ツールは一度動き出すと便利なんですが、周辺環境の変化に都度追随するのが大変です。いっそherokuとかのPaaSに移ったほうが良いのかもしれませんが、柔軟性の高さが欲しい場合もあるので、もう少し自力で頑張る方向で。

macOSをCatalinaにアップデートしたら、capコマンドが使えなくなっていたので、改めてgemをインストールします。

sudo gem install capistrano-bundler capistrano-rails

プロジェクト内のCapfileでは以下の3行をコメントアウトします。capistrano/passenger は何故か動かない(再起動に失敗する)のでそのまま無効化しています。

require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
# require "capistrano/passenger"

config/deploy.rb は以下のように使っています(repo_treeやkeep_releasesは状況や好みによって変わると思いますが)。Rails5.2系になって一番の変化は master.key です。これは git 管理しないことが推奨されているので、別の経路でサーバに転送しておく必要があります。sharedフォルダが(currentやreleasesと並んで)存在しているので、その中に設置しておいて、linked_filesの中に加えて上げると、各リリースの配備時にこのファイルのリンクを作ってくれます。

前述したようにPassengerの再起動がうまくいかないので、ここでタスクを定義して配備完了後にApacheを再起動するコマンド(apachectl restart)を実行しています。

set :application, "myapp"
set :repo_url, "git@bitbucket.org:myrepo/myapp.git"
set :repo_tree, 'railsapp'
set :keep_releases, 3

append :linked_files, "config/master.key"

namespace :deploy do
  after :publishing, :restart

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :sudo, 'apachectl restart'
    end
  end
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?