Search code examples
ruby-on-railsgitdeploymentcapistrano

Deploying a Git subdirectory in Capistrano


My master branch layout is like this:

/ <-- top level

/client <-- desktop client source files

/server <-- Rails app

What I'd like to do is only pull down the /server directory in my deploy.rb, but I can't seem to find any way to do that. The /client directory is huge, so setting up a hook to copy /server to / won't work very well, it needs to only pull down the Rails app.


Solution

  • Without any dirty forking action but even dirtier !

    In my config/deploy.rb :

    set :deploy_subdir, "project/subdir"
    

    Then I added this new strategy to my 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)