アプリを作ってHerokuにpushをしようとした際に下記エラーが発生しました。
$ git push heroku main
〜 略 〜
remote: !
remote: ! The Ruby version you are trying to install does not exist on this stack.
remote: !
remote: ! You are trying to install ruby-2.5.1 on heroku-20.
remote: !
remote: ! Ruby ruby-2.5.1 is present on the following stacks:
remote: !
remote: ! - cedar-14
remote: ! - heroku-16
remote: ! - heroku-18
remote: !
remote: ! Heroku recommends you use the latest supported Ruby version listed here:
remote: ! https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote: !
remote: ! For more information on syntax for declaring a Ruby version see:
remote: ! https://devcenter.heroku.com/articles/ruby-versions
remote: !
remote: ! Push rejected, failed to compile Ruby app.
remote:
remote: ! Push failed
remote: Verifying deploy...
remote:
remote: ! Push rejected to アプリ名
remote:
To https://git.heroku.com/アプリ名.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/アプリ名.git'
これだけだとエラーの原因がわからなかったため、エラーの詳細を確認するために下記コマンドを実行
heroku logs --tail --app アプリ名
› Warning: heroku update available from 7.51.0 to 7.54.0.
2021-05-26T23:56:16.574357+00:00 app[api]: Initial release by user ~@gmail.com
2021-05-26T23:56:16.574357+00:00 app[api]: Release v1 created by user ~@gmail.com
2021-05-26T23:56:18.425449+00:00 app[api]: Release v2 created by user ~@gmail.com
2021-05-26T23:56:18.425449+00:00 app[api]: Enable Logplex by user ~@gmail.com
2021-05-27T00:07:47.000000+00:00 app[api]: Build started by user ~@gmail.com
2021-05-27T00:07:49.000000+00:00 app[api]: Build failed -- check your build output: https://dashboard.heroku.com/apps/aaa7924-21d4-4200-9690-0db3f8cdd6ab/activity/builds/c5003c56-1cc2-470e-8c4c-6566c2fcb33
ここで一度、明示的にスタックheroku-18を指定して、再度pushを試してみる。
heroku stack:set heroku-18 -a アプリ名
› Warning: heroku update available from 7.51.0 to 7.54.0.
Setting stack to heroku-18... done
エラー解消出来なかったので、今度はRailsのmaster.keyを設定してみる。
heroku config:set RAILS_MASTER_KEY=`cat config/master.key`
これでもまだエラーが解消出来なかった。
そこで今度はHerokuのダッシュボードから該当アプリ名のBuild logを確認する。
(初めに見とけばすぐ解決出来たと後になって理解した)
-----> Building on the Heroku-18 stack
-----> Determining which buildpack to use for this app
! Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
Detected buildpacks: Ruby,Node.js
See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
-----> Ruby app detected
-----> Installing bundler 2.2.16
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rails
-----> Using Ruby version: ruby-2.5.1
-----> Installing dependencies using bundler 2.2.16
Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
Your bundle only supports platforms ["x86_64-darwin-19"] but your local platform
is x86_64-linux. Add the current platform to the lockfile with `bundle lock
--add-platform x86_64-linux` and try again.
Bundler Output: Your bundle only supports platforms ["x86_64-darwin-19"] but your local platform
is x86_64-linux. Add the current platform to the lockfile with `bundle lock
--add-platform x86_64-linux` and try again.
!
! Failed to install gems via Bundler.
!
! Push rejected, failed to compile Ruby app.
! Push failed
ここでようやくエラーの原因がHerokuとローカルでのBundlerのバージョンの違いによって発生していることが分かった。
さらには上記のログから「bundle lock
」するといいよというヒントまであることに気づく。
--add-platform x86_64-linux
ということで下記コマンドを実行。
bundle lock --add-platform x86_64-linux
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
(そしてこれでGemfile.lockが更新されたので、下記コマンドを実行)
git add .
git commit -m "任意のmessage"
git push origin ~ (リモートにpush)
そしてHerokuに再度pushしてみる。
git push heroku main
〜 略 〜
remote: https://アプリ名.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
ようやく無事にHerokuにデプロイが通りました。
お疲れ様でした
参考サイト
herokuへのデプロイでエラーとなった話
HerokuへPushする際に「Failed to install gems via Bundler.」が出た時の対処法
Rails 環境構築が難しい件 エラーが出たらググるしかない