20

I'm developing a bash script to automatic clone some projects and another task in dev VM's, but we have one project in Heroku and repository is in it. In my .sh file I have:

> heroku login

And this prompt to enter credentials, I read the "help" guide included on binary and documentation but I can't found anything to automatic insert username and password, I want something like this:

> heroku login -u someUser -p mySecurePassword

Exist any way similar to it?

Cami Rodriguez
  • 1,057
  • 4
  • 14
  • 30
  • Don't use Heroku as your code repository! Heroku currently permits users to clone from its Git interface, but this is [provided as a courtesy](https://devcenter.heroku.com/articles/git-clone-heroku-app). Heroku is _not_ a code repository. It's a platform-as-a-service host. You should use a repository service to host your repository. – ChrisGPT was on strike Sep 19 '17 at 02:34
  • Wow wow, thanks, only I follow their instructions. I have a question.. when I run heroku git:clone -a myapp this command is inside git folder (myapp)? and what is the result? change git url? (sorry for my questions, documentation is so general) – Cami Rodriguez Sep 19 '17 at 03:10
  • Now I'm confused. If you already have a folder `myapp` containing your application there's no point in running `heroku git:clone -a myapp`. What are you trying to accomplish? – ChrisGPT was on strike Sep 19 '17 at 11:41
  • 1
    " What are you trying to accomplish?": I want to login into Heroku without email and password prompt – Cami Rodriguez Sep 22 '17 at 05:46

4 Answers4

25

The Heroku CLI only uses your username and password to retrieve your API key, which it stores in your ~/.netrc file ($HOME\_netrc on Windows).

You can manually retrieve your API key and add it to your ~/.netrc file:

  1. Log into the Heroku web interface
  2. Navigate to your Account settings page
  3. Scroll down to the API Key section and click the Reveal button
  4. Copy your API key
  5. Open your ~/.netrc file, or create it, with your favourite text editor
  6. Add the following content:

    machine api.heroku.com
      login <your-email@address>
      password <your-api-key>
    machine git.heroku.com
      login <your-email@address>
      password <your-api-key>
    

    Replace <your-email@address> with the email address registered with Heroku, and <your-api-key> with the API key you copied from Heroku.

This should manually accomplish what heroku login does automatically. However, I don't recommend this. Running heroku login does the same thing more easily and with fewer opportunities to make a mistake.

If you decide to copy ~/.netrc files between machines or accounts you should be aware of two major caveats:

Please be very careful if you intend to log into Heroku using any mechanism other than heroku login.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
13

You can generate a non-expiring OAuth token then pass it to the CLI via an environment variable. This is useful if you need to run Heroku CLI commands indefinitely from a scheduler and you don't want the login to expire. Do it like this (these are not actual Tokens and IDs, BTW):

$ heroku authorizations:create
Creating OAuth Authorization... done
Client:      <none>
ID:          80fad839-876b-4ea0-a41e-6a9a2fb0cf97
Description: Long-lived user authorization
Scope:       global
Token:       ddf4a0e5-9294-4c5f-8820-b51c52fce4f9
Updated at:  Fri Aug 02 2019 21:26:09 GMT+0100 (British Summer Time) (less than a minute ago)

Get the token (not the ID) from that authorization and pass it to your CLI:

$ HEROKU_API_KEY='ddf4a0e5-9294-4c5f-8820-b51c52fce4f9' heroku run ls --app my-app
Running ls on ⬢ my-app... up, run.2962 (Hobby)
<some file names>
$

By the way this also solves the problem of how to use the Heroku CLI when you have MFA enabled on your Heroku account but your machine doesn't have a web browser e.g., if you are working on an EC2 box via SSH:

$ heroku run ls --app my-app
heroku: Press any key to open up the browser to login or q to exit:
 ›   Error: quit
$ HEROKU_API_KEY='ddf4a0e5-9299-4c5f-8820-b51c52fce4f9' heroku run ls --app my-app
Running ls on ⬢ my-app... up, run.5029 (Hobby)
<some file names>
$

EDIT: For Windows Machines

After you run heroku authorizations:create, copy the "Token", and run the following commands:

set HEROKU_API_KEY=ddf4a0e5-9299-4c5f-8820-b51c52fce4f9
heroku run ls --app my-app
Roger Marlow
  • 349
  • 3
  • 9
1

I agree that Heroku should have by now provided a way to do this with their higher level CLI tool.

You can avoid extreme solutions (and you should, just like Chris mentioned in his answer) by simply using curl and the Heroku API. Heroku allow you to use your API Token (obtainable through your user settings / profile page on the Heroku dashboard).

You can then use the API to achieve whatever it is you wanted to do with their command line tool.

For example, if I wanted to get all config vars for an app I would write a script that did something like the following:

-H "Accept: application/vnd.heroku+json; version=3" \
-H "Authorization: Bearer YOUR_TOKEN```

If *YOUR_APP_NAME* had only one config variable called *my_var* the response of the above call would be 

    { 
        "my_var": some_value
    }

I've found using this all the time in CI tools that need access to *Heroku* information / resources.

joakim
  • 3,533
  • 2
  • 23
  • 28
1

If your goal is just to get the source code, you could use a simple git client. You just need the api key.

Steps to get api key

  • Log into the Heroku web interface
  • Navigate to your Account settings page
  • Scroll down to the API Key section and click the Reveal button
  • Copy your API key

Download source code using git

Use this url template for git clone

https://my_user:my_password@git.heroku.com/name_of_your_app.git

In my case the user value was my email without domain.

Example : 
if mail is **duke@gmail.com**
user for heroku auth will be **duke**

Finally just clone it like any other git repositories:

git clone https://duke:my_password@git.heroku.com/name_of_your_app.git
JRichardsz
  • 14,356
  • 6
  • 59
  • 94