Capistrano での Git サブディレクトリのデプロイ
-
09-06-2019 - |
質問
私のマスターブランチのレイアウトは次のようになります。
/ <-- トップレベル
/クライアント <-- デスクトップ クライアントのソース ファイル
/サーバ <-- Rails アプリ
私がやりたいのは、/server ディレクトリをプルダウンすることだけです。 deploy.rb
, しかし、それを行う方法が見つからないようです。/client ディレクトリは巨大なので、/server を / にコピーするフックの設定はあまりうまく機能せず、Rails アプリをプルダウンするだけで十分です。
解決
ダーティーなフォークアクションはありませんが、さらにダーティーです。
私の config/deploy.rb では次のようになります。
set :deploy_subdir, "project/subdir"
次に、この新しい戦略を 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 -RPp #{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)
他のヒント
Capistrano 3.0 の場合、次のものを使用します。
私の中で Capfile
:
# Define a new SCM strategy, so we can deploy only a subdirectory of our repo.
module RemoteCacheWithProjectRootStrategy
def test
test! " [ -f #{repo_path}/HEAD ] "
end
def check
test! :git, :'ls-remote', repo_url
end
def clone
git :clone, '--mirror', repo_url, repo_path
end
def update
git :remote, :update
end
def release
git :archive, fetch(:branch), fetch(:project_root), '| tar -x -C', release_path, "--strip=#{fetch(:project_root).count('/')+1}"
end
end
そして私の中では deploy.rb
:
# Set up a strategy to deploy only a project directory (not the whole repo)
set :git_strategy, RemoteCacheWithProjectRootStrategy
set :project_root, 'relative/path/from/your/repo'
重要なコードはすべて戦略の中にあります release
メソッドを使用します。 git archive
リポジトリのサブディレクトリのみをアーカイブするには、 --strip
に対する議論 tar
適切なレベルでアーカイブを抽出します。
アップデート
Capistrano 3.3.3 では、 :repo_tree
構成変数により、この回答は廃止されます。例えば:
set :repo_url, 'https://example.com/your_repo.git'
set :repo_tree, 'relative/path/from/your/repo' # relative path to project root in repo
見る http://capistranorb.com/documentation/getting-started/configuration.
また、Capistrano を使用して、完全なリポジトリのクローンを作成し、未使用のファイルとフォルダーを削除し、目的のフォルダーを階層の上に移動することでこれを実行しています。
デプロイ.rb
set :repository, "git@github.com:name/project.git"
set :branch, "master"
set :subdir, "server"
after "deploy:update_code", "deploy:checkout_subdir"
namespace :deploy do
desc "Checkout subdirectory and delete all the other stuff"
task :checkout_subdir do
run "mv #{current_release}/#{subdir}/ /tmp && rm -rf #{current_release}/* && mv /tmp/#{subdir}/* #{current_release}"
end
end
プロジェクトが大きくなりすぎない限り、これは非常にうまく機能しますが、可能であれば、コンポーネントごとに独自のリポジトリを作成し、それらを git サブモジュールでグループ化します。
2 つの Git リポジトリ (クライアントとサーバー) を用意し、それらを「スーパー プロジェクト」(アプリ) に追加できます。この「スーパープロジェクト」では、2 つのリポジトリをサブモジュールとして追加できます (確認してください) このチュートリアル).
もう 1 つの考えられる解決策 (もう少し汚い) は、クライアントとサーバーに別々のブランチを用意し、「サーバー」ブランチからプルできるようにすることです。
解決策はあります。crdloを取得します カピストラーノ用パッチ そしてその カピストラーノソース ギットハブから。既存の capistrano gem を削除し、パッチを適用し、setup.rb をインストールすると、非常に単純な設定行を使用できるようになります。 set :project, "mysubdirectory"
サブディレクトリを設定します。
唯一の問題は、どうやら github が「アーカイブ コマンドをサポートしていない」ということです...少なくとも彼がそれを書いたときは。私は svn 経由で独自のプライベート git リポジトリを使用していますが、問題なく動作します。github では試していませんが、十分な数の人々が不満を述べれば、その機能を追加するだろうと想像しています。
また、capistrano の作者にこの機能を cap に追加してもらうことができるかどうかも確認してください。 関連するバグで.
Capistrano 3 の場合、@Thomas Fankhauser の回答に基づいて:
set :repository, "git@github.com:name/project.git"
set :branch, "master"
set :subdir, "relative_path_to_my/subdir"
namespace :deploy do
desc "Checkout subdirectory and delete all the other stuff"
task :checkout_subdir do
subdir = fetch(:subdir)
subdir_last_folder = File.basename(subdir)
release_subdir_path = File.join(release_path, subdir)
tmp_base_folder = File.join("/tmp", "capistrano_subdir_hack")
tmp_destination = File.join(tmp_base_folder, subdir_last_folder)
cmd = []
# Settings for my-zsh
# cmd << "unsetopt nomatch && setopt rmstarsilent"
# create temporary folder
cmd << "mkdir -p #{tmp_base_folder}"
# delete previous temporary files
cmd << "rm -rf #{tmp_base_folder}/*"
# move subdir contents to tmp
cmd << "mv #{release_subdir_path}/ #{tmp_destination}"
# delete contents inside release
cmd << "rm -rf #{release_path}/*"
# move subdir contents to release
cmd << "mv #{tmp_destination}/* #{release_path}"
cmd = cmd.join(" && ")
on roles(:app) do
within release_path do
execute cmd
end
end
end
end
after "deploy:updating", "deploy:checkout_subdir"
残念ながら、git にはこれを行う方法がありません。代わりに、「git way」は、クライアントとサーバーの 2 つのリポジトリを用意し、必要なリポジトリのクローンを作成することです。
以前の回答と github で見つかったその他の情報に基づいて、Capistrano 3.x で動作する抜粋を作成しました。
# Usage:
# 1. Drop this file into lib/capistrano/remote_cache_with_project_root_strategy.rb
# 2. Add the following to your Capfile:
# require 'capistrano/git'
# require './lib/capistrano/remote_cache_with_project_root_strategy'
# 3. Add the following to your config/deploy.rb
# set :git_strategy, RemoteCacheWithProjectRootStrategy
# set :project_root, 'subdir/path'
# Define a new SCM strategy, so we can deploy only a subdirectory of our repo.
module RemoteCacheWithProjectRootStrategy
include Capistrano::Git::DefaultStrategy
def test
test! " [ -f #{repo_path}/HEAD ] "
end
def check
test! :git, :'ls-remote -h', repo_url
end
def clone
git :clone, '--mirror', repo_url, repo_path
end
def update
git :remote, :update
end
def release
git :archive, fetch(:branch), fetch(:project_root), '| tar -x -C', release_path, "--strip=#{fetch(:project_root).count('/')+1}"
end
end
Gist としても利用できます。 ギットハブ.
codebasehq.com でも動作しないようなので、混乱を解消する capistrano タスクを作成することになりました :-) 実際には、capistrano タスクをオーバーライドすることで、よりハック性の低い方法があるかもしれません...
これは私にとっては数時間効果がありました。
# Capistrano assumes that the repository root is Rails.root
namespace :uploads do
# We have the Rails application in a subdirectory rails_app
# Capistrano doesn't provide an elegant way to deal with that
# for the git case. (For subversion it is straightforward.)
task :mv_rails_app_dir, :roles => :app do
run "mv #{release_path}/rails_app/* #{release_path}/ "
end
end
before 'deploy:finalize_update', 'uploads:mv_rails_app_dir'
ディレクトリの変数を宣言することもできます (ここでは、rails_app)。
どれくらい頑丈なのか見てみましょう。「before」を使うのはかなり弱いです。