heroku / heroku-buildpack-cli

MIT License
42 stars 26 forks source link

Add Instructions On How To Authenticate In A Deploy Step #8

Open alexfinnarn opened 4 years ago

alexfinnarn commented 4 years ago

I'm trying to run a release script to copy a database from a staging environment to a review app.

  # In release script...
  heroku config:set HEROKU_API_KEY=`heroku auth:token`
  heroku pg:copy stage-app-name::$DATABASE_URL $DATABASE_URL -a $HEROKU_APP_NAME --confirm $HEROKU_APP_NAME

But I get authentication errors.

 ›   Error: not logged in
 ›   Error: Missing required flag:
 ›     -a, --app APP  app to run command against
 ›   See more help with --help
 ▸    Invalid credentials provided.
heroku: Press any key to open up the browser to login or q to exit:  ▸    Invalid credentials provided.
Enter your Heroku credentials:
Email: 

So, I'm trying to figure out how to login the best way in a review app environment in order to use the CLI commands during the release phase. Is the recommended way to use a permanemtn machine user? Or to do it in a more ephemeral way each release?

I think I can add a user, store creds in environmental variables, and then use those to login, but I'm not sure of the best option. I figure plenty of other people want to do this or have accomplished it already.

rlueder commented 3 years ago

After spending more time on this than I'm willing to admit I eventually got it working by directly setting HEROKU_API_KEY under my pipeline settings (see attached)

image

On your local terminal (assuming you have already logged into the CLI app with your username/password) run: heroku authorizations:create --short this will create a long-lived token you can use to set HEROKU_API_KEY

You'll also want to wrap your command inside of heroku run e.g. heroku run "heroku pg:copy stage-app-name::$DATABASE_URL $DATABASE_URL -a $HEROKU_APP_NAME --confirm $HEROKU_APP_NAME" -a $HEROKU_APP_NAME

alexfinnarn commented 3 years ago

Thanks @rlueder for the explanation. I can also say I ended up using this method of directly setting HEROKU_API_KEY config var in the pipeline settings and it just works.

This is kind of documentation...and good enough for people searching for the answer, but I am too busy now to formally add to docs. So, whoever monitors these issues can close this one if no one has time to add to docs. I no longer have this issue.