LoginSignup
0
0

More than 1 year has passed since last update.

【備忘録】rails + Herokuのデプロイの流れ

Last updated at Posted at 2021-06-02

Herokuとは

webアプリケーションを超簡単にデプロイできるプラットフォーム

デプロイの流れ

Heroku

  • Herokuに登録する
  • デバイスにherokuを入れる
ターミナル
 $ brew tap heroku/brew && brew install heroku
  • ターミナル上でログインする
ターミナル
$ heroku login
  • アプリ名を登録する

    • 選択肢1 ターミナル上で行う
    ターミナル
      $ heroku create 好きなアプリ名
    
    • 選択肢2 herokuのページ上で行う スクリーンショット 2021-06-02 15.06.23.png
  • herokuとgitの紐付けをする

ターミナル
    $ heroku git:remote -a リポジトリ名

Rails側の設定

いじるファイル⇩

  • /config/route.rb トップページの設定をしないと, ページがないというエラーが出る.
/config/route.rb
 Rails.application.routes.draw do
   resources :コントローラ名 #基本の7アクションのルーティングをやってくれるやつ(index,show,new,create,update,edit,destroy)
   root 'コントローラ名#index' #トップページを[コントローラ]のindexアクションに設定
 end
  • Gemfile production(本番環境,heroku)ではsqliteが使えないのでPostgreSQL(pg)を使うように書き変える
/Gemfile
# gem sqlite3




group :development do
  ・・・
  gem "sqlite3"
end

group :test do
  ・・・
  gem "sqlite3"
end




group :production do
  gem 'pg'
end

この設定を反映するために以下のコマンドを打つ

$ bundle install --without production
  • /config/database.yml 使うデータベースの種類, encode, 同時接続数を設定し, それ以外はデフォルトに設定
production:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5
  • /config/enbironments/production.rb 本番環境でのプリコンパイルがオフになっている(動的な画像の表示ができない)ので以下の記述をfalseからtrueに変える
/config/enbironments/production.rb
  config.asset.compile = false

git

gitですでに管理されているならば以下のコマンドを打つだけ

ターミナル
$ git push heroku main

なお, 現在作業しているブランチがmainじゃない場合は

$ git push heroku 現在のブランチ名:main

マイグレーション

本番環境のマイグレーションもしましょう

ターミナル
$ heroku run:detached rails db:migrate

更新

$ git add -A
$ git commit -m"コメント"
$ git push heroku main

#テーブル追加, 内容変更があるなら
$ heroku run:detached rails db:migrate

トラブルシューティング

その1

  • git push heroku main でエラーが出る
(前略)
remote:  !
remote:  !     The Ruby version you are trying to install does not exist on this stack.
remote:  !     
remote:  !     You are trying to install ruby-2.6.3 on heroku-20.
remote:  !     
remote:  !     Ruby ruby-2.6.3 is present on the following stacks:
remote:  !     
remote:  !     - cedar-14
remote:  !     - heroku-16
remote:  !     - heroku-18
remote:  !     
remote:  !     Heroku recommends you use the latest supported Ruby version listed here:
remote:  !     https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote:  !     
remote:  !     For more information on syntax for declaring a Ruby version see:
remote:  !     https://devcenter.heroku.com/articles/ruby-versions
remote:  !

(中略)

To https://git.heroku.com/リポジトリ名.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/リポジトリ名.git'

その1の解決策

Ruby ruby-2.6.3 is present on the following stacks:
 - cedar-14
 - heroku-16
 - heroku-18

と書いてあるので

$ heroku stack:set heroku-18 --app アプリ名

Setting stack to heroku-18... done

解決

その2

手元ではうまく動くのに, herokuでは以下のエラーが出る

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation

その2の解決策

herokuコマンドでマイグレーションしてなかった

$ heroku run:detached rails db:migrate

参考

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