MENU

【備忘録】【rails6】古いRailsアプリを最新の環境でHerokuにデプロイするまで

★環境
 Ruby:3.1.0
 Rails:6.1.4.4

※Railsアプリの旧環境
 Ruby:2.6.3
 Rails:6.0.3.2

目次

エラーが発生した背景

古いRailsアプリをそのままHerokuにデプロイした所、様々なエラーが発生しました。

こちらを解決するまでに対応した事を紹介いたします。

エラーを解消するにあたり、いっその事、RubyやRails、Gemなどのバージョンを動かす事にしまいした。

なお、Railsは7系が最新ですが、先月にリリースされたばかりのため、Rails6.1系の最新版を選択しました。

Herokuへのデプロイエラー

-----> Building on the Heroku-20 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.33
-----> 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:
 !     
 !     - 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
docker-compose up web

web_1 |
web_1 | ERROR: It looks like you're trying to use Nokogiri as a precompiled native gem on a system with glibc < 2.17:
web_1 |
web_1 | /lib/aarch64-linux-gnu/libm.so.6: version GLIBC_2.29' not found (required by /usr/local/bundle/ruby/2.6.0/gems/nokogiri-1.13.1-aarch64-linux/lib/nokogiri/2.6/nokogiri.so) - /usr/local/bundle/ruby/2.6.0/gems/nokogiri-1.13.1-aarch64-linux/lib/nokogiri/2.6/nokogiri.so web_1 | web_1 | If that's the case, then please install Nokogiri via theruby` platform gem:
web_1 | gem install nokogiri --platform=ruby
web_1 | or:
web_1 | bundle config set force_ruby_platform true
web_1 |
web_1 | Please visit https://nokogiri.org/tutorials/installing_nokogiri.html for more help.

<中略>

まずは「Docker」で環境構築

まずはローカルの開発環境で、アプリを起動するため環境を「Docker」で構築しました。

★環境
 Ruby:3.1.0
 Rails:6.1.4.4
 ※各種Gemも最新版へ

しかしいくつかのエラーが次々と発生し四苦八苦しました。

yarnのインストール・アップデート

ログでyarnの警告が出たので、インストールとアップデートしました。

しかしそれだけでは解決しませんでした。

% docker-compose up web
Creating network "dk-r6_bugs-cookthings_default" with the default driver
Creating dk-r6_bugs-cookthings_web_1 ... done
Attaching to dk-r6_bugs-cookthings_web_1
web_1 | => Booting Puma
web_1 | => Rails 6.1.4.4 application starting in development
web_1 | => Run bin/rails server --help for more startup options
web_1 | error Couldn't find an integrity file
web_1 | error Found 1 errors.
web_1 |
web_1 |
web_1 | ========================================
web_1 | Your Yarn packages are out of date!
web_1 | Please run yarn install --check-files to update.
web_1 | ========================================
web_1 |
web_1 |
web_1 | To disable this check, please change check_yarn_integrity
web_1 | to false in your webpacker config file (config/webpacker.yml).
web_1 |
web_1 |
web_1 | yarn check v1.22.17
web_1 | info Visit https://yarnpkg.com/en/docs/cli/check for documentation about this command.
web_1 |
web_1 |
web_1 | Exiting
dk-r6_bugs-cookthings_web_1 exited with code 1
yarn install --check-files

yarn upgrade

webpackerのバージョンアップ

「Webpacker::Manifest::MissingEntryError」エラーが発生したため、webpackerのバージョンアップを実施しました。

#gem 'webpacker', '~> 4.0'
gem 'webpacker', '~> 5.0'

railsインストールをした際に、古いファイルの設定なども上書きされた。

必要に応じてGitから上書きされてはまずい設定は差し戻す。

rails webpacker:install

後はDBのマイグレートを実施して、Docker環境で動作確認できました!

docker-compose run --rm web rake db:migrate

しかしDockerで動作したものの、Herokuではデプロイエラーが解消されず・・・

<参考>

Qiita
error /node_modules/node-sass: Command failedの解決策 - Qiita タイトルの通り、/node_modules/node-sass: Command failedのエラーが発生し、うまくWebpackが動作せずWebpacker::Manifest::Missing…
Qiita
[Rails] rails コマンドの際のyarnエラー yarn.lockで不整合が起きてる場合に起こる - Qiita rails sの際にエラーが起きました$ rails s=> Booting Puma=> Rails 6.0.0.rc1 application starting in developmen…
Qiita
Rails6 開発時につまづきそうな webpacker, yarn 関係のエラーと解決方法 - Qiita はじめにRails6でwebpackerが標準になったことにより、Railsアプリの開発環境にyarnのインストールが必要になりました。webpacker,yarnがインストールされてない環境で…

「Heroku」環境で動作するように対応

Herokuでのエラー発生の一つに、Herokuでは「Bundler」のバージョンが2.1.4(もしくは2.2.22)まででないと動かない、つまり2.1.4(もしくは2.2.22)より大きいバージョンでは現時点で動かないという記事がありました。

ただ、これが絶対であるという確証がなかったのと、あるいは特定のパッケージを使う上での現象かもしれません。

そして遂に最新版の「Bundler」2.3.3で動作確認できました。

今回発生したHerokuでのエラーです。

2022-01-21T04:18:25.052971+00:00 heroku[web.1]: State changed from crashed to starting
2022-01-21T04:18:32.773018+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2022-01-21T04:18:35.241153+00:00 app[web.1]: => Booting Puma
2022-01-21T04:18:35.241176+00:00 app[web.1]: => Rails 6.1.4.4 application starting in production
2022-01-21T04:18:35.241177+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2022-01-21T04:18:35.605349+00:00 app[web.1]: Exiting
2022-01-21T04:18:35.611288+00:00 app[web.1]: /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.10.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:33:in `require': cannot load such file -- net/smtp (LoadError)
2022-01-21T04:18:35.611291+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/zeitwerk-2.5.3/lib/zeitwerk/kernel.rb:35:in `require'
2022-01-21T04:18:35.611293+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail.rb:9:in `<module:Mail>'
2022-01-21T04:18:35.611302+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/mail-2.7.1/lib/mail.rb:3:in `<main>'

これはRuby3.1.0の場合、以下のGemが必要との事なので、Gemfileに加えました。

gem "net-smtp", require: false
gem "net-pop", require: false
gem "net-imap", require: false

これにより、遂にHeroku上でも動作しまいした!

Bundlerも2.3.3で動作確認できました。

bundle -v
Bundler version 2.3.3

<参考>

あわせて読みたい
library net/imap (Ruby 3.3 リファレンスマニュアル) このライブラリは Internet Message Access Protocol (IMAP) の クライアントライブラリです。[[RFC:2060]] を元に 実装されています。
Stack Overflow
Rails 7 Ruby 3.1 LoadError: cannot load such file -- net/smtp I upgraded to Rails 7 and Ruby 3.1. While trying to run tests with rspec I got the error below. How can I fix it? An error occurred while loading rails_helper. ...

Dockerファイル

docker-compose.yml

version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8"
      PGPASSWORD: postgres123
      POSTGRES_DB: cookthings_development
      TZ: "Asia/Tokyo"
#    restart: always
    ports:
      - "5432:5432"
    user: root
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - ../../github/rails-app/:/myapp
      - bundle_path:/bundle
    ports:
      - "3000:3000"
volumes:
  bundle_path:
#    depends_on:
#     - db

Dockerfile

FROM ruby:3.1.0
RUN wget --quiet -O - /tmp/pubkey.gpg https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \
    && apt-get update -qq \
    && apt-get install -y nodejs yarn postgresql-client \
    && mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

RUN bundle install
RUN bundle update

RUN yarn install --check-files
RUN yarn upgrade

COPY . /myapp

RUN bundle exec rails webpacker:install

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次