LoginSignup
23
20

More than 3 years have passed since last update.

【Heroku】デプロイ(1年前の自分へ)| Rails PostgreSQL

Last updated at Posted at 2019-11-15

Railsでアプリを作ってこれからデプロイするところからのスタート!!

とりあえずHerokuにログイン

$ heroku login

// ※windowsの場合
$ heroku login --interactive

Herokuにアプリを作成

$ heroku create

// アプリのURLを変更したい場合
$ heroku create 付けたいアプリのURL

プリコンパイル

参考

$ rails assets:precompile

HerokuのbuildpackにRuby、nodejsのパッケージをセット

$ heroku buildpacks:set heroku/ruby
$ heroku buildpacks:add --index 1 heroku/nodejs

GitHubのmasterにpush

$ git add .
$ git commit -m 'heroku'
$ git push origin master

Herokuへデプロイ!

$ git push heroku master

Herokuでdb:migrate

$ heroku run rails db:migrate

アプリへ飛ぶ!

$ heroku open

完成〜〜〜!!!

初めてデプロイする際に使えるコマンド

対象: テストなどで、適当にデプロイを行いたいとき

$ heroku create && rails assets:precompile && git add . && git commit -m 'heroku deproy' && git push origin master && heroku buildpacks:set heroku/ruby && heroku buildpacks:add --index 1 heroku/nodejs && git push heroku master && heroku run rails db:migrate && heroku open

2回目以降のデプロイ

対象

$ rails assets:precompile && git push heroku master && heroku run rails db:migrate && heroku open

その他

master以外のBranchからHerokuへデプロイする場合

$ git push heroku ブランチ名:master

Herokuのデータベース削除

まず以下のコマンドを実行

$ heroku pg:reset DATABASE

次に、赤文字でそのアプリケーションのDB名が表示され、
このDBを削除してもいいですか? 的なノリで最終確認されるので
そのDB名をコピーしてそのままターミナルに貼り付け、Enter

Herokuのアプリを削除

まず以下を実行して、Herokuに作ったアプリケーションをターミナルに出力

$ heroku apps

次に削除したいアプリケーションを選択し、以下を書き換えて実行

$ heroku apps:destroy --app アプリケーション名 --confirm アプリケーション名

参考
https://qiita.com/chihiro/items/5c3ff400f6cb99deb945

heroku run rake db:migrateができない場合

$ heroku run rake db:migrateで以下のエラーが出る場合の対処

rake aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Herokuにpostgresqlのアドオンを追加して migrat

$ heroku addons:create heroku-postgresql
$ heroku run rake db:migrat

参考
https://qiita.com/suzuki-x/items/b878723080aea1a673ed

Herokuのテーブルを確認する

Herokuのpostgresに接続

$ heroku pg:psql

Herokuのテーブルを出力

herokuのアプリ名::DATABASE-> \d

参考
https://vavolab.com/article/2018/06/05/00/02/01/heroku-postgres%E3%81%A7%E3%83%86%E3%83%BC%E3%83%96%E3%83%AB%E4%BD%9C%E6%88%90%E3%83%BB%E7%A2%BA%E8%AA%8D%E6%96%B9%E6%B3%95/

日本語フォント

$ heroku buildpacks:add --index 1 heroku/nodejs
$ heroku buildpacks:add --index 2 https://github.com/hopkinschris/heroku-buildpack-system-fonts.git
$ heroku buildpacks:set https://github.com/CoffeeAndCode/puppeteer-heroku-buildpack.git

参考
https://pgmemo.tokyo/data/archives/1061.html

Heroku app 削除

使用しなくなったHerokuアプリを削除

Herokuアプリの一覧を表示

$ heroku apps

削除

$ heroku apps:destroy --app アプリ名

参考
https://qiita.com/chihiro/items/5c3ff400f6cb99deb945

23
20
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
23
20