无法使用 Capistrano 删除旧版本

Unable to delete old releases using Capistrano

我最近将我的 Rails 应用程序从 Ruby 2.7.3 升级到 3.0.1。现在我无法在使用 Capistrano 部署时删除旧版本。我收到以下错误

 DEBUG [f5dff474]       rm: cannot remove '/home/deploy/apps/myapp/releases/20210527195617/tmp/cache/bootsnap/compile-cache/36/9709c0fbdbcd6c': Permission denied

我在 shared/tmp/cache/

上尝试了 chmod -R 777chown $USER

我也试过了this solution,我得到了错误

OptionParser::AmbiguousOption: ambiguous option: -s


Caused by:
OptionParser::InvalidOption: invalid option: s


Caused by:
ArgumentError: wrong number of arguments (given 1, expected 0)

我也试过设置 set :use_sudo, true

config/deploy.rb

# frozen_string_literal: true

# config valid only for current version of Capistrano
# lock '3.13.0'


set :application,     'my-app'
set :user,            'deploy'

set :puma_threads,    [4, 16]
set :puma_workers,    0

set :appsignal_config, name: 'MyApp'
set :appsignal_env, :production
set :appsignal_revision, `git log --pretty=format:'%h' -n 1`

# Don't change these unless you know what you're doing
set :pty,             false # https://github.com/seuros/capistrano-sidekiq#known-issues-with-capistrano-3
set :use_sudo,        true
set :stage,           :production
set :deploy_via,      :remote_cache
set :deploy_to,       "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :keep_assets, 5

## Defaults:
# set :scm,           :git
# set :branch,        :master
# set :format,        :pretty
# set :log_level,     :debug
set :keep_releases, 5

set :rbenv_type, :user
set :rbenv_ruby, '3.0.1'
set :migration_role, :app
# set :bundle_binstubs, -> { shared_path.join('bin') }
# set :rbenv_map_bins, %w{rake gem bundle ruby rails}

## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_files, %w{config/master.key}
set :linked_files, ['.env.production']
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system db/backups/production}

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

task :restart_sidekiq do
  on roles(:worker) do
    execute :service, "sidekiq restart"
  end
end
after "deploy:published", "restart_sidekiq"

namespace :deploy do
  desc 'Make sure local git is in sync with remote.'
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts 'WARNING: HEAD is not the same as origin/master'
        puts 'Run `git push` to sync changes.'
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Run rake yarn:install'
  task :yarn_install do
    on roles(:web) do
      within release_path do
        execute("cd #{release_path} && yarn install")
      end
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
  before "deploy:assets:precompile", "deploy:yarn_install"
end

# ps aux | grep puma    # Get puma pid
# kill -s SIGUSR2 pid   # Restart puma
# kill -s SIGTERM pid   # Stop puma

感觉您的 capistrano 可能与参考答案中的不一样。 您也许可以跳过 -s 参数并尝试:

cap production deploy:cleanup use_sudo=true

请注意,由于 3.x,某些参数(例如 use_sudo)不再是必需的。

您也可以尝试在清理上限前加上 sudo,看看会发生什么:

sudo cap production deploy:cleanup

很可能两者都不起作用,然后我建议查看此文档:upgrading capistrano from 2.xx 更具体地说,第 2 项建议从头开始对项目进行 capify 项目并手动移动旧的 cap 文件。

编辑:您可以使用 sudo 并手动删除有问题的目录吗?

sudo rm -rf /apps/myapp/releases/20210527195617/tmp/cache/bootsnap/compile-cache/36/9709c0fbdbcd6c

如果可行 - 这意味着您需要调整权限,以便允许您的 capistrano 用户删除该文件夹。如果不能这样做,手动清理以前的版本,然后从那里开始用新用户完成的新部署应该可以工作。

运行 sudo chown deploy app/myapp/releases 解决了这个问题。仍然时不时弹出,但这已修复它。

您已经找到适合您的解决方案,但无需先设置正确的所有者/权限即可强制删除的其他方法是将“rm”重新映射到“sudo rm”:

SSHKit.config.command_map[:rm] = 'sudo rm'

所以 capistrano 中的每个“rm”调用都将使用“sudo rm”。当然,您的 capistrano 用户需要无密码的 sudo。