Automate Heroku CLI login - bash

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?

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:
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
Open your ~/.netrc file, or create it, with your favourite text editor
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:
This file is used by many other programs; be careful to only copy the configuration stanzas you want.
Your API key offers full programmatic access to your account. You should protect it as strongly as you protect your password.
Please be very careful if you intend to log into Heroku using any mechanism other than heroku login.

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

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

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.

Related

heroku CLI auth by token

Every time after building and pushing docker image from Gitlab registry to Heroku registry I need to execute heroku container:release web to Heroku run image (release), but I wanna automate this
I added heroku CLI tool installation into gitlab-ci-yml, but I can't auth heroku CLI by token
When I try to set HEROKU_API_KEY=token and run heroku login I get an error Error: Cannot log in with HEROKU_API_KEY set
Also tried to do this with HEROKU_DEBUG on, but debugger info couldn't help me
I can't use ~/.netrc
Any way to auth heroku CLI or automate releasing docker images in heroku?
current gitlab-ci.yml:
before_script:
- apt install snapd
- snap install --classic heroku
- HEROKU_API_KEY=$HEROKU_API_TOKEN heroku login
- docker login -u $REGISTRY_UNAME -p $REGISTRY_PWD registry.gitlab.com
- docker login --username=_ --password=$HEROKU_PWD registry.heroku.com
script:
# a lot of tag & push lines
- heroku container:release web
If you have set the HEROKU_API_KEY environment variable, you don't have to log in again. The API key will be used for the Heroku CLI commands if present.
Make sure to use heroku authorizations:create to create never-expiring tokens. Check this out for a detailed explanation.
Ref: https://github.com/heroku/cli/issues/502#issuecomment-309099883
Note that the git commands like git push heroku master won't use the API key. See this for more info.
problem solved by changing account password that causes tokens changing and re-creating new token
And then run again HEROKU_API_KEY=token heroku container:release web with success

On deploy, Heroku says it can't find API key even though I've logged in

