はじめに
個人開発のアプリ制作を始めました。
調べながらの実装になるので、メモとして記載しています。
※注意※
プログラミング学習中の初心者です。
記述に間違いがあれば、ご指摘いただけますと幸いです。
環境
Mac M1
rails 7.0.7.2
ruby 3.2.0
Node.js 20.2.0
Yarn 1.22.19
やったこと
今回は下記ページを元に進めていきました。
①Herokuアカウント登録
参考HPページを元に、Herokuのアカウント登録から始めました。
記事では2段階認証については記載がありませんでしたが、
下記のアプリをスマホにインストールする必要がありました。
②Heroku CLIをインストールする
Herokuをコマンドライン上から操作するためのツールをインストールします。
後続のデプロイ作業に必要。
$ brew tap heroku/brew && brew install heroku
バージョン情報が表示されれば、OK
$ heroku -v
heroku/8.4.2 darwin-arm64 node-v16.19.0
③ログイン
-iのオプションをつけない場合、下記のように表示されるので、任意のキーということでEnterを押しました。
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
するとブラウザ上にログイン画面が表示されるので、ログイン。
無事にログインできれば、下記が表示されます。
Logged in as メールアドレス
④RailsアプリとHerokuの紐付け
[ 成功例 ]
$ heroku create (好きなアプリ名)
$ heroku create self-growth-diary
Creating ⬢ self-growth-diary... done
(紐づいたURLが表示される)
[ 失敗例 ]
名前は文字で始まり、文字または数字で終わる必要があり、小文字、数字、ダッシュのみ使用可能でした。
$ heroku create self_growth_diary
Creating ⬢ self_growth_diary... !
▸ Name must start with a letter, end with a letter or digit and can only contain lowercase letters, digits, and dashes.
(ちなみにcreateする前にHerokuにクレジットカード紐付けて置く必要がありました。)
⑤Heroku上へのデプロイ
とうとうデプロイ!!
$ git push heroku main
・・・エラー出ました笑
Enumerating objects: 123, done.
Counting objects: 100% (123/123), done.
Delta compression using up to 8 threads
Compressing objects: 100% (97/97), done.
Writing objects: 100% (123/123), 31.74 KiB | 2.44 MiB/s, done.
Total 123 (delta 8), reused 3 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (8/8), done.
remote: Updated 86 paths from a879e69
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-22 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.2.0
remote: -----> Installing dependencies using bundler 2.3.25
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 ["arm64-darwin-21"] but your local platform
remote: is x86_64-linux. Add the current platform to the lockfile with
remote: `bundle lock --add-platform x86_64-linux` and try again.
remote: Bundler Output: Your bundle only supports platforms ["arm64-darwin-21"] but your local platform
remote: is x86_64-linux. Add the current platform to the lockfile with
remote: `bundle lock --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: Verifying deploy...
remote:
remote: ! Push rejected to self-growth-diary.
remote:
To https://git.heroku.com/self-growth-diary.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/self-growth-diary.git'
HerokuはLinux系の環境なので、下記を入れないといけなかったみたいです。
$ bundle lock --add-platform x86_64-linux
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Writing lockfile to /Users/ユーザー名/self_growth_diary/Gemfile.lock
無事インストールできたので、再度$ git push heroku main
。
TOPページ作っていなかったので、ActionController::RoutingError (No route matches [GET] "/"):
が表示されているのをHeroku上で確認できたので一旦完了です。
(View logsをクリックすると表示されます)
余談
...最後RootingErrorに辿り着く前にアプリクラッシュ起こしました😭
原因わからなくて修正するのに数時間かかりました。。。。((´°̥̥̥̥̥̥̥̥ω°̥̥̥̥̥̥̥̥`))
2023-09-11T14:31:52.583004+00:00 heroku[web.1]: Process exited with status 1
2023-09-11T14:31:52.605354+00:00 heroku[web.1]: State changed from starting to crashed
2023-09-11T14:35:22.059383+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=self-growth-diary-4433b37b338e.herokuapp.com request_id=5848f750-68f1-4319-b462-3a5bef6c9919 fwd="126.25.225.107" dyno= connect= service= status=503 bytes= protocol=https
2023-09-11T14:35:22.508367+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=self-growth-diary-4433b37b338e.herokuapp.com request_id=041cc880-d86c-456f-992d-bd66f9d3a9e4 fwd="126.25.225.107" dyno= connect= service= status=503 bytes= protocol=https
結論
database.ymlは標準のままで使う
原因
下記のライブコーディングも参考にしながらやっていたので、database.ymlを下記の通り変更していました。
production:
url: <%= ENV['DATABASE_URL'] %>
色々調べた結果それがローカルの環境変数を私の方ではうまく設定できておらず、結果クラッシュしていた。といった感じでした。
上記の記述のままでの解決方法を探しましたが、泥沼化したので標準に戻しました。
セキュリティ上は上記がいいそうなので、もし解決方法がわかれば修正したいと思います。
$ heroku run rails console
Running rails console on ⬢ self-growth-diary... up, run.2257 (Basic)
/app/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/connection_handler.rb:261:in `resolve_pool_config': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
参考