Deploying a Git subdirectory in Capistrano

前端 未结 11 1392
梦谈多话
梦谈多话 2020-12-07 09:03

My master branch layout is like this:

/ <-- top level

/client <-- desktop client source files

/server

11条回答
  •  遥遥无期
    2020-12-07 09:23

    For Capistrano 3.0, I use the following:

    In my 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
    

    And in my 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'
    

    All the important code is in the strategy release method, which uses git archive to archive only a subdirectory of the repo, then uses the --strip argument to tar to extract the archive at the right level.

    UPDATE

    As of Capistrano 3.3.3, you can now use the :repo_tree configuration variable, which makes this answer obsolete. For example:

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

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

提交回复
热议问题