When I run heroku deploy:jar app.jar --app app, I receive this error:
$ heroku deploy:jar build/libs/app.jar --app app
Uploading app.jar
-----> Packaging application...
- app: app
- including: build/libs/app.jar
! ERROR: Could not get API key! Please install the toolbelt and login with `heroku login` or set the HEROKU_API_KEY environment variable.
! Re-run with HEROKU_DEBUG=1 for more info.
▸ There was a problem deploying to app.
▸ Make sure you have permission to deploy by running: heroku apps:info -a app
Here's there environment's information:
On a Mac.
Heroku version: heroku-cli/6.16.12 (darwin-x64) node-v9.11.1
Heroku plugins: heroku-cli-deploy 0.4.0
java version "1.8.0_131"
node version: v4.9.1
I've tried running heroku login multiple times. After it's run, I can run heroku apps:info -a app and it lists the information. I'm inferring that that means I'm logged in already.
My login works on another computer, so I assume it's a local config issue and not an account issue. When I set HEROKU_DEBUG=1, it provides no additional information.
The issue lay in heroku login. It creates a file ~/.netrc containing your login and token.
The format of this generated file on another mac (the mac I mentioned the deploy was working fine) was as follows:
machine api.heroku.com
password ...
login ...
machine git.heroku.com
password ...
login ...
The format of the same generated file on the mac which was giving the above problem was as follows:
machine api.heroku.com login ... password ...
machine git.heroku.com login ... password ...
Apparently, both formats are fine for most Heroku commands such as viewing apps and such. But for the deploy command (which requires the heroku-cli-deploy plugin, only the first format is acceptable.
So basically the heroku cli accepts both new lines and spaces as delimiters between the endpoint, login, and token. But the deploy plugin only accepts new lines. heroku login was using spaces for some reason; so heroku commands were working, while the heroku deploy commands were not.
There would be another reason for the same error in Ubuntu. For some reason in Ubuntu it is easy to make jhipster cli installation that requires sudo while heroku cli don't allow to log in under sudo because browser start doesn't work from the sudo call. And it is uneasy to figure out, what was wrong with jhipster sudo installation, which particular files now owned by the root and so on.
But if you have logged to heroku cli not from the root user (i.e not using sudo) or created api key variable not from root user (i.e not using sudo) your deploy will fail with the error above if you'll request jhipster with sudo:
sudo hipster heroku
if you'll try it without sudo, it will hang on
Installing Heroku CLI deployment plugin
To make it working just run
sudo -E jhipster heroku
and -E will pass your api key variable to the sudo execution

Multiple accounts heroku keys mixed up

I have multiple accounts on heroku and I think my ssh keys got messed up but I have no idea how to fix it.
When i try 'git push heroku master' i get the error 'your account ***** does not have access to *****'
How do I fix my keys so that I can push to heroku?
Heroku wont allow to share ssh key between 2 heroku accounts.
I will help you to get through this by a hacky fix
1.First go to .ssh/ directory in your home folder
2.Move all the files and keep it in any other folder(Don't delete it)
3.Open the terminal and Login to heroku using "heroku login" command and login to your heroku account which you want to push now.
4.After sucessfully logging in to your account enter "heroku keys:add" command. it will create a new ssh keys for your account. you can now push to your account easily.
Suppose if you want to push to any application using your old account replace the .ssh/ folder files to backuped files. (Backup your current .ssh/ to any folder)
or you can repeat same step 1-4 for that account.
I hope this will solve your problem.

Authentication failure in Heroku CLI after password change

After a password change on heroku's website, I can't access the heroku api.
When I do heroku login, I get Authentication successful, even though it shouldn't because the password has changed. It doesn't even ask for my email or password, like it should!
If I try heroku apps (or any other application related command), I get Authentication failure.
If I try heroku status, it works.
I don't have any plugins installed.
Logs:
$ heroku version
heroku-gem/3.6.0 (i686-linux) ruby/2.1.0
$ heroku logout
Local credentials cleared.
$ heroku login
Authentication successful.
$ heroku apps
Authentication failure
$ heroku plugins
You have no installed plugins.
I'll appreciate any help or ideas I can get. I already opened a ticket and waiting for the Heroku Support to get back to me.
Thanks
After a password change on heroku's website, I can't access the heroku api.
Are you using the heroku-accounts plugin? If so, try uninstalling it.
heroku plugins:uninstall heroku-accounts
rm -r ~/.heroku/accounts
heroku logout
heroku login
https://github.com/ddollar/heroku-accounts/issues/56
Apparently, changing the password for heroku regenerates a new API key, and I had the wrong HEROKU_API_KEY configured. (It regenerates a new one after password change).
I'm posting here the response from the great Heroku's support team, which gave me these suggestions:
Is there a ~/.netrc file on your computer? If there is, can you please check if the file contains something Heroku related things? Please remove if there is, and try login again.
Is there an HEROKU_API_KEY environment variable defined on your machine? If yes, could you remove it and login try again?
Is there a ~/.heroku/credentials file (with or without an extension) on your machine? If yes, could you remove it and try login again?
Finally, if none of those work, could try the following command: GEM_HOME='' BUNDLE_GEMFILE='' GEM_PATH='' RUBYOPT='' heroku login and see how it goes?
Are you using the heroku-accounts plugin? If so, try uninstalling it.
You may not need to uninstall it...
Make sure you are not in a project directory (or you may run into the "account not found" loop).
cd
List your accounts.
heroku accounts
On each account where the password was changed
heroku accounts:remove ACCOUNT
heroku accounts:add ACCOUNT
(I did not need to do anything with the ~/.ssh/config.)
Reset your default account, if needed.
heroku accounts:default ACCOUNT
If you changed the account name or git config --unset heroku.account.
cd /path/to/your/project
git config --add heroku.account ACCOUNT
I didn't want to give up on the heroku-accounts plugin. :)
heroku accounts:remove personal or whatever account name you have
git config --unset heroku.account
heroku accounts:add personal then login
In my case, I had multiple heroku accounts managed by heroku-accounts gem and authentication started failing after I changed one of my account's password.
Steps I followed to make things normal again:
Removed the account (that's failing to authenticate) from heroku accounts:
heroku accounts:remove account_name
Opened up a new terminal and then added the account again that I just deleted:
heroku accounts:add account_name --auto
Here you have to use your new credentials.
That's it. Problem solved.
Try deleting ~/.netrc (this is where the Heroku CLI tool saves the auth tokens) and starting over.

How to attach my repo to heroku app

I create a heroku app and then my machine crashed. I have a new machine. How do I attach my existing app to heroku app. When I visit heroku page the url for my app is like this
git#heroku.com:myapp.git
I can't do clone this app because I already have myapp from github. So I need to add heroku as remote to my existing github app. Anyone knows the syntax.
If you've heroku toolbelt:
If you're using the Heroku Toolbelt, the newer syntax is
heroku git:remote -a project
See this for more.
Credits: user101289's solution
Else if you don't have heroku toolbelt:
First do this:
git remote add heroku git#heroku.com:{heroku-app-name}.git
Then do this:
git push heroku master
heroku open
If you're using the Heroku Toolbelt, the newer syntax is
heroku git:remote -a project
See this for more.
If you're using just Git without installing the Heroku Toolbelt, you can also create a new application.
Login to your account and go to this link
https://dashboard.heroku.com/apps
Look at the plus sign on the top right corner then select
Create new app
Leave the application name blank to let heroku choose one for you.
Let say your heroku app name is new-app-xxxxx, so to test on adding a file in to it you may try the following command:
git clone https://git.heroku.com/<new-app-xxxxx>.git
cd <new-app-xxxxx>
echo "my test file" > test.txt
git add .
git commit . -m "my test on commit"
git push
Put empty (blank) when the Git prompt for username, and your API Key for the password. You can get your API Key by showing it from the link below.
https://dashboard.heroku.com/account
Note: You cannot authenticate with the Heroku HTTP Git endpoint using your Heroku username (email) and password. Use an API key as described here.

Resources