Herokuでのデプロイ

Herokuでのデプロイ

Herokuでのデプロイの際にハマった事があったので、こちらにまとめます
Herokuでデプロイする際に以下のようにエラーになってしまい、プラットフォームが違うとなってしまいました。

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git push heroku master
Enumerating objects: 130, done.
Counting objects: 100% (130/130), done.
Delta compression using up to 8 threads
Compressing objects: 100% (113/113), done.
Writing objects: 100% (130/130), 146.38 KiB | 5.42 MiB/s, done.
Total 130 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
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.2.21
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.0
remote: -----> Installing dependencies using bundler 2.2.21
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.
remote:        Bundler Output: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
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: 7b817d45a55e0f304231fb6d1f156b63a20750c7
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 7b817d45a55e0f304231fb6d1f156b63a20750c7
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 heroku-assignment-higmonta.
remote: 
To https://git.heroku.com/heroku-assignment-higmonta.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/heroku-assignment-higmonta.git'

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % bundle lock --add-platform x86_64-linux
Fetching gem metadata from https://rubygems.org/............
Writing lockfile to /Users/higmonta/workspace/runteq/application_tasks/heroku_deploy/Gemfile.lock

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % bundle                                 
Using rake 13.0.6
Using concurrent-ruby 1.1.9
Using i18n 1.8.10
Using minitest 5.14.4
Using tzinfo 2.0.4
Using zeitwerk 2.4.2
Using activesupport 6.1.4.1
Using builder 3.2.4
Using erubi 1.10.0
Using racc 1.5.2
Using nokogiri 1.12.4 (x86_64-darwin)
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.12.0
Using rails-html-sanitizer 1.4.2
Using actionview 6.1.4.1
Using rack 2.2.3
Using rack-test 1.1.0
Using actionpack 6.1.4.1
Using nio4r 2.5.8
Using websocket-extensions 0.1.5
Using websocket-driver 0.7.5
Using actioncable 6.1.4.1
Using globalid 0.5.2
Using activejob 6.1.4.1
Using activemodel 6.1.4.1
Using activerecord 6.1.4.1
Using marcel 1.0.1
Using mini_mime 1.1.1
Using activestorage 6.1.4.1
Using mail 2.7.1
Using actionmailbox 6.1.4.1
Using actionmailer 6.1.4.1
Using actiontext 6.1.4.1
Using public_suffix 4.0.6
Using addressable 2.8.0
Using bindex 0.8.1
Using msgpack 1.4.2
Using bootsnap 1.8.1
Using bundler 2.2.3
Using byebug 11.1.3
Using regexp_parser 2.1.1
Using xpath 3.2.0
Using capybara 3.35.3
Using childprocess 3.0.0
Using ffi 1.15.4
Using jbuilder 2.11.2
Using rb-fsevent 0.11.0
Using rb-inotify 0.10.1
Using listen 3.7.0
Using method_source 1.0.0
Using puma 5.4.0
Using rack-mini-profiler 2.3.3
Using rack-proxy 0.7.0
Using thor 1.1.0
Using railties 6.1.4.1
Using sprockets 4.0.2
Using sprockets-rails 3.2.2
Using rails 6.1.4.1
Using rubyzip 2.3.2
Using sassc 2.4.0
Using tilt 2.0.10
Using sassc-rails 2.1.2
Using sass-rails 6.0.0
Using selenium-webdriver 3.142.7
Using semantic_range 3.0.0
Using spring 3.0.0
Using sqlite3 1.4.2
Using turbolinks-source 5.2.0
Using turbolinks 5.2.1
Using web-console 4.1.0
Using webdrivers 4.6.1
Using webpacker 5.4.3
Bundle complete! 18 Gemfile dependencies, 73 gems now installed.
Gems in the group production were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git push heroku master                 
Enumerating objects: 130, done.
Counting objects: 100% (130/130), done.
Delta compression using up to 8 threads
Compressing objects: 100% (113/113), done.
Writing objects: 100% (130/130), 146.38 KiB | 6.10 MiB/s, done.
Total 130 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
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.2.21
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.0
remote: -----> Installing dependencies using bundler 2.2.21
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock   ①
remote:        --add-platform x86_64-linux` and try again.
remote:        Bundler Output: Your bundle only supports platforms ["x86_64-darwin-20"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with `bundle lock
remote:        --add-platform x86_64-linux` and try again.
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
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: 7b817d45a55e0f304231fb6d1f156b63a20750c7
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version 7b817d45a55e0f304231fb6d1f156b63a20750c7
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 heroku-assignment-higmonta.
remote: 
To https://git.heroku.com/heroku-assignment-higmonta.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/heroku-assignment-higmonta.git'

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git status  
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   Gemfile.lock

no changes added to commit (use "git add" and/or "git commit -a")

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git add .

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git commit -m "Fix プラットフォームの変更"
[master 1a1a870] Fix プラットフォームの変更
 1 file changed, 3 insertions(+)

higmonta@higuchimiyukiyuunoMacBook-Pro heroku_deploy % git push heroku master                    
Enumerating objects: 133, done.
Counting objects: 100% (133/133), done.
Delta compression using up to 8 threads
Compressing objects: 100% (116/116), done.
Writing objects: 100% (133/133), 147.20 KiB | 4.91 MiB/s, done.
Total 133 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
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.2.21
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.0
remote: -----> Installing dependencies using bundler 2.2.21
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Fetching gem metadata from https://rubygems.org/
remote:        Fetching gem metadata from https://rubygems.org/............
remote:        Fetching rake 13.0.6
remote:        Installing rake 13.0.6
remote:        Fetching concurrent-ruby 1.1.9
remote:        Fetching minitest 5.14.4
remote:        Fetching zeitwerk 2.4.2
remote:        Fetching builder 3.2.4
remote:        Installing builder 3.2.4
remote:        Installing zeitwerk 2.4.2
remote:        Fetching erubi 1.10.0
remote:        Installing minitest 5.14.4
remote:        Installing concurrent-ruby 1.1.9
remote:        Installing erubi 1.10.0
remote:        Fetching racc 1.5.2
remote:        Fetching crass 1.0.6
remote:        Fetching rack 2.2.3
remote:        Installing crass 1.0.6
remote:        Installing racc 1.5.2 with native extensions
remote:        Installing rack 2.2.3
remote:        Fetching nio4r 2.5.8
remote:        Installing nio4r 2.5.8 with native extensions
remote:        Fetching websocket-extensions 0.1.5
remote:        Fetching marcel 1.0.1
remote:        Installing websocket-extensions 0.1.5
remote:        Installing marcel 1.0.1
remote:        Fetching mini_mime 1.1.1
remote:        Installing mini_mime 1.1.1
remote:        Fetching msgpack 1.4.2
remote:        Installing msgpack 1.4.2 with native extensions
remote:        Using bundler 2.2.21
remote:        Fetching ffi 1.15.4
remote:        Installing ffi 1.15.4 with native extensions
remote:        Fetching method_source 1.0.0
remote:        Installing method_source 1.0.0
remote:        Fetching pg 1.2.3
remote:        Installing pg 1.2.3 with native extensions
remote:        Fetching thor 1.1.0
remote:        Installing thor 1.1.0
remote:        Fetching tilt 2.0.10
remote:        Installing tilt 2.0.10
remote:        Fetching semantic_range 3.0.0
remote:        Installing semantic_range 3.0.0
remote:        Fetching turbolinks-source 5.2.0
remote:        Installing turbolinks-source 5.2.0
remote:        Fetching i18n 1.8.10
remote:        Installing i18n 1.8.10
remote:        Fetching tzinfo 2.0.4
remote:        Installing tzinfo 2.0.4
remote:        Fetching rack-test 1.1.0
remote:        Installing rack-test 1.1.0
remote:        Fetching rack-proxy 0.7.0
remote:        Installing rack-proxy 0.7.0
remote:        Fetching sprockets 4.0.2
remote:        Installing sprockets 4.0.2
remote:        Fetching websocket-driver 0.7.5
remote:        Installing websocket-driver 0.7.5 with native extensions
remote:        Fetching mail 2.7.1
remote:        Installing mail 2.7.1
remote:        Fetching nokogiri 1.12.4 (x86_64-linux)
remote:        Installing nokogiri 1.12.4 (x86_64-linux)
remote:        Fetching puma 5.4.0
remote:        Installing puma 5.4.0 with native extensions
remote:        Fetching turbolinks 5.2.1
remote:        Installing turbolinks 5.2.1
remote:        Fetching activesupport 6.1.4.1
remote:        Installing activesupport 6.1.4.1
remote:        Fetching loofah 2.12.0
remote:        Installing loofah 2.12.0
remote:        Fetching bootsnap 1.8.1
remote:        Installing bootsnap 1.8.1 with native extensions
remote:        Fetching rails-dom-testing 2.0.3
remote:        Installing rails-dom-testing 2.0.3
remote:        Fetching globalid 0.5.2
remote:        Installing globalid 0.5.2
remote:        Fetching activemodel 6.1.4.1
remote:        Installing activemodel 6.1.4.1
remote:        Fetching jbuilder 2.11.2
remote:        Installing jbuilder 2.11.2
remote:        Fetching rails-html-sanitizer 1.4.2
remote:        Installing rails-html-sanitizer 1.4.2
remote:        Fetching activejob 6.1.4.1
remote:        Installing activejob 6.1.4.1
remote:        Fetching activerecord 6.1.4.1
remote:        Installing activerecord 6.1.4.1
remote:        Fetching actionview 6.1.4.1
remote:        Installing actionview 6.1.4.1
remote:        Fetching actionpack 6.1.4.1
remote:        Installing actionpack 6.1.4.1
remote:        Fetching actioncable 6.1.4.1
remote:        Installing actioncable 6.1.4.1
remote:        Fetching activestorage 6.1.4.1
remote:        Installing activestorage 6.1.4.1
remote:        Fetching actionmailer 6.1.4.1
remote:        Installing actionmailer 6.1.4.1
remote:        Fetching railties 6.1.4.1
remote:        Installing railties 6.1.4.1
remote:        Fetching sprockets-rails 3.2.2
remote:        Installing sprockets-rails 3.2.2
remote:        Fetching actionmailbox 6.1.4.1
remote:        Installing actionmailbox 6.1.4.1
remote:        Fetching actiontext 6.1.4.1
remote:        Installing actiontext 6.1.4.1
remote:        Fetching webpacker 5.4.3
remote:        Installing webpacker 5.4.3
remote:        Fetching rails 6.1.4.1
remote:        Installing rails 6.1.4.1
remote:        Fetching sassc 2.4.0
remote:        Installing sassc 2.4.0 with native extensions
remote:        Fetching sassc-rails 2.1.2
remote:        Installing sassc-rails 2.1.2
remote:        Fetching sass-rails 6.0.0
remote:        Installing sass-rails 6.0.0
remote:        Bundle complete! 18 Gemfile dependencies, 56 gems now installed.
remote:        Gems in the groups 'development' and 'test' were not installed.
remote:        Bundled gems are installed into `./vendor/bundle`
remote:        Bundle completed (141.95s)
remote:        Cleaning up the bundler cache.
remote: -----> Installing node-v12.16.2-linux-x64
remote: -----> Installing yarn-v1.22.4
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        yarn install v1.22.4
remote:        [1/4] Resolving packages...
remote:        [2/4] Fetching packages...
remote:        info fsevents@2.3.2: The platform "linux" is incompatible with this module.
remote:        info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
remote:        info fsevents@1.2.13: The platform "linux" is incompatible with this module.
remote:        info "fsevents@1.2.13" is an optional dependency and failed compatibility check. Excluding it from installation.
remote:        [3/4] Linking dependencies...
remote:        [4/4] Building fresh packages...
remote:        Done in 23.77s.
remote:        I, [2021-09-17T14:47:38.770279 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js
remote:        I, [2021-09-17T14:47:38.770620 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/manifest-b4bf6e57a53c2bdb55b8998cc94cd00883793c1c37c5e5aea3ef6749b4f6d92b.js.gz
remote:        I, [2021-09-17T14:47:38.770904 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/application-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css
remote:        I, [2021-09-17T14:47:38.771086 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/application-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css.gz
remote:        I, [2021-09-17T14:47:38.771329 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/boards-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
remote:        I, [2021-09-17T14:47:38.771505 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/boards-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css.gz
remote:        I, [2021-09-17T14:47:38.771745 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/scaffolds-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css
remote:        I, [2021-09-17T14:47:38.771933 #1321]  INFO -- : Writing /tmp/build_1c7270dc/public/assets/scaffolds-04c3ae28f07e1bf734223bf526d0cdd296440ef53bcb3f80b9f093c6bf02f747.css.gz
remote:        Compiling...
remote:        Compiled all packs in /tmp/build_1c7270dc/public/packs
remote:        Hash: 0a072893f38dc3f88c56
remote:        Version: webpack 4.46.0
remote:        Time: 5770ms
remote:        Built at: 09/17/2021 2:47:46 PM
remote:                                                Asset       Size  Chunks                         Chunk Names
remote:               js/application-6f8d1c44357d365c38a4.js   69.4 KiB       0  [emitted] [immutable]  application
remote:            js/application-6f8d1c44357d365c38a4.js.br   15.3 KiB          [emitted]              
remote:            js/application-6f8d1c44357d365c38a4.js.gz   17.8 KiB          [emitted]              
remote:           js/application-6f8d1c44357d365c38a4.js.map    205 KiB       0  [emitted] [dev]        application
remote:        js/application-6f8d1c44357d365c38a4.js.map.br   43.9 KiB          [emitted]              
remote:        js/application-6f8d1c44357d365c38a4.js.map.gz   50.9 KiB          [emitted]              
remote:                                        manifest.json  364 bytes          [emitted]              
remote:                                     manifest.json.br  127 bytes          [emitted]              
remote:                                     manifest.json.gz  142 bytes          [emitted]              
remote:        Entrypoint application = js/application-6f8d1c44357d365c38a4.js js/application-6f8d1c44357d365c38a4.js.map
remote:        [0] (webpack)/buildin/module.js 552 bytes {0} [built]
remote:        [4] ./app/javascript/packs/application.js 480 bytes {0} [built]
remote:        [5] ./app/javascript/channels/index.js 205 bytes {0} [built]
remote:        [6] ./app/javascript/channels sync _channel\.js$ 160 bytes {0} [built]
remote:            + 3 hidden modules
remote:        
remote:        Asset precompilation completed (34.23s)
remote:        Cleaning assets
remote:        Running: rake assets:clean
remote: -----> Detecting rails configuration
remote: 
remote: ###### WARNING:
remote: 
remote:        There is a more recent Ruby version available for you to use:
remote:        
remote:        3.0.2
remote:        
remote:        The latest version will include security and bug fixes. We always recommend
remote:        running the latest version of your minor release.
remote:        
remote:        Please upgrade your Ruby version.
remote:        
remote:        For all available Ruby versions see:
remote:          https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote: 
remote: ###### WARNING:
remote: 
remote:        No Procfile detected, using the default web server.
remote:        We recommend explicitly declaring how to boot your server process via a Procfile.
remote:        https://devcenter.heroku.com/articles/ruby-default-web-server
remote: 
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> (none)
remote:        Default types for buildpack -> console, rake, web
remote: 
remote: -----> Compressing...
remote:        Done: 77.6M
remote: -----> Launching...
remote:        Released v6
remote:        https://heroku-assignment-higmonta.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/heroku-assignment-higmonta.git
 * [new branch]      master -> master

①のエラー文より、bundleとlocalでプラットフォームが違うとのことです。
[bundle lock --add-platform x86_64-linux]を実行しろとなっているので、実行し[bundle install]したところ、以下のように[Gemfile.lock]にプラットフォームの記述が追加されました。

Image from Gyazo

[git add .]でステージングして、[git commit]でコミットしてから再度[git push heroku master]でpushするとデプロイができました。

### 参考記事

[Heroku] エラー: ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'Git URL' [Ruby] - Qiita

【Rails】Bundler 2.2.x以降は開発者が適切なプラットフォームを追加する必要がある - AUTOVICE

【Rails】Bundler 2.2.x以降は開発者が適切なプラットフォームを追加する必要がある - AUTOVICE

【Rails6】herokuデプロイエラー時の対処 - Qiita

heroku へクレジットカード登録 – Linux & Android Dialy