Rails Capistrano 任务环境凭据

Rails credentials with environment on Capistrano task

我正在使用 Rails 凭据在我的 Rails 6 应用程序上存储机密数据。有 3 个环境(开发、暂存、生产)。

当我 运行 cap staging deploy:my_task 任务总是使用默认密钥路径 (config/master.key) 而不是 config/environments/staging.key。我试图将它作为变量传递,但它没有用。这是我的任务


task :my_task do
    on roles(:app), in: :sequence, wait: 5 do
      run_locally do
        with rails_env: fetch(:rails_env), rails_master_key: `cat config/credentials/#{fetch(:rails_env)}.key` do
          
          pp Rails.application.credentials
        end
      end
    end
  end

pp Rails.application.credentials 我得到了:

#<ActiveSupport::EncryptedConfiguration:0x00005640fe8cce28
@config={},
@content_path=
 #<Pathname:/builds/path/to/my/project/config/credentials.yml.enc>,
@env_key="RAILS_MASTER_KEY",
@key_file_contents=nil,
@key_path=
 #<Pathname:/builds/path/to/my/project/config/master.key>,
@options={},
@raise_if_missing_key=false>

我错过了什么?

最后还是选择了手动方式。这是我找到的唯一方法。

task :my_task do
    on roles(:app), in: :sequence, wait: 5 do
      run_locally do
        with rails_env: fetch(:rails_env) do
          env_path = "config/credentials/#{fetch(:rails_env)}"
          credentials = Rails.application.encrypted("#{env_path}.yml.enc",
                                          key_path: "#{env_path}.key").config
          pp credentials
      end
    end
  end
end