Railsチュートリアル 第1章 1.5 デプロイする

1.5.1 Herokuのセットアップ

・今、作ったアプリがあるのはローカルとGithub上だけなので、このままではユーザーに公開できない。
・公開するためには外部にデプロイする必要がある。Herokuはそのクラウドサービスの名前。
・HerokuはPostgreSQLというデータベースでコンテンツを管理している。そのため、RailsPostgreSQLとの通信に使うgemであるpgを用いる。
・Gemfileを変更し、以下の文言を追加する。

group :production do
  gem "pg", "0.20.0"
end

・次にbundle install --without production で、Gemfile.lockに変更を適用する。pgはローカル環境では使わないので、ローカルにはインストールされないが、こうしておかないと本番環境へのデプロイに失敗する。
・最後にgit commitをしてコミットする。
・次にherokuをインストールする。

$ source <(curl -sL https://cdn.learnenough.com/heroku_install)

AWSにログインし直すたびにherokuコマンドがないと言われるらしい。これはPATHの設定が間違っていることが原因?らしく、.bash_profileというファイルを作成し、以下のように書き込む。

PATH=/usr/local/heroku/bin:$PATH

qiita.com
・インストールが完了したら、IDとパスワードでHerokuにログインする。

$ heroku rogin --interactive
heroku: Enter your login credentials
Email:
Password:

・次に、heroku側にappを入れておくリポジトリ?を作る。

heroku create

・最後に、デプロイする。デプロイはpushすればできる。

git push heroku master

・createしたときにできたURLを開くと、ちゃんとデプロイされているのがわかる。
https://mysterious-ocean-23441.herokuapp.com/

演習

Q1.本番アプリでも"hola, mundo!"を表示する。
A1. ①application_controller.rbにアクションhelloがあることを確認する。
②routes.rbを編集し、helloを実行するように変える。
③git commit -aでコミットし、, git push heroku masterで変更をherokuにデプロイする。

Q2.本番アプリで"goodbye,world!"を表示する。
A2. ①application_controller.rbにアクションgoodbyeがあることを確認する。
②routes.rbを編集し、goodbyeを実行するように変える。
③git commit -aでコミットし、, git push heroku で変更をherokuにデプロイする。(masterを省略しても大丈夫)

1.5.4 Herokuコマンド

・heroku rename A でappの名前を変えられる。
・heroku logs でログが見られる。
・一覧は以下。

VERSION
  heroku/7.39.2 linux-x64 node-v12.13.0

USAGE
  $ heroku [COMMAND]

COMMANDS
  access          manage user access to apps
  addons          tools and services for developing, extending, and operating your app
  apps            manage apps on Heroku
  auth            check 2fa status
  authorizations  OAuth authorizations
  autocomplete    display autocomplete installation instructions
  base
  buildpacks      scripts used to compile apps
  certs           a topic for the ssl plugin
  ci              run an application test suite on Heroku
  clients         OAuth clients on the platform
  config          environment variables of apps
  container       Use containers to build and deploy Heroku apps
  domains         custom domains for apps
  drains          forward logs to syslog or HTTPS
  features        add/remove app features
  git             manage local git repository for app
  help            display help for heroku
  keys            add/remove account ssh keys
  labs            add/remove experimental features
  local           run Heroku app locally
  logs            display recent log output
  maintenance     enable/disable access to app
  members         manage organization members
  notifications   display notifications
  orgs            manage organizations
  pg              manage postgresql databases
  pipelines       manage pipelines
  plugins         list installed plugins
  ps              Client tools for Heroku Exec
  psql            open a psql shell to the database
  redis           manage heroku redis instances
  regions         list available regions for deployment
  releases        display the releases for an app
  reviewapps      manage reviewapps in pipelines
  run             run a one-off process inside a Heroku dyno
  sessions        OAuth sessions
  spaces          manage heroku private spaces
  status          status of the Heroku platform
  teams           manage teams
  update          update the Heroku CLI
  webhooks        list webhooks on an app