Ruby on Rails チュートリアル第1章で Heroku へのデプロイに失敗 (2周目)
Ruby on Rails チュートリアル の2周目、最後の Heroku へのデプロイがうまくできなかったため、対処方法をまとめておく。
ビルドエラー
git push heroku master
で Heroku に push するとビルドが開始するが、途中でエラーとなってしまう。発生したエラーの内容は、下記のようになっている。
: remote: Building source: remote: 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: -----> Compiling Ruby/Rails remote: -----> Using Ruby version: ruby-2.4.5 remote: -----> Installing dependencies using bundler 1.15.2 remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment remote: Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`. remote: Fetching gem metadata from https://rubygems.org/......... : remote: Running: rake assets:precompile remote: Yarn executable was not detected in the system. remote: Download Yarn at https://yarnpkg.com/en/docs/install remote: rake aborted! :
上記で、1箇所ワーニングメッセージが出ている。Heroku 上では Bundler のバージョンが 1.15.2 を使用していることを示している。PC 上では 1.17.1 を使用していたため、Gemfile.lock に 1.17.1 が記録されている。これについては、PC 上の Bundler を一旦削除して 1.15.2 を導入することで対応した。
remote: Warning: the running version of Bundler (1.15.2) is older than the version that created the lockfile (1.17.1). We suggest you upgrade to the latest version of Bundler by running `gem install bundler`.
エラーメッセージが出ている。これは node.js でビルドできなければならないことを示しているらしい。
remote: Yarn executable was not detected in the system.
このエラーに対しては、「herokuにアップできません」を参考にして、下記コマンドで対応した。
$ heroku buildpacks:add --index 1 heroku/nodejs
アプリケーションエラー
ビルドのエラーがなくなっても、ブラウザー画面にはアプリケーションエラーが表示される。ログを参照すると、npm start
を実行しようとしている。
$ heroku logs --tail : 2018-12-06T17:01:39.670033+00:00 heroku[web.1]: Starting process with command `npm start` 2018-12-06T17:01:37.789569+00:00 app[api]: Scaled to web@1:Free by user xxxxxxxx@xxxx.com 2018-12-06T17:01:38.000000+00:00 app[api]: Build succeeded 2018-12-06T17:01:37.767042+00:00 app[api]: Release v3 created by user xxxxxxxx@xxxx.com 2018-12-06T17:01:41.658747+00:00 app[web.1]: npm ERR! missing script: start 2018-12-06T17:01:41.665561+00:00 app[web.1]: 2018-12-06T17:01:41.665874+00:00 app[web.1]: npm ERR! A complete log of this run can be found in: 2018-12-06T17:01:41.666017+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2018-12-06T17_01_41_660Z-debug.log 2018-12-06T17:01:41.736904+00:00 heroku[web.1]: State changed from starting to crashed 2018-12-06T17:01:41.738983+00:00 heroku[web.1]: State changed from crashed to starting 2018-12-06T17:01:41.719407+00:00 heroku[web.1]: Process exited with status 1 :
「herokuにアップできません」を参考にして、「Heroku - The Procfile」をProcfile を作成してみたがアプリケーションエラー発生。Profile の内容は下記の通り。
web: bundle exec rails server -p $PORT
ログの内容は下記の通り。
: 2018-12-06T17:17:14.000000+00:00 app[api]: Build succeeded 2018-12-06T17:17:20.202948+00:00 heroku[web.1]: Process exited with status 1 2018-12-06T17:17:20.227931+00:00 heroku[web.1]: State changed from starting to crashed 2018-12-06T17:17:20.230674+00:00 heroku[web.1]: State changed from crashed to starting 2018-12-06T17:17:20.134913+00:00 app[web.1]: /usr/lib/ruby/2.5.0/rubygems.rb:289:in `find_spec_for_exe': Could not find 'bundler' (1.17.1) required by your /app/Gemfile.lock. (Gem::GemNotFoundException) :
一旦、別の Rails アプリを作成して Heroku に新しいリポジトリーを追加し、同様にデプロイしたところサーバーの起動まではうまくいった。
この時点で Heroku のダッシュボードを見ると、エラーが発生しているリポジトリーの Settings -> Framework が Node.js になっている。もう一つのリポジトリーを見ると、Settings -> Framework は Ruby,Node.js になっている。
恐らく heroku buildpacks:add --index 1 heroku/nodejs
が悪さをしていると思われたため、heroku/ruby
も追加したところうまくサーバーの起動まで行った。
アプリケーションエラー
しかし、ブラウザーからアクセスすると、アプリケーションエラーが発生している。ログを参照したところ、下記のエラーが発生している。
: 2018-12-08T15:52:05.876298+00:00 heroku[web.1]: State changed from starting to crashed 2018-12-08T15:52:05.754921+00:00 app[web.1]: /app/vendor/bundle/ruby/2.4.0/gems/activerecord-5.1.6.1/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec': Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError) :
「 Rails アプリを初めて Heroku にデプロイして動かなかったときの対処法 」を参考に config/database.yml を書き換えてデプロイしたところ、エラーは解消して、ブラウザーから期待通りの表示がされることが確認できた。
まとめ
Heroku でのビルドの失敗への対応
- Bundler のバージョンを heroku に合わせる。(不要かもしれない)
$ bundle clean --force $ gem uninstall bundler $ gem install bundler:1.5.2 $ rm Gemfile.lock $ bundle install --without production
- config/database.yml の puroduction のデータベースの設定を SQLite3 から PostgresQL に変更する。
$ vi config/database.yml $ git diff config/database.yml diff --git a/config/database.yml b/config/database.yml index 0d02f24..9ea67d8 100644 --- a/config/database.yml +++ b/config/database.yml @@ -22,4 +22,6 @@ test: production: <<: *default - database: db/production.sqlite3 + adapter: postgresql + encoding: unicode + pool: 5 $
- Heroku のアプリケーションを作り直して、ビルドパックに heroku/nodejs と heroku/ruby を設定する。
$ heroku apps:destroy $ heroku create $ heroku buildpacks:add heroku/nodejs $ heroku buildpacks:add heroku/ruby
- ビルドを実行する。
$ git commit -a -m "retry building on heroku" $ git push heroku master $ heroku logs --tail
以上。