Wdrażanie podkatalogu Git w Capistrano

Mój układ gałęzi master wygląda tak:

/

/client

/Serwer

To, co chciałbym zrobić, to tylko ściągnąć katalog / server w moim deploy.rb, ale nie mogę znaleźć sposobu, aby to zrobić. Katalog / client jest ogromny, więc skonfigurowanie Hooka do kopiowania / serwera do / nie będzie działać zbyt dobrze, wystarczy tylko ściągnąć aplikację Rails.

Author: Charles Menguy, 2008-08-27

10 answers

Bez żadnej brudnej akcji rozwidlenia, ale nawet brudniejsze !

W moim config/deploy.rb:

set :deploy_subdir, "project/subdir"

Potem dodałem tę nową strategię do mojego 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)
 76
Author: thodg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-12 08:18:01

Do Capistrano 3.0 używam:

W moim 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

I w moim 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'

Cały ważny kod znajduje się w metodzie strategy release, która używa git archive do archiwizacji tylko podkatalogu repo, a następnie używa argumentu --strip do tar do rozpakowania archiwum na odpowiednim poziomie.

UPDATE

Począwszy od Capistrano 3.3.3, możesz teraz używać zmiennej konfiguracyjnej :repo_tree, co sprawia, że ta odpowiedź jest przestarzała. Na przykład:

set :repo_url, 'https://example.com/your_repo.git'
set :repo_tree, 'relative/path/from/your/repo' # relative path to project root in repo

Zobacz http://capistranorb.com/documentation/getting-started/configuration .

 42
Author: Mr Friendly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-08 16:09:23

Robimy to również z Capistrano, klonując pełne repozytorium, usuwając nieużywane pliki i foldery oraz przesuwając żądany folder w górę hierarchii.

/ Align = "left" / rb
set :repository,  "[email protected]: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

Tak długo, jak projekt nie będzie zbyt duży, działa to całkiem dobrze dla nas, ale jeśli możesz, utwórz własne repozytorium dla każdego komponentu i pogrupuj je razem z modułami podrzędnymi git.

 10
Author: Thomas Fankhauser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-01-19 15:16:23

Możesz mieć dwa repozytoria git (klienta i serwera) i dodać je do "super-projektu" (aplikacji). W tym "super-projekcie" możesz dodać dwa repozytoria jako moduły podrzędne (sprawdź ten tutorial ).

Innym możliwym rozwiązaniem (nieco bardziej brudnym) jest posiadanie osobnych gałęzi dla klienta i serwera, a następnie można ściągnąć z gałęzi 'serwer'.

 4
Author: Federico Builes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-13 15:17:25

Niestety, git nie zapewnia takiego sposobu. Zamiast tego, 'git way' ma mieć dwa repozytoria-klienta i serwera i klonować te, których potrzebujesz.

 2
Author: Silas Snider,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-27 06:38:13

Jest rozwiązanie. Pobierz łatkę crdlo dla capistrano i źródło Capistrano z Githuba. Usuń istniejący klejnot capistrano, appy patch, setup.RB install, a następnie możesz użyć jego bardzo prostej linii konfiguracyjnej set :project, "mysubdirectory", aby ustawić podkatalog.

Jedyną zaletą jest to, że najwyraźniej github nie "obsługuje polecenia archive"... przynajmniej kiedy to pisał. Używam własnego prywatnego git repo nad svn i działa dobrze, nie próbowałem go z github ale wyobraź sobie, że jeśli wystarczająco dużo osób narzeka, dodadzą tę funkcję.

Sprawdź również, czy możesz zmusić autorów capistrano do dodania tej funkcji do cap przy odpowiednim błędzie .

 2
Author: Simon Woodside,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-14 06:15:09

Dla Capistrano 3, na podstawie @ Thomas Fankhauser odpowiedź:

set :repository,  "[email protected]: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"
 2
Author: fsainz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-22 11:38:56

Wygląda na to, że nie działa również z codebasehq.com więc skończyło się na tworzeniu zadań capistrano, które czyści bałagan : -) może jest rzeczywiście mniej hacky sposób, aby to zrobić, zastępując niektóre zadania capistrano...

 1
Author: StuFF mc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-16 19:27:50

Stworzyłem snipped, który działa z Capistrano 3.x na podstawie poprzednich anwerów i innych informacji znalezionych w GitHubie:

# 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

Jest również dostępny jako Gist na Github .

 1
Author: JAlberto,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-06 15:04:20

To działa dla mnie od kilku godzin.

# 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'

Możesz zadeklarować zmienną dla katalogu (tutaj rails_app).

Zobaczmy, jak wytrzymały jest. Używanie "przed" jest dość słabe.

 0
Author: Stephan Wehner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-06 22:50:37