75963
新規サーバーで capistorano をはじめて実行するんですが

```

    /usr/bin/env git archive  api | tar -x -C /var/www/api/releases/20210519123116 --strip=1
    
```

の部分で失敗します

おそらく前回デプロイした内容をアーカイブしようとしてるみたいですが  
初回なのに実行されてしまうのは何が悪いのでしょうか

archive api

の間がスペース２個あいてるのでおそらく前回リビジョン名とかがはいるんだと思うんですが  
これがないせいで  
`git stderr: fatal: not a valid object name: api`  
となってしまいます

Capfile

```

    # Load DSL and set up stages
    require 'capistrano/setup'
    
    # Include default deployment tasks
    require 'capistrano/deploy'
    
    # Include tasks from other gems included in your Gemfile
    #
    # For documentation on these, see for example:
    #
    #   https://github.com/capistrano/rvm
    #   https://github.com/capistrano/rbenv
    #   https://github.com/capistrano/chruby
    #   https://github.com/capistrano/bundler
    #   https://github.com/capistrano/rails
    #   https://github.com/capistrano/passenger
    #
    # require 'capistrano/rvm'
    # require 'capistrano/rbenv'
    # require 'capistrano/chruby'
    require 'capistrano/bundler'
    require 'capistrano/rails/assets'
    require 'capistrano/rails/migrations'
    require 'capistrano/rails'
    require 'capistrano/rails/db'
    # require 'capistrano/passenger'
    require 'capistrano3/unicorn'
    
    require 'capistrano/git'
    require "whenever/capistrano"
    
    class Capistrano::Git
      module SubDirectoryStrategy
        include DefaultStrategy
    
        def release
          git :archive, fetch(:branch), fetch(:project_root), '| tar -x -C', release_path, "--strip=#{fetch(:project_root).count('/')+1}"
        end
      end
    end
    
    # Load custom tasks from `lib/capistrano/tasks` if you have any defined
    Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
    
    require 'capistrano/delayed_job'
    
```

deploy.rb

```

    # config valid for current version and patch releases of Capistrano
    lock '3.4.1'
    
    set :application, 'api'
    set :repo_url, 'ssh://xxxxxx.git'
    
    # Default branch is :master
    # ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
    set :branch, ENV['BRANCH'] || "master"
    
    # Default deploy_to directory is /var/www/my_app_name
    # set :deploy_to, '/var/www/my_app_name'
    set :deploy_to, '/var/www/api'
    # set :deploy_via, :remote_cache
    
    set :git_strategy, Capistrano::Git::SubDirectoryStrategy
    set :project_root, 'api'
    
    # Default value for :scm is :git
    set :scm, :git
    # Default value for :format is :pretty
    # set :format, :pretty
    
    # Default value for :log_level is :debug
    set :log_level, :debug
    
    # Default value for :pty is false
    # set :pty, true
    
    # Default value for :linked_files is []
    # set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
    # set :linked_files, fetch(:linked_files, []).push('config/secrets.yml')
    # set :linked_files, fetch(:linked_files, []).push('config/master.key')
    
    # Default value for linked_dirs is []
    set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
    # set :linked_dirs, (fetch(:linked_dirs) + ['tmp'])
    
    # Default value for default_env is {}
    # set :default_env, { path: "/opt/ruby/bin:$PATH" }
    set :default_env, {
      rbenv_root: "/usr/local/rbenv",
      path: "/usr/local/rbenv/shims:/usr/local/rbenv/bin:/home/ec2-user/.rbenv/versions/2.5.1/bin:$PATH"
    }
    
    # Default value for keep_releases is 5
    set :keep_releases, 5
    
    set :unicorn_rack_env, "none"
    set :unicorn_config_path, 'config/unicorn.rb'
    
    # set :whenever_command, "bundle exec whenever"
    set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
    # set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:development)}" }
    # set :whenever_roles, :batch
    # set :whenever_environment, :development
    # set :whenever_roles,        ->{ :db }
    # set :whenever_command,      ->{ [:bundle, :exec, :whenever] }
    # set :whenever_command_environment_variables, ->{ {} }
    # set :whenever_identifier,   ->{ fetch :application }
    # set :whenever_environment,  ->{ fetch :rails_env, fetch(:stage, "development") }
    # set :whenever_variables,    ->{ "environment=#{fetch :whenever_environment}" }
    # set :whenever_update_flags, ->{ "--update-crontab #{fetch :whenever_identifier} --set #{fetch :whenever_variables}" }
    # set :whenever_clear_flags,  ->{ "--clear-crontab #{fetch :whenever_identifier}" }
    
    SSHKit.config.command_map[:rake] = 'bundle exec rake'
    
    after 'deploy:publishing', 'deploy:restart'
    namespace :deploy do
      task :db_seed do
         on roles(:db) do |host|
           within current_path do
             with rails_env: fetch(:rails_env) do
               execute :rake, 'db:seed_fu'
             end
           end
         end
      end
    
      task :start do
        invoke 'unicorn:start'
      end
      task :restart do
        invoke 'unicorn:restart'
      end
      task :stop do
        invoke 'unicorn:stop'
      end
      
      desc "Dump the data to db/data.yml"
      task :dump_data_yml do
        on roles(:db) do |host|
          within current_path do
            with rails_env: fetch(:rails_env) do
              execute :rake, 'db:data:dump'
            end
          end
        end
      end
      desc "Load the seed data from db/data.yml"
      task :load_data_yml do
        on roles(:db) do |host|
          within current_path do
            with rails_env: fetch(:rails_env) do
              execute :rake, 'db:load'
            end
          end
        end
      end
      
    #  after :restart, :clear_cache do
    #    on roles(:web), in: :groups, limit: 3, wait: 10 do
          # Here we can do anything such as:
          # within release_path do
          #   execute :rake, 'cache:clear'
          # end
    #    end
    #  end
    
    end
    
    
```
CC BY-SA 4.0
2021-05-19T12:43:31.630
75948
2021-05-20T03:00:56.847
2021-05-20T02:41:08.467
question
ruby-on-rails ruby
capistorano 初回実行時に git archive で失敗する
Capfile の中に

```

    git :archive, fetch(:branch), fetch(:project_root), '| tar -x -C', release_path, "--strip=#{fetch(:project_root).count('/')+1}"
    
```

というのがあって

```

    set :branch, ENV['BRANCH'] || "master"
    
```

ここで環境変数からブランチ名をうけとるようになっていたようです  
環境変数にブランチをセットしたところこの部分でのエラーは無くなりました
CC BY-SA 4.0
2021-05-20T03:00:56.847
75963
2021-05-20T03:00:56.847
75948
answer