てしりこじり

しりがちいさいエンジニアがいるという

capistrano で サブディレクトリ 内の 特定アプリ をデプロイする

単一リポジトリに複数アプリを格納した案件を手伝ってんですが、
capistrano でデプロイしてると、こういうとき困ります。

偉大な先達方の知恵をお借りして解決した。ほんとありがたい。

リポジトリイメージ

REPOSITORY
├── JAVA_APP-1
├── JAVA_APP-2
├── RUBY_APP
│     ├── RUBY_APP-1
├── JAVA_APP-3



あたし RUBY_APP-1 だけデプロイしたいの!!

対応

Capfile に以下を追記する

require 'capistrano/recipes/deploy/strategy/remote_cache'

class RemoteCacheSubdir < Capistrano::Deploy::Strategy::RemoteCache
  private
  def repository_cache_subdir
    if configuration[:deploy_subdir] then
      File.join(repository_cache, configuration[:deploy_subdir])
    else
      repository_cache
    end
  end

  def copy_repository_cache
    logger.trace "copying the cached version to #{configuration[:release_path]}"
    if copy_exclude.empty? 
      run "cp -RpL #{repository_cache_subdir} #{configuration[:release_path]} && #{mark}"
    else
      exclusions = copy_exclude.map { |e| "--exclude=\"#{e}\"" }.join(' ')
      run "rsync -lrpt #{exclusions} #{repository_cache_subdir}/* #{configuration[:release_path]} && #{mark}"
    end
  end
end

set :strategy, RemoteCacheSubdir.new(self)



deploy.rb に以下を追記する。

set :application,   "hogehoge"
set :scm,           :git
set :repository,    "git@github.com:Project/REPOSITORY.git"
set :deploy_to,     "/var/www/#{application}"
set :deploy_subdir, "RUBY_APP/RUBY_APP-1"    # デプロイしたいディレクトリを設定
set :deploy_via,    :copy

これで deploy_to の下に deploy_subdir で指定したディレクトリのみが
デプロイされ /var/www/hogehoge/RUBY_APP-1 となります。



ほんとありがとうございます。

複数のRailsアプリが同居しているGitリポジトリをデプロイする方法
Deploying a Git subdirectory in Capistrano

あ。 capistrano v2 のお話です。
v3 については試行錯誤中。


Railsデプロイ

Railsデプロイ