Deploying a Git subdirectory in Capistrano

前端 未结 11 1389
梦谈多话
梦谈多话 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:17

    For Capistrano 3, based on @Thomas Fankhauser answer:

    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"
    

提交回复
热议问题