Search code examples
ruby-on-railsherokurake

Authenticating Heroku gem from rake task


I am trying to get my automatic backup script to work. I am using the heroku gem. The script is being run from Heroku's Scheduler addon.

require "heroku/command/pg"
require "heroku/command/pg_backups"

....
def initialize
  @client     = Heroku::Command::Pg.new([], app: ENV['APP_NAME'])
end

...
attachment = @client.send(:generate_resolver)
...

As soon as I call attachment = @client.send(:generate_resolver), the scripts stops execution and ask for

Enter your Heroku credentials.
Email: 

Is there a way to avoid this, given that the task is being initiated from within heroku anyway and therefore should already be authenticated.

Any help is much appreciated


Solution

  • For non-interactive login, you need to use an API key, which Heroku will read from the HEROKU_API_KEY environment variable. You can fetch the key for your account from a logged-in CLI using heroku auth:token.

    There's no way to avoid authentication just because you're running inside Heroku already. That would have significant inter-app security implications.

    Also check out pgbackups-archive. Looks like it does what you're after, or at least might have some useful techniques for you.