Gemfile
1. Herokuコマンドの紹介 2 . Herokuのその他機能の紹介
(1) . Herokuコマンドの紹介
Herokuへのデプロイはとてもシンプルです。コマンドを順番に実行するだけでデプロイ ができてしまいます。以下の通りにターミナルへ入力していってください。
(参考:https://devcenter.heroku.com/articles/getting-started-with-rails5)
① Herokuへのログイン
ターミナルからHerokuへログインしておきます。これであとのHerokuコマンドが利用 できるようになります。
username:~/workspace (master) $ heroku login Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Logged in as [email protected]
② アプリの初期設定
app-name
の部分にアプリの名前を入力してください。これは、Herokuのダッシュボー ドで識別するためのもので、herokuが用意するurlにも反映されます。railsのプロジェクト名と同じである必要はありません。自由に名前をつけることができます。もし他のプロ ジェクトと名前が重複している場合はメッセージが出ますので、その場合は違う名前で実行 しなおしてください。
usernamey:~/workspace (master) $ heroku create app-name Creating
(C)app-name... done
https://app-name.herokuapp.com/ | https://git.heroku.com/app-name.git
この中のhttps://app-name.herokuapp.com がこのアプリに割り当てられた URLです。これが終わると、gitのリモートリポジトリとしてのherokuが追加されます。
usernamey:~/workspace (master) $ git remote -v
heroku https://git.heroku.com/app-name.git (fetch)
③ アプリへのコードのデプロイ
デプロイは、gitコマンドでherokuのリモートリポジトリへpushするだけです。push先 のブランチ名は必ずmasterにしてください。
usernamey:~/workspace (master) $ git push heroku master Counting objects: 478, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (440/440), done.
Writing objects: 100% (478/478), 105.43 KiB | 0 bytes/s, done.
Total 478 (delta 207), reused 0 (delta 0) remote: Compressing source files... done.
remote: Building source:
remote:
remote: ---> Ruby app detected remote: ---> Compiling Ruby/Rails
remote: ---> Using Ruby version: ruby-2.2.6
remote: ---> Installing dependencies using bundler 1.13.6
remote: Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment remote: Fetching gem metadata from
https://rubygems.org/...
remote: Fetching version metadata from https://rubygems.org/..
remote: Fetching dependency metadata from
https://rubygems.org/.
remote: Installing i18n 0.7.0 remote: Installing rake 12.0.0 (中略)
remote: ---> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web, worker remote:
remote: ---> Compressing...
remote: Done: 30M remote: ---> Launching...
remote: Released v5
remote: https://app-name.herokuapp.com/ deployed to Heroku remote:
remote: Verifying deploy... done.
To https://git.heroku.com/app-name.git * [new branch] master -> master
④ アプリのデータベース設定
データベースの反映は自動的には行われません。db:migrateと、データ元のseedを 反映させましょう。2回目以降、アプリの修正などでデータベースの反映がなければ、これを 実行する必要はありません。
usernamey:~/workspace (master) $ heroku run rails db:migrate Running rails db:migrate on ● app-name... up, run.4419 (Free)
== 20161210052143 CreateBooks: migrating ======================================
-- create_table(:books) -> 0.0085s
== 20161210052143 CreateBooks: migrated (0.0086s) =============================
== 20161210052334 CreateTags: migrating =======================================
-- create_table(:tags) -> 0.0078s
== 20161210052334 CreateTags: migrated (0.0079s) ==============================
== 20161210052429 CreateTaggings: migrating ===================================
-- create_table(:taggings) -> 0.0227s
== 20161210052429 CreateTaggings: migrated (0.0228s) ==========================
== 20161219102255 DeviseCreateUsers: migrating ================================
-- create_table(:users) -> 0.0114s
-- add_index(:users, :email, {:unique=>true}) -> 0.0078s
-- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0085s
== 20161219102255 DeviseCreateUsers: migrated (0.0280s) =======================
== 20161221085504 AddColumnsToUser: migrating =================================
-- add_column(:users, :name, :string) -> 0.0016s
-- add_column(:users, :role, :string) -> 0.0014s
== 20161221085504 AddColumnsToUser: migrated (0.0033s) ========================
⑤ アプリのデータ反映
開発中に用意したseedデータを反映しましょう。
usernamey:~/workspace (master) $ heroku run rails db:seed Running rails db:seed on ● app-name... up, run.6227 (Free)
これでHeorkuでのデプロイが完成しました。これでHerokuに割り当てられたURLか らアプリにアクセスすることができるかどうか、確認してください。
アプリが確認できたら1stリリース完了です。おめでとうございます!
(2) . Herokuのその他機能の紹介
ここでは、運用、デバッグしていく際によく使うHerokuコマンドをご紹介します。コマンド のオプションなどが変更される可能性もありますので、最新情報はHerokuの開発者向け ホームページを参照ください。
https://devcenter.heroku.com
① 環境変数の一覧
デフォルトでの一覧は以下のとおりです。herokuconfig:setなどで、ここに任意の環 境変数を追加することもできます。詳しくはherokuhelpconfigを見てください。
username:~/workspace (master) $ heroku config
=== app-name Config Vars
DATABASE_URL: postgres://(何かの羅列).amazonaws.com:(何かの羅列) LANG: en_US.UTF-8
RACK_ENV: production RAILS_ENV: production RAILS_LOG_TO_STDOUT: enabled RAILS_SERVE_STATIC_FILES: enabled SECRET_KEY_BASE: (何かの羅列)
② ログ閲覧
デフォルトでは直近から1500行のログが保存されています。それ以上保存したい場合は、
Add-onsで拡張機能を利用してください。もしくは、ブラウザの管理画面でもログを見 ることができます。
https://dashboard.heroku.com/apps/app-name/logs username:~/workspace (master) $ heroku logs
2016-12-24T08:14:44.612193+00:00 app[api]: Initial release by user [email protected] 2016-12-24T08:14:44.841327+00:00 app[api]: Enable Logplex by user [email protected] 2016-12-24T08:14:44.612193+00:00 app[api]: Release v1 created by user [email protected] 2016-12-24T08:14:44.841327+00:00 app[api]: Release v2 created by user [email protected] 2016-12-24T08:16:17.196667+00:00 app[api]: Set LANG, RACK_ENV,
RAILS_ENV, RAILS_LOG_TO_STDOUT, RAILS_SERVE_STATIC_FILES, SECRET_KEY_BASE config vars by user [email protected]
2016-12-24T08:16:17.196667+00:00 app[api]: Release v3 created by user [email protected] 2016-12-24T08:16:18.396611+00:00 app[api]: Release v4 created by user [email protected] 2016-12-24T08:16:18.396611+00:00 app[api]: Attach DATABASE (@
ref:postgresql-rugged-32907) by user [email protected]
2016-12-24T08:16:19.247083+00:00 heroku[slug-compiler]: Slug compilation started 2016-12-24T08:16:19.247093+00:00 heroku[slug-compiler]: Slug compilation finished 2016-12-24T08:16:18.927856+00:00 app[api]: Deploy 69acfa3 by user [email protected] 2016-12-24T08:16:18.927856+00:00 app[api]: Release v5 created by user [email protected] 2016-12-24T08:16:22.171682+00:00 heroku[web.1]: Starting process
with command bin/rails server -p 27764 -e production ....
③ アプリのリスタート
Dyno(Linuxコンテナ)をリスタートします。
username:~/workspace (master) $ heroku restart Restarting dynos on ● app-name... done
④ railsコンソールの起動
Dyno(Linuxコンテナ)でアプリのコンソールを起動します。
usernmae:~/workspace (master) $ heroku run rails console Running rails console on ● app-name... up, run.1384 (Free) Loading production environment (Rails 5.0.0.1)
irb(main):001:0>
⑤ bashの起動
Dyno(Linuxコンテナ)でターミナルを起動します username:~/workspace (master) $ heroku run bash Running bash on ● app-name... up, run.6008 (Free)
~ $ ls
app config db Gemfile.lock log Rakefile README.rdoc test vendor bin config.ru Gemfile lib public README.md spec tmp
⑥ データバックアップ
PostgreSQLを使用した場合のデータのバックアップ方法です。まず、データセットを作 成します。
username:~/workspace (master) $ heroku pg:backups capture Use Ctrl-C at any time to stop monitoring progress; the backup
will continue running. Use heroku pg:backups info to check progress.
Stop a running backup with heroku pg:backups cancel.
DATABASE ---backup---> b001 Backup completed
そうすると、下記のようにIDが付けられたバックアップファイルが作成されます。
username:~/workspace (master) $ heroku pg:backups
=== Backups
ID Backup Time Status Size Database ---- - --- --- ---b001 2016-12-27 07:41:11 +0000 Completed 2016-12-27 07:41:15 +0000 14.8kB DATABASE
=== Restores
No restores found. Use heroku pg:backups restore to restore a backup
=== Copies
No copies found. Use heroku pg:copy to copy a database to another
ロ ー カ ル で herokuコ マ ンド を 使 用 し て い る 場 合、 こ こ で
heroku pg:backups:download
とすれば勝手にダンプファイルのダウンロードができます。しかし、Cloud9では制限されているため、バックアップ保存先のurlを取得し、そこに直接アクセ
username:~/workspace (master) $ heroku pg:backups public-url b001 The following URL will expire at 2016-12-27 09:35:29 +0000:
"https://xxxx.s3.amazonaws.com/xxxxx"
httpsで始まるAWSS3の長いURLが表示されます。そこにアクセスするとダンプファ イルをダウンロードすることができます。
⑦ データリストア
データのリストアでも制限があり、バックアップIDがついたものを直接リストアすることしか できません。ですので、Herokuで運用する場合は、すべてHeroku上で解決する方法が 必要です。リストアのコマンドは以下の通りです。
username:~/workspace (master) $ heroku pg:backups restore b003 DATABASE_URL ! WARNING: Destructive Action
! This command will affect the app: app-name ! To proceed, type "app-name" or re-run this command with --confirm app-name
> app-name
Use Ctrl-C at any time to stop monitoring progress; the backup will continue restoring. Use heroku pg:backups to check progress.
Stop a running restore with heroku pg:backups cancel.
r005 ---restore---> DATABASE Restore completed
restore のあとに、バックアップIDと、環境変数のDATABASE_URLまたは該当のデー
タベースのURLを指定します。ここでのDATABASE_URLは、先のheroku config
で確認できるURLです。Cloud9やHerokuなどのいわゆるクラウドサービスは進化・変化も早く、ここにあげた 内容がすぐに陳腐化する可能性もあります。常に最新の情報を元に開発を進めるようにして ください。