git push heroku masterができないとき【初心者あるある】

#要諦
git push heroku masterでエラーが発生したので
<a href="https://teratail.com/questions/319514">こちら</a>の質問を見て解決しましたという話


#環境
MacOSX
Rails 5.2.4.4
ruby 2.6.3

#困ったこと
```
$ git push heroku master　
```

とした際に以下のエラーが出る

```
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote:  !     Warning: Multiple default buildpacks reported the ability to handle this app. The first buildpack in the list below will be used.
remote:                         Detected buildpacks: Ruby,Node.js
remote:                         See https://devcenter.heroku.com/articles/buildpacks#buildpack-detect-order
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.1.4
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-20/ruby-2.6.3.tgz -s -o - | tar zxf - ' failed on attempt 1 of 3.
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-20/ruby-2.6.3.tgz -s -o - | tar zxf - ' failed on attempt 2 of 3.
remote: 
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.6.3 on heroku-20.
remote:  !     
remote:  !     Ruby ruby-2.6.3 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:  !
remote:  ! ## Warning - The same version of this code has already been built: 05065015816a610c62c1bbdaad0012a617fc2ac7
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 05065015816a610c62c1bbdaad0012a617fc2ac7
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote: 
remote: Verifying deploy...
remote: 
remote: !       Push rejected to secure-lowlands-21453.
remote: 
To https://git.heroku.com/secure-lowlands-21453.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/<アプリ名>.git'
```
最後の２行だけ見ると
いくつかのpushに失敗したらしい

#原因を探る

<a href="https://qiita.com/luglio22/items/cbf989e109cb1d6db1e8">こちら</a>の記事を参考に、

```
$ heroku logs
```

を使ってエラー箇所から原因を探る

①heroku logs から得たエラー

```
Build failed -- check your build output: https://dashboard.heroku.com/apps/4cfee377-22bf-4445-9847-c048ad3202de/activity/builds/e1f39ecc-a3c9-4c8e-a137-584a04662a76
```

heroku logsに出力されたリンクに飛ぶ
→②herokuのダッシュボードに出力されたエラー


```
-----> Building on the Heroku-20 stack
 !     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.1.4
-----> Removing BUNDLED WITH version in the Gemfile.lock
-----> Compiling Ruby/Rails
       Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-20/ruby-2.6.3.tgz -s -o - | tar zxf - ' failed on attempt 1 of 3.
       Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://s3-external-1.amazonaws.com/heroku-buildpack-ruby/heroku-20/ruby-2.6.3.tgz -s -o - | tar zxf - ' failed on attempt 2 of 3.
 !
 !     The Ruby version you are trying to install does not exist on this stack.
 !     
 !     You are trying to install ruby-2.6.3 on heroku-20.
 !     
 !     Ruby ruby-2.6.3 is present on the following stacks:
 !     
 !     - cedar-14
 !     - heroku-16
 !     - heroku-18
 !     
 !     Heroku recommends you use the latest supported Ruby version listed here:
 !     https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
 !     
 !     For more information on syntax for declaring a Ruby version see:
 !     https://devcenter.heroku.com/articles/ruby-versions
 !
 !     Push rejected, failed to compile Ruby app.
 !     Push failed
```

最後の２行からRubyになにか原因があるのと推測する
（元々そうなんじゃないかと思ってはいた）

最後の２行をググった結果
→<a href="https://teratail.com/questions/319514">こちら</a>の質問にたどり着いた

実は①の段階で

```
 !     The Ruby version you are trying to install does not exist on this stack.
 !     
 !     You are trying to install ruby-2.6.3 on heroku-20.
 !     
 !     Ruby ruby-2.6.3 is present on the following stacks:
 !     
 !     - cedar-14
 !     - heroku-16
 !     - heroku-18
 !     
 !     Heroku recommends you use the latest supported Ruby version listed here:
 !     https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
```
Rubyのバージョンがheroku-20の環境では対応していないというのを翻訳サイトで突き止めていたはずなのに、そのことについては華麗にスルーしていた

#解決策

```
$　heroku stack:set heroku-18
```

書いてあるエラーメッセージを読めばわかる通り、ruby-2.6.3はheroku-18に対応している。
そこで、heroku-20からheroku-18に変えてやることによってgit push heroku masterがきちんと成功した

#結論
エラーメッセージはよく読もう