久々にCircleCIを回したら「Error: Cannot log in with HEROKU_API_KEY set」と言われた。

2019.07.19

久しく触っていなかった転職用ポートフォリオのリファクタリングをして、git push したところ CircleCI が以下のようなエラーを出していた。

 ▸    Invalid credentials
 ▸    provided.
 ›   Error: Cannot log in with HEROKU_API_KEY set
 ▸    Command failed: heroku
 ▸    login

CircleCI で設定していた「HEROKUAPIKEY」が無効であるという内容だ。こちらの原因を調べてみた。

そもそも Heroku の token はどのように取得するか?

CircleCI への環境変数の設定方法については省略するが、Heroku の token は以下のコマンドによって取得することができる。

$ heroku auth:token
 ›   Warning: token will expire 07/13/2020
 ›   Use heroku authorizations:create to generate a long-term token
 #ここに表示される

僕の環境では上記の通りexpire 07/13/2020 と表示されているので、まだ期限を過ぎている訳ではない。 ではなぜ API_KEY が無効になっているのか?

原因:heroku logout を叩いていた。

原因は heroku logout コマンドを叩いていたことだった。

インターンをしていた会社でもインフラとして Heroku を使っていて、アカウントを切り替えて Heroku にログインする機会があり、heroku logout を叩いていた。

Heroku のヘルプページでは、token が無効化されるタイミングとして以下のように書かれている。

In the Heroku Dev Center you'll see the CLI command heroku auth:token. This command works by showing the token that is associated with the account that you are logged into Heroku with. By default, this token expires a year after you log in. This token will also expire if you run heroku logout from the CLI. Furthermore, if you have SSO enabled, this token will expire 8 hours after you login. While this command is appropriate for development, you will want to avoid it for production use. How should I generate an API key that allows me to use the Heroku Platform API?

解消方法

単純に再度 token の発行を行います。

$ heroku auth:token
 ›   Warning: token will expire 07/13/2020
 ›   Use heroku authorizations:create to generate a long-term token

もしくは、以下のコマンドでも同様に token の発行ができるようです。

$ heroku autorizations:create