LoginSignup
10
11

More than 5 years have passed since last update.

capistrano3で指定したサブディレクトリだけデプロイする方法

Last updated at Posted at 2015-08-22

プロジェクトが以下のような構造になっているとします。

├── client
│   ├── Capfile
│   ├── Gemfile
│   ├── Gemfile.lock
│   ├── c1.rb
│   └── config
│       ├── deploy
│       │   ├── development.rb
│       │   └── staging.rb
│       └── deploy.rb
├── common
│   └── comon1.rb
└── server
    └── s1.rb

このとき、capistranoでclientをデプロイするときに、commonディレクトリも同時にデプロイしたい場合の設定方法を説明します。

client/CapfileにMultiSubDirectoryStrategyを定義

# client/Capfile

require 'capistrano/git'

class Capistrano::Git
  module MultiSubDirectoryStrategy
    include DefaultStrategy

    def release
      git :archive, fetch(:branch), fetch(:deploy_sub_dirs).join(" "), '| tar -x -f - -C', release_path
    end
  end
end

client/config/deploy.rbを編集

# client/config/deploy.rb

set :application, 'client'
set :repo_url, 'https://github.com/k-yamada/capistrano-multi-subdirectory-deploy-sample.git'

set :deploy_to, '/tmp/capistrano-multi/client'
set :git_strategy, Capistrano::Git::MultiSubDirectoryStrategy
set :deploy_sub_dirs, ["client", "common"] # clientとcommonだけデプロイする

set :bundle_gemfile,  "client/Gemfile"

デプロイ実行

$ bundle exec cap development deploy

デプロイ先には以下のように展開されます。

$ tree /tmp/capistrano-multi/client/current
/tmp/capistrano-multi/client/current
├── REVISION
├── client
│   ├── Capfile
│   ├── Gemfile
│   ├── Gemfile.lock
│   ├── c1.rb
│   └── config
│       ├── deploy
│       │   ├── development.rb
│       │   └── staging.rb
│       └── deploy.rb
└── common
    └── comon1.rb

実験用に作ったプロジェクトをgithubに置いておきました。

トラブルシューティング

デプロイ時に「Could not locate Gemfile」エラーが発生する

Solution

config/deploy.rbにGemfileのpathを指定してください。

# config/deploy.rb

set :bundle_gemfile,  "server/Gemfile"

サブディレクトリにあるRailsをデプロイする場合に「assets:precompile」や「unicorn」の起動で失敗する

Problem

サブディレクトリにあるRailsをデプロイする場合に、「capistrano/rails」と「capistrano3/unicorn」がサブディレクトリに対応していないため、デプロイ時にエラーが発生します

Solution

railsのルートディレクトリをオプションで指定できるように、上記のgemを修正したものを作りました。
それを使うにように、Gemfileを修正します

# Gemfile:

group :development do
...
  gem 'capistrano-rails', github: 'k-yamada/capistrano-rails'
  gem 'capistrano3-unicorn', github: 'k-yamada/capistrano3-unicorn', :require => false
end

config/deploy.rbの「rails_root」オプションに、railsのルートディレクトリを指定します。

# config/deploy.rb

set :rails_root, 'server'

その他

railsのルートディレクトリをオプションで指定できるように、修正したgem一覧

10
11
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
10
11