0
0

More than 1 year has passed since last update.

Railsプロジェクトの作成からHerokuにデプロイするまで

Last updated at Posted at 2022-01-04

Rails newコマンドによって新しく作成されたアプリケーションをHerokuにデプロイするだけなのですが、ハマったので備忘録として記載します。

かなり基本的な所からの内容になりますが、エラーに振り回されながらでしたので、同じ過ちを繰り返さないように起きたことをそのまま順序立てて書いていきます。

基本的にはRailsチュートリアルの順序通りですが、そのままこなそうとするとエラーになったりするので、工夫します。

開発環境
mac OS バージョン11.6
エディタ
VScode

デスクトップに空のファイルを用意し、VScodeにドラッグ&ドロップ。ファイル名は何でもいいですが、ここではport-forioにしました。

ここからはVScodeのターミナルで操作します。

$ rails new アプリ名

これでRailsプロジェクトが作成されます。ここではアプリ名をsampleとします。

$ cd sample

プロジェクトが作成されたら、まずはコミットしておくといいです(以降に変更を加えたときに、どの部分が変更されたのかわかるため)

$ git init
$ git add .
$ git commit -m "init sample"
$ git branch -M main

次に、ターミナル上でHerokuにログインします。

$ heroku login -i

Enter your Heroku credentials.
Email: Herokuに登録したメールアドレスを入力
Password: Herokuに登録したパスワードを入力

ターミナルでログインした後は、herokuコマンドを利用してHerokuを操作します。

最初にHerokuアプリケーションを作成します。

$ heroku create sample7852(アプリ名)

(※Herokuでのアプリ名であり、ローカルのアプリ名{sample}ではありません。)

指定したアプリ名が既にHerokuアプリケーション名として存在する場合は、以下のようなエラーになり作成できません。

$ heroku create sample
Creating ⬢ sample... !
 ▸    Name sample is already taken

そのため文字列を追加する(例:sample-12345)など、独自の名前をつける必要があります。(なおHerokuアプリケーション名は、小文字、数字、-(ダッシュ)のみを含むことができ、先頭は必ず文字でないとダメです。また、大文字やアンダーバーを含めてしまうとエラーになります)

$ heroku create だけでアプリケーション名を指定しなかった場合は、アプリケーション名がランダムで作成されるのでそれでもOKです。

Herokuアプリケーションが作成されたか確認します。先ほど作成したHerokuアプリケーション名が表示されれば、問題ありません。

$ heroku apps
=== ***@gmail.com Apps
message-task1378
mo-vie
movie1378
movies1267
sample7852

Heroku上に作成されたGitリポジトリがリモートリポジトリ heroku として登録されるので、確認しておきます。

$ git remote -v
heroku  https://git.heroku.com/sample7852.git (fetch)
heroku  https://git.heroku.com/sample7852.git (push)

ここで、データベースについて説明します。

Herokuの標準データベースはPostgreSQLなので、Rails側にPostgreSQLを設定する必要があります。データベースの設定は Gemfile と config/database.yml に記述されています。

Gemfile の最後に追加

group :production do
  gem 'pg', '>= 0.18', '< 2.0'
end

pg は PostgreSQL と Rails を連携するための Gem です。productionという環境において、pg という Gem を使用することを指定しています。

Gemfile を変更したあとは bundle install

config/database.yml
config/database.yml のproduction: 部分が default: では adapter: mysql2 となっているので、production 環境(Heroku)でPostgreSQLに接続できません。なので、以下に書き換えます。

production:
  adapter: postgresql
  encoding: unicode
  pool: 5
  database: sample_production
  username: sample_board
  password: <%= ENV['MESSAGE_BOARD_DATABASE_PASSWORD'] %>

これでHerokuではPostgreSQLへ接続することになります。

$ git add .
$ git commit -m 'sample heroku'

準備が整ったのでデプロイします。

登録されたリモートリポジトリherokuに対して git push heroku を行うとデプロイされます。

$ git push heroku main

→ここでエラーが発生。

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: Verifying deploy...
remote: 
remote: !       Push rejected to sample7852.
remote: 
To https://git.heroku.com/sample7852.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/sample7852.git'

真ん中の方に--add-platform x86_64-linux` and try again.と書いてあります。もう一度やり直してくださいとのことです。なので、

$ bundle lock --add-platform x86_64-linux

上記を実行した後は必ずコミットします。

$ git add -A
$ git commit -m 'init sample'

そしてもう一度 git push heroku mainをします。

そうしたら再度エラーが、、

 An error occurred while installing sqlite3 (1.4.2), and Bundler cannot continue.
remote:        
remote:        In Gemfile:
remote:          sqlite3
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !     Detected sqlite3 gem which is not supported on Heroku:
remote:  !     https://devcenter.heroku.com/articles/sqlite3
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to sample7852.
remote: 
To https://git.heroku.com/sample7852.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/sample7852.git'

要約すると、「sqlite3 (version 1.4.2 )をインストールしている時にエラーが起きたので、bundle installを続けられません。再度bundle installをする前にgem install sqlite3 -v '1.4.2' --source 'https://rubygems.org/' となっているか確かめてください」となります。

Gemfile

group :development, :test do
  gem 'sqlite3', '1.4.2'
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

上記に書き換えて bundle install →再度コミットして、Herokuにデプロイします。
特にエラーが起きずデプロイできればオッケーです。

最後に、自分で設定した Herokuアプリ名 のURLにアクセスして、Herokuアプリを開きます。
https://Herokuアプリ名.herokuapp.com/
(※Herokuアプリ名が sample7852 なら、https://sample7852.herokuapp.com/ になります。)

ただし今の時点でURLを開こうとしても、まだ環境変数などの設定を行っていないのでエラーになります。

なので heroku run コマンド で、マイグレーションを行います。

$ heroku run rails db:migrate

もう一度 https://Herokuアプリ名.herokuapp.com/のURLにアクセスします。エラーが出ずに表示されれば、デプロイ成功です。
(注意:今回のようにrootなどを何も設定していない状態だと、ページを開こうとしても「The page you were looking for doesn't exist.」と出てきます)

ここまででかなり長くなりましたが、もしも冗長な部分や過不足などあればご意見いただけると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0