LoginSignup
0

More than 1 year has passed since last update.

Herokuをデプロイ(Rails mysql)

Last updated at Posted at 2022-09-03

要約

herokuでデプロイする方法、その際に詰まったエラーを残していきます。

環境

  • ruby 3.0.3
  • rails 6.1.4
  • heroku 7.60.2
  • mysql 8.0.28

1. Herokuの初期設定

Herokuをbrewにインストールする

terminal
% brew tap heroku/brew && brew install heroku

インストールできたか確認する

% heroku -v
heroku/7.60.2 darwin-x64 node-v14.19.0

Herokuにログイン(サインインする)

% heroku login
heroku: Press any key to open up the browser to login or q to exit:
# なんらかのキーを押すとHerokuのブラウザが開く、qはキャンセルされる
# ブラウザが開いて正常にログインされると
Logging in... done
Logged in as ~~~~

2. Herokuでアプリを作成する

% heroku create [アプリ名]
Creating ⬢ ***... done
アプリのURL | GitのURL

git remote -vでherokuでのgitのURLも追加されている

% git push heroku main

  ~~~~~~~~~~~~
remote: Verifying deploy... done.
To https://git.heroku.com/[アプリ名].git
 * [new branch]      main -> main

HerokuのMysql Creardbを追加する

herokuのアカウントにクレジットカードを登録する。
下記のコマンドを実行する。

% heroku addons:create cleardb:ignite
# igniteはfree

これを実行をするとmysqlで始まるURLが環境変数に追加されている。

% heroku config
CLEARDB_DATABASE_URL:     mysql://~~~~~

今回はmysqlではなくmysql2を使うので冒頭を「mysql」から「mysql2」に変更した環境変数を追加する。

% heroku config:set DATABASE_URL=mysql2://~~~~~
Setting config vars and restarting app-name... done
DATABASE_URL: mysql2://~~~~~~~~~
database.yml
production:
  url: <%= ENV["DATABASE_URL"] %>

Herokuにデプロイする

これでHerokuにアップします。
今回、ここでエラーが起きたので下記に最後に記述します。

% git push heroku main

本番環境にマイグレーションする

% heroku run rake db:migrate

エラーについて

Herokuでログの見方
heroku logs --tail

git push heroku main でのエラー

% git push heroku main 
Counting objects: 100% (3108/3108), done.
Delta compression using up to 8 threads
Compressing objects: 100% (1578/1578), done.
Writing objects: 100% (3108/3108), 614.38 KiB | 6.98 MiB/s, done.
Total 3108 (delta 1831), reused 2156 (delta 1261), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/ruby
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.10
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.3
remote: -----> Installing dependencies using bundler 2.3.10
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:  !     Could not detect rake tasksEnumerating objects: 3108, done.
remote:  !     ensure you can run `$ bundle exec rake -P` against your app
remote:  !     and using the production group of your Gemfile.
remote:  !     rake aborted!
remote:  !     RestClient::BadRequest: 400 Bad Request
〜〜〜
remote:  !     /tmp/build_156d056a/vendor/bundle/ruby/3.0.0/gems/rest-client-2.0.2/lib/restclient.rb:71:in `post'
remote:  !     /tmp/build_156d056a/vendor/bundle/ruby/3.0.0/gems/rspotify-2.11.1/lib/rspotify/connection.rb:31:in `authenticate'
remote:  !     /tmp/build_156d056a/config/application.rb:37:in `<class:Application>'
remote:  !     /tmp/build_156d056a/config/application.rb:24:in `<module:Prefy>'
remote:  !     /tmp/build_156d056a/config/application.rb:22:in `<main>'

となった、とりあえず

ensure you can run `$ bundle exec rake -P` against your app

を実行したが変化せず。。

下記のエラーが発生原因は

  1. bundlerのバージョン違い
  2. 環境変数の未設定

1. bundlerのバージョンをHerokuに合わせる

bundlerのバージョンが2.2.32だったことが一つの原因のようでした

% gem list
~~~
bundler (2.2.32)
~~~

bundlerのバージョンを合わせます。

% gem uninstall bundler -v 2.2.32
% gem install bundler -v 2.3.10
% bundle install
% gem list
~~~
bunlder (2.3.10)
~~~

公式で示されている下記のコマンドをします。
Heroku公式 bundlerの既知の問題について

% bundle lock --add-platform ruby
% bundle lock --add-platform x86_64-linux
% bundle install
Gemfile.lock
~~~
PLATFORMS
  ruby
  x86_64-darwin-21
  x86_64-linux
~~~
BUNDLED WITH
   2.3.10

gitを更新する。

2. 環境変数を設定する

エラーログを確認すると

remote:  !     /tmp/build_156d056a/vendor/bundle/ruby/3.0.0/gems/rest-client-2.0.2/lib/restclient.rb:71:in `post'
remote:  !     /tmp/build_156d056a/vendor/bundle/ruby/3.0.0/gems/rspotify-2.11.1/lib/rspotify/connection.rb:31:in `authenticate'
remote:  !     /tmp/build_156d056a/config/application.rb:37:in `<class:Application>'
remote:  !     /tmp/build_156d056a/config/application.rb:24:in `<module:Prefy>'
remote:  !     /tmp/build_156d056a/config/application.rb:22:in `<main>'

とconfig/applcaiton.rbとなっており、

config/application.rb
RSpotify.authenticate(ENV['SPOTIFY_CLIENT_ID'], ENV['SPOTIFY_SECRET_ID'])

があり、環境変数が影響しているようでした。

Herokuの環境変数を設定

Herokuにログインし、ダッシュボードからアプリを開く。
SettingのConfig Varsのフォームにkeyとvalueを追加する。

環境変数が設定できたか確認する
% heroku config

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