LoginSignup
2
0

More than 3 years have passed since last update.

herokuを使ったデプロイ手順

Last updated at Posted at 2020-05-14

今回はherokuを使ったデプロイ方法について紹介していきます。

手順

0.自分のPCとherokuを繋げて上げる必要があります。(初回のみ)
ホームディレクトリで以下のコマンドを実行しましょう。

$ brew tap heroku/brew && brew install heroku

1.herokuにログインする(herokuのアカウントがない人は下記のURLからアカウントを作成しましょう)
https://signup.heroku.com/

$ heroku login

2.Gemfileを以下のように編集

*DBでsqliteを使用している場合*


group :development do
  <!-- sqliteを使用している場合下記のgemを追加 -->
  gem 'sqlite3'
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

*DBでmysqlを使用している場合*


group :development do
  <!-- mysqlを使用している場合下記のgemを追加 -->
  gem 'mysql2', '>= 0.4.4', '< 0.6.0'
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

アプリ立ち上げ時にDBを指定した際は、初期でgemfileにgemの記述があるので削除かコメントアウトしておきましょう!
Image from Gyazo

下記の記述もGemfileに追加し本番環境に適したパッケージに変更します。


group :production do 
  gem 'pg'
end

3.productionの中以外をアップデートする

$ bundle --without production

4.gitの管理下に置く

$ git init

5.heroku上にアプリ作成(アプリ名は被らないような名前)

$ heroku create アプリ名

6.全てのフォルダやファイルを管理対象にすることを明示

$ git add *

7.現在の状態をコメント付きで保存

$ git commit -m "first commit"

8.現在の状態をherokuに送信

$ git push heroku master

9.Heroku上にデータベース作成
*2回目以降はDBを編集していないかぎり実行する必要はありません*

$ heroku run rails db:migrate

*seeds.rbに記述している場合は下記のコマンドも実行する*

$ heroku run rails db:seed

10.アプリを開く

$ heroku open

デプロイ後にローカルで編集した際には、6の手順から再び実行すると本番環境にも反映されます。

本番環境のDBをリセットする方法は以下の通りです。

$ heroku pg:reset DATABASE_URL

すると以下のように問われる。
本当にDBのリセットしていいの??って聞かれています。
実行ならname-app、再度コマンドから実行なら--confirm name-appを選択します。


     WARNING: Destructive action
     postgresql-round-20524 will lose all of its data
     
     To proceed, type name-app or re-run this command with --confirm name-app

今回はリセットしたいので。

> name-app

あとは、もう一度DBをマイグレートすれば完了。

heroku run rails db:migrate

以上

2
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
2
0