Heroku スターターガイド (Rails 7.x)
最終更新日 2022年03月29日(火)
Table of Contents
Ruby on Rails は、Ruby で記述され、広く使われている Web フレームワークです。このガイドでは、Heroku での Rails 7 の使用について説明します。Heroku での旧バージョンの Rails の実行については、Rails 6.x または Rails 5.x に関するチュートリアルを参照してください。
続行する前に、次のものを用意しておくと役立ちます。
- Ruby、Ruby on Rails、および Git に関する基本的な知識
- Ruby 2.7.0+、Rubygems、Bundler、Rails 7 のローカルにインストールされたバージョン+
- Heroku ユーザーアカウント: 無料ですぐにサインアップできます。
- Heroku CLI のローカルにインストールされたバージョン
ローカルの設定
Heroku CLI がインストールされていると、heroku
はターミナルで使用可能なコマンドになっています。CLI を使用して Heroku にログインします。
$ heroku login
heroku: Enter your Heroku credentials
Email: schneems@example.com
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
プロンプトで Enter キーを押して既存の ssh
キーをアップロードするか、新しいキーを作成します。
情報 2021 年 11 月 30 日以降、Heroku では SSH Git トランスポートがサポートされなくなります。SSH キーは、Heroku プラットフォーム上のアプリケーションにコードをプッシュする目的には対応しません。
新しい Rails アプリの作成または既存の Rails アプリのアップグレード
アプリを作成する前に、rails -v
を使用して Rails 7 がインストールされていることを確認します。必要に応じて、gem install
を使用して Rails 7 をインストールします。
$ gem install rails --no-document --pre
Successfully installed rails-7.0.0.alpha2
1 gem installed
アプリを作成し、そのルートディレクトリに移動します。
$ rails new myapp --database=postgresql
アプリケーション内に直接移動し、Gemfile.lock
に x86_64-linux
および ruby
プラットフォームを追加します。
$ cd myapp
$ bundle lock --add-platform x86_64-linux --add-platform ruby
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Writing lockfile to ./myapp/Gemfile.lock
ローカルデータベースを作成します。
$ bin/rails db:create
Created database 'myapp_development'
Created database 'myapp_test'
pg gem の追加
--database=postgresql
が定義されていなかった新規または既存のアプリの場合は、Gemfile
内に sqlite3
gem が存在しないことを確認します。pg
gem を所定の位置に追加します。
Gemfile
内で、次の行を削除します。
gem 'sqlite3'
次に、それを次の行に置き換えます。
gem 'pg'
開発中は PostgreSQL をローカルで使用することを強くお勧めします。開発環境とデプロイ環境の間の同等性を維持することにより、これらの環境の違いのために導入される微妙なバグが防止されます。
ここで Postgres をローカルにインストールしてください (まだシステムにない場合)。Sqlite3 の代わりに Postgres が推奨される理由についての詳細は、Sqlite3 が Heroku と互換性がない理由を参照してください。
Gemfile
が更新されたら、依存関係を再インストールします。
$ bundle install
それを行うと、前に加えられた変更を使用して Gemfile.lock
が更新されます。
pg
gem の使用に加えて、config/database.yml
で postgresql
アダプタが定義されていることを確認します。config/database.yml
ファイルの開発セクションは次のようになります。
$ cat config/database.yml
# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
#username: myapp
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: myapp_test
# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV["MYAPP_DATABASE_PASSWORD"] %>
ここで注意点があります。adapter
の値が postgresql
ではなく、postgres
である場合 (最後にある sql
に注意してください)、アプリケーションは機能しません。
ウェルカムページの作成
Rails 7 の本番環境には、デフォルトで静的なインデックスページがありません。Rails 7 にアップグレードされたアプリでは既存のページ設定が保持されますが、新しい Rails 7 アプリには、自動的に生成されるウェルカムページがありません。ホームページを保持するための welcome
コントローラーを作成します。
$ rails generate controller welcome
app/views/welcome/index.html.erb
を作成し、次のスニペットを追加します。
ファイル app/views/welcome/index.html.erb
で、以下のように記述します。
<h2>Hello World</h2>
<p>
The time is now: <%= Time.now %>
</p>
ウェルカムページが作成されたら、このアクションにマッピングするためのルートを作成します。config/routes.rb
を編集して、インデックスページを新しいメソッドに設定します。
ファイル config/routes.rb
の 2 行目に以下を追加します。
root 'welcome#index'
Rails Web サーバーを起動して、ページが表示されることを確認します。
$ rails server
ブラウザで http://localhost:3000 にアクセスします。ページが表示されない場合は、rails server
が起動したのと同じターミナル内で Rails が出力したログを参照して、エラーをデバッグします。
Heroku gem
旧バージョンの Rails (Rails 4 以前) では、rails_12factor gem を使用して Heroku で静的アセットの提供やログ記録を有効にする必要がありました。新しい Rails アプリケーションでは、この gem は不要です。config/environments/production.rb
内に次のコードが存在する場合は、この gem を既存のアップグレードされたアプリケーションから削除できます。
# config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Ruby バージョンの指定
Rails 7 では、Ruby 2.7.0 以上が必要です。Heroku は、デフォルトでは最新バージョンの Ruby をインストールします。Ruby 3.0.2 を定義する次の例に示すように、Gemfile
内の ruby
DSL で正確なバージョンを指定します。
ruby "3.0.2"
常に、ローカルでも同じバージョンの Ruby を使用します。ruby -v
を使用して ruby のローカルバージョンを確認します。特定の Ruby バージョンの定義についての詳細は、「Ruby のバージョン」の記事を参照してください。
Git でのアプリの保存
Heroku では、アプリケーションのデプロイを分散型ソース管理ツールである Git に依存しています。アプリケーションがまだ Git に存在しない場合は、まず git --help
を使用して git
がシステムに存在することを確認してください。
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
このコマンドで出力が生成されないか、または command not found
が出力される場合、Git は存在しません。システムに Git をインストールします。
Git が機能することを確認したら、Rails アプリのルートディレクトリに移動します。ls
を使用すると、Rails アプリのコンテンツが次のように表示されます。
$ ls
Gemfile
Gemfile-e
Gemfile.lock
README.md
Rakefile
app
bin
config
config.ru
db
lib
log
public
storage
test
tmp
vendor
Rails アプリ内で直接、ローカルの空の Git リポジトリを初期化し、そのアプリのコードをコミットします。
$ git init
$ git add .
$ git commit -m "init"
git status
を使用して、すべてが正しくコミットされたことを確認します。
$ git status
On branch main
nothing to commit, working tree clean
アプリケーションが Git にコミットされたら、Heroku にデプロイする準備ができました。
Heroku へのアプリケーションのデプロイ
Rails アプリのルートディレクトリ内で、Heroku CLI を使用して Heroku 上にアプリを作成します。
$ heroku create
Creating app... done, radiant-sands-01405
https://radiant-sands-01405.herokuapp.com/ | https://git.heroku.com/radiant-sands-01405.git
この Heroku CLI によって Git リモートが自動的に追加されます。git config
を使用して、それが設定されていることを確認します。
$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/radiant-sands-01405.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*
現在のディレクトリが正しくないか、または Git が初期化されていない場合、Git は fatal: not in a git directory
を返します。Git がリモートの一覧を返した場合は、デプロイする準備ができています。
業界での変更に従い、Heroku ではデフォルトのブランチ名を main
に更新しました。プロジェクトでデフォルトのブランチ名として master
を使用している場合は、git push heroku master
を使用してください。
次のコードをデプロイします。
$ git push heroku main
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.2.21
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.0.2
remote: -----> Installing dependencies using bundler 2.2.21
remote: Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote: Fetching gem metadata from https://rubygems.org/
remote: Fetching gem metadata from https://rubygems.org/............
remote: Fetching rake 13.0.6
remote: Installing rake 13.0.6
remote: Fetching concurrent-ruby 1.1.9
remote: Fetching minitest 5.14.4
remote: Fetching erubi 1.10.0
remote: Fetching builder 3.2.4
remote: Installing builder 3.2.4
remote: Installing erubi 1.10.0
remote: Installing minitest 5.14.4
remote: Fetching mini_portile2 2.6.1
remote: Installing concurrent-ruby 1.1.9
remote: Fetching racc 1.6.0
remote: Installing mini_portile2 2.6.1
remote: Fetching crass 1.0.6
remote: Installing racc 1.6.0 with native extensions
remote: Installing crass 1.0.6
remote: Fetching rack 2.2.3
remote: Fetching nio4r 2.5.8
remote: Installing rack 2.2.3
remote: Installing nio4r 2.5.8 with native extensions
remote: Fetching websocket-extensions 0.1.5
remote: Fetching marcel 1.0.2
remote: Installing websocket-extensions 0.1.5
remote: Installing marcel 1.0.2
remote: Fetching mini_mime 1.1.2
remote: Fetching msgpack 1.4.2
remote: Installing mini_mime 1.1.2
remote: Installing msgpack 1.4.2 with native extensions
remote: Using bundler 2.2.22
remote: Fetching method_source 1.0.0
remote: Installing method_source 1.0.0
remote: Fetching thor 1.1.0
remote: Installing thor 1.1.0
remote: Fetching zeitwerk 2.5.1
remote: Installing zeitwerk 2.5.1
remote: Fetching pg 1.2.3
remote: Installing pg 1.2.3 with native extensions
remote: Fetching redis 4.5.1
remote: Installing redis 4.5.1
remote: Fetching rack-test 1.1.0
remote: Installing rack-test 1.1.0
remote: Fetching i18n 1.8.10
remote: Installing i18n 1.8.10
remote: Fetching tzinfo 2.0.4
remote: Installing tzinfo 2.0.4
remote: Fetching sprockets 4.0.2
remote: Installing sprockets 4.0.2
remote: Fetching websocket-driver 0.7.5
remote: Installing websocket-driver 0.7.5 with native extensions
remote: Fetching mail 2.7.1
remote: Installing mail 2.7.1
remote: Fetching nokogiri 1.12.5
remote: Installing nokogiri 1.12.5 with native extensions
remote: Fetching activesupport 7.0.0.alpha2
remote: Installing activesupport 7.0.0.alpha2
remote: Fetching puma 5.5.2
remote: Installing puma 5.5.2 with native extensions
remote: Fetching globalid 0.5.2
remote: Installing globalid 0.5.2
remote: Fetching activemodel 7.0.0.alpha2
remote: Installing activemodel 7.0.0.alpha2
remote: Fetching jbuilder 2.11.2
remote: Installing jbuilder 2.11.2
remote: Fetching bootsnap 1.9.1
remote: Installing bootsnap 1.9.1 with native extensions
remote: Fetching activejob 7.0.0.alpha2
remote: Installing activejob 7.0.0.alpha2
remote: Fetching activerecord 7.0.0.alpha2
remote: Installing activerecord 7.0.0.alpha2
remote: Fetching rails-dom-testing 2.0.3
remote: Fetching loofah 2.12.0
remote: Installing rails-dom-testing 2.0.3
remote: Installing loofah 2.12.0
remote: Fetching rails-html-sanitizer 1.4.2
remote: Installing rails-html-sanitizer 1.4.2
remote: Fetching actionview 7.0.0.alpha2
remote: Installing actionview 7.0.0.alpha2
remote: Fetching actionpack 7.0.0.alpha2
remote: Installing actionpack 7.0.0.alpha2
remote: Fetching actioncable 7.0.0.alpha2
remote: Fetching activestorage 7.0.0.alpha2
remote: Fetching railties 7.0.0.alpha2
remote: Fetching actionmailer 7.0.0.alpha2
remote: Installing activestorage 7.0.0.alpha2
remote: Installing actionmailer 7.0.0.alpha2
remote: Installing actioncable 7.0.0.alpha2
remote: Installing railties 7.0.0.alpha2
remote: Fetching sprockets-rails 3.2.2
remote: Installing sprockets-rails 3.2.2
remote: Fetching actiontext 7.0.0.alpha2
remote: Fetching actionmailbox 7.0.0.alpha2
remote: Installing actionmailbox 7.0.0.alpha2
remote: Installing actiontext 7.0.0.alpha2
remote: Fetching rails 7.0.0.alpha2
remote: Installing rails 7.0.0.alpha2
remote: Fetching importmap-rails 0.8.1
remote: Fetching turbo-rails 7.1.1
remote: Fetching stimulus-rails 0.7.1
remote: Installing stimulus-rails 0.7.1
remote: Installing importmap-rails 0.8.1
remote: Installing turbo-rails 7.1.1
remote: Bundle complete! 15 Gemfile dependencies, 51 gems now installed.
remote: Gems in the groups 'development' and 'test' were not installed.
remote: Bundled gems are installed into `./vendor/bundle`
remote: Bundle completed (44.86s)
remote: Cleaning up the bundler cache.
remote: Removing bundler (2.2.21)
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: I, [2021-10-21T22:11:18.492467 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js
remote: I, [2021-10-21T22:11:18.492756 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js.gz
remote: I, [2021-10-21T22:11:18.493485 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css
remote: I, [2021-10-21T22:11:18.493714 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css.gz
remote: I, [2021-10-21T22:11:18.494261 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js
remote: I, [2021-10-21T22:11:18.494478 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js.gz
remote: I, [2021-10-21T22:11:18.495030 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/application-0a88d7da94dddbd4b5db3a7e58aba83c761c0de29f578c197e4e41a3a79d014f.js
remote: I, [2021-10-21T22:11:18.495196 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/application-0a88d7da94dddbd4b5db3a7e58aba83c761c0de29f578c197e4e41a3a79d014f.js.gz
remote: I, [2021-10-21T22:11:18.495444 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js
remote: I, [2021-10-21T22:11:18.495608 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js.gz
remote: I, [2021-10-21T22:11:18.495831 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js
remote: I, [2021-10-21T22:11:18.495994 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js.gz
remote: I, [2021-10-21T22:11:18.496233 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/turbo-7c5e418c7a4d154780d11e54c0fbd17ca23ba401e915c299c4166410c90db9df.js
remote: I, [2021-10-21T22:11:18.496401 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/turbo-7c5e418c7a4d154780d11e54c0fbd17ca23ba401e915c299c4166410c90db9df.js.gz
remote: I, [2021-10-21T22:11:18.496620 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js
remote: I, [2021-10-21T22:11:18.496786 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js.gz
remote: I, [2021-10-21T22:11:18.497012 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/turbo.min-2b7ef8ab9e438aaca507c9bc1590ef0539f72ed4624a04a31f179106683d2459.js
remote: I, [2021-10-21T22:11:18.497182 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/turbo.min-2b7ef8ab9e438aaca507c9bc1590ef0539f72ed4624a04a31f179106683d2459.js.gz
remote: I, [2021-10-21T22:11:18.497408 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js
remote: I, [2021-10-21T22:11:18.497574 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js.gz
remote: I, [2021-10-21T22:11:18.497798 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js
remote: I, [2021-10-21T22:11:18.497968 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js.gz
remote: I, [2021-10-21T22:11:18.498314 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css
remote: I, [2021-10-21T22:11:18.498836 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css.gz
remote: I, [2021-10-21T22:11:18.499290 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/es-module-shims-d6de260f2f90279fc671f4b90340d4e3e16ecf5e91864345f69fd44f87c37a4b.js
remote: I, [2021-10-21T22:11:18.499686 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/es-module-shims-d6de260f2f90279fc671f4b90340d4e3e16ecf5e91864345f69fd44f87c37a4b.js.gz
remote: I, [2021-10-21T22:11:18.500112 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js
remote: I, [2021-10-21T22:11:18.506397 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js.gz
remote: I, [2021-10-21T22:11:18.507072 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/es-module-shims.min-fb6f777d3001ba8b2a4d89d3b4c33deeb216fcdd610d55eb876ea72ad5ba50ef.js
remote: I, [2021-10-21T22:11:18.508259 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/es-module-shims.min-fb6f777d3001ba8b2a4d89d3b4c33deeb216fcdd610d55eb876ea72ad5ba50ef.js.gz
remote: I, [2021-10-21T22:11:18.508804 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus.min-74a228014930350d7d09d7f50a8c42015eccf7e6e9e9f72f38a66d07f11ebfbf.js
remote: I, [2021-10-21T22:11:18.509385 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus.min-74a228014930350d7d09d7f50a8c42015eccf7e6e9e9f72f38a66d07f11ebfbf.js.gz
remote: I, [2021-10-21T22:11:18.510490 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-autoloader-2c31fda20cec0bdbfa933e7f149712e27af6e6ac829d23f81975f6ebd4d830cf.js
remote: I, [2021-10-21T22:11:18.511451 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-autoloader-2c31fda20cec0bdbfa933e7f149712e27af6e6ac829d23f81975f6ebd4d830cf.js.gz
remote: I, [2021-10-21T22:11:18.512001 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-importmap-autoloader-b10ce93483412df368cf597e99e0d924a712f36e0910e674236239f6028ed0a8.js
remote: I, [2021-10-21T22:11:18.512629 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-importmap-autoloader-b10ce93483412df368cf597e99e0d924a712f36e0910e674236239f6028ed0a8.js.gz
remote: I, [2021-10-21T22:11:18.514099 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-loading-685d40a0b68f785d3cdbab1c0f3575320497462e335c4a63b8de40a355d883c0.js
remote: I, [2021-10-21T22:11:18.514549 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-loading-685d40a0b68f785d3cdbab1c0f3575320497462e335c4a63b8de40a355d883c0.js.gz
remote: I, [2021-10-21T22:11:18.515360 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-0ce1b26664523b4ad005eb6a6358abf11890dad17c46d207e5b61a04056d7b26.js
remote: I, [2021-10-21T22:11:18.515790 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-0ce1b26664523b4ad005eb6a6358abf11890dad17c46d207e5b61a04056d7b26.js.gz
remote: I, [2021-10-21T22:11:18.518565 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-autoloader-2c31fda20cec0bdbfa933e7f149712e27af6e6ac829d23f81975f6ebd4d830cf.js
remote: I, [2021-10-21T22:11:18.518991 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-autoloader-2c31fda20cec0bdbfa933e7f149712e27af6e6ac829d23f81975f6ebd4d830cf.js.gz
remote: I, [2021-10-21T22:11:18.519503 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-importmap-autoloader-b10ce93483412df368cf597e99e0d924a712f36e0910e674236239f6028ed0a8.js
remote: I, [2021-10-21T22:11:18.519925 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-importmap-autoloader-b10ce93483412df368cf597e99e0d924a712f36e0910e674236239f6028ed0a8.js.gz
remote: I, [2021-10-21T22:11:18.520397 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-loading-685d40a0b68f785d3cdbab1c0f3575320497462e335c4a63b8de40a355d883c0.js
remote: I, [2021-10-21T22:11:18.520858 #1229] INFO -- : Writing /tmp/build_32bdb9ef/public/assets/stimulus-loading-685d40a0b68f785d3cdbab1c0f3575320497462e335c4a63b8de40a355d883c0.js.gz
remote: Asset precompilation completed (1.55s)
remote: Cleaning assets
remote: Running: rake assets:clean
remote: -----> Detecting rails configuration
remote:
remote: ###### WARNING:
remote:
remote: No Procfile detected, using the default web server.
remote: We recommend explicitly declaring how to boot your server process via a Procfile.
remote: https://devcenter.heroku.com/articles/ruby-default-web-server
remote:
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> console, rake, web
remote:
remote: -----> Compressing...
remote: Done: 36.4M
remote: -----> Launching...
remote: Released v6
remote: https://radiant-sands-01405.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/radiant-sands-01405.git
* [new branch] main -> main
出力に警告またはエラーメッセージが表示される場合があります。出力でこれらをチェックし、必要に応じて調整を行ってください。
デプロイに成功した場合は、アプリケーションに次のような追加の調整が必要になることがあります。
- データベースの移行
- 適切な dyno のスケーリングの確認
- 何らかの問題が発生した場合は、アプリのログの参照
データベースの移行
アプリケーションでデータベースを使用している場合は、Heroku CLI を使用して One-off dyno (Heroku の構成の基本単位である軽量コンテナ) を起動し、db:migrate
を実行することによって移行をトリガーします。
$ heroku run rake db:migrate
代わりに、heroku run bash
を使用して、対話型シェルセッションを取得します。
アプリケーションへのアクセス
アプリケーションが Heroku に正常にデプロイされました。Heroku は、定義されたプロセスとプロセスタイプを使用してアプリケーションコードを実行します。新しいアプリケーションでは、デフォルトではプロセスタイプがアクティブになりません。Heroku CLI の ps:scale
コマンドを使用して、web
プロセスタイプをスケーリングします。
$ heroku ps:scale web=1
Heroku CLI の ps
コマンドを使用して、アプリのすべての dyno の状態をターミナルに表示します。
$ heroku ps
Free dyno hours quota remaining this month: 1000h 0m (100%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): bin/rails server -p ${PORT:-5000} -e $RAILS_ENV (1)
web.1: up 2021/10/21 15:11:34 -0700 (~ 13s ago)
前の例では、1 つの web
プロセスが実行されています。
heroku open
を使用して、ブラウザでアプリを起動します。
$ heroku open
ブラウザに、前に定義された「Hello World」というテキストが表示されます。表示されない場合、またはエラーが存在する場合は、ウェルカムページのコンテンツをレビューして確認してください。
Heroku では、開発中のすべてのアプリケーションにデフォルトの Web URL が提供されます。アプリケーションを本番環境用にスケールアップする準備ができたら、カスタムドメインを追加します。
アプリケーションログの表示
アプリが正しく実行されないか、またはエラーを生成する場合、アプリログは貴重なツールです。
実行中のアプリに関する情報は、Heroku CLI のログコマンド heroku logs
を使用して表示します。出力例を次に示します。
$ heroku logs
2021-10-21T22:10:23.424063+00:00 app[api]: Release v1 created by user developer@example.com2021-10-21T22:10:23.424063+00:00 app[api]: Initial release by user developer@example.com2021-10-21T22:10:23.595219+00:00 app[api]: Enable Logplex by user developer@example.com2021-10-21T22:10:23.595219+00:00 app[api]: Release v2 created by user developer@example.com2021-10-21T22:10:26.000000+00:00 app[api]: Build started by user developer@example.com2021-10-21T22:11:26.466841+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 developer@example.com2021-10-21T22:11:26.466841+00:00 app[api]: Release v3 created by user developer@example.com2021-10-21T22:11:28.391179+00:00 app[api]: Running release v4 commands by user developer@example.com2021-10-21T22:11:28.391179+00:00 app[api]: Attach DATABASE (@ref:postgresql-clear-00720) by user developer@example.com2021-10-21T22:11:28.408511+00:00 app[api]: @ref:postgresql-clear-00720 completed provisioning, setting DATABASE_URL. by user developer@example.com2021-10-21T22:11:28.408511+00:00 app[api]: Release v5 created by user developer@example.com2021-10-21T22:11:28.799460+00:00 app[api]: Release v6 created by user developer@example.com2021-10-21T22:11:28.799460+00:00 app[api]: Deploy c8c3f095 by user developer@example.com2021-10-21T22:11:28.818058+00:00 app[api]: Scaled to console@0:Free rake@0:Free web@1:Free by user developer@example.com2021-10-21T22:11:30.761337+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2021-10-21T22:11:31.000000+00:00 app[api]: Build succeeded
2021-10-21T22:11:32.893299+00:00 app[web.1]: => Booting Puma
2021-10-21T22:11:32.893311+00:00 app[web.1]: => Rails 7.0.0.alpha2 application starting in production
2021-10-21T22:11:32.893311+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2021-10-21T22:11:33.893576+00:00 app[web.1]: Puma starting in single mode...
2021-10-21T22:11:33.893601+00:00 app[web.1]: * Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
2021-10-21T22:11:33.893602+00:00 app[web.1]: * Min threads: 5
2021-10-21T22:11:33.893602+00:00 app[web.1]: * Max threads: 5
2021-10-21T22:11:33.893602+00:00 app[web.1]: * Environment: production
2021-10-21T22:11:33.893602+00:00 app[web.1]: * PID: 4
2021-10-21T22:11:33.893805+00:00 app[web.1]: * Listening on http://0.0.0.0:45732
2021-10-21T22:11:33.898605+00:00 app[web.1]: Use Ctrl-C to stop
2021-10-21T22:11:34.035712+00:00 heroku[web.1]: State changed from starting to up
アプリのログの完全なライブストリームを表示するには、コマンドに -t
/--tail
を追加します。
$ heroku logs --tail
dyno のスリープとスケーリング
新しいアプリケーションは、デフォルトでは Free dyno にデプロイされます。アイドル状態が一定期間続いた後、無料のアプリはリソースを節約するために「スリープ」します。Heroku の Free dyno の動作についての詳細は、「Free dyno 時間」を参照してください。
dyno がスリープしないようにするには、「dyno タイプ」の記事で説明されている Hobby または Professional の dyno タイプにアップグレードします。たとえば、アプリを本番 dyno に移行すると、Heroku CLI の ps:scale
コマンドを使用して、Heroku プラットフォームに同じ web
プロセスタイプを実行する追加の dyno を起動または停止するよう指示することにより、スケーリングを簡単に行うことができます。
Rails コンソール
One-off dyno をトリガーして、スクリプトやアプリケーションを必要な場合にのみ実行するには、Heroku CLI の run
コマンドを使用します。このコマンドを使用して、アプリの環境で試行するためにローカルターミナルにアタッチされた Rails コンソールプロセスを起動します。
$ heroku run rails console
irb(main):001:0> puts 1+1
2
また、run bash
Heroku CLI コマンドもデバッグに役立ちます。このコマンドは、対話型の bash セッションで新しい One-off dyno を起動します。
Rake コマンド
Rails コンソールと同様に、run
コマンドを使用して rake
コマンド (db:migrate
など) を実行します。
$ heroku run rake db:migrate
Web サーバーの設定
デフォルトでは、Rails アプリの Web プロセスは、Rails 7 で Puma を使用する rails server
を実行します。Rails 7 にアップグレードされたアプリでは、そのアプリの Gemfile
に puma
gem を追加する必要があります。
gem 'puma'
puma
gem を追加したら、それをインストールします。
$ bundle install
Rails 7 では、config/puma.rb
を使用して、Puma がインストールされているときの Puma の設定や機能を定義します。Heroku では、アプリのパフォーマンスを最大化するために、追加の Puma 設定オプションを確認することをお勧めします。
config/puma.rb
が存在しない場合は、最大のパフォーマンスを得るために、Heroku の Puma ドキュメントを使用して作成してください。
Puma がインストールされている場合は、Procfile
を使用して、Heroku に dyno で Rails アプリを起動する方法を指示します。
Procfile の作成
アプリのルートディレクトリ内に Procfile という名前のファイルを作成することによって、Web プロセスを起動するために使用されるコマンドを変更します。次の行を追加します。
ファイル Procfile
で、以下のように記述します。
web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}
このファイルは、大文字 P
、小文字 rocfile
、ファイル拡張子なしの、正確に Procfile
という名前である必要があります。
Procfile をローカルで使用するには、local
Heroku CLI コマンドを使用します。
Procfile
でのコマンドの実行に加えて、heroku local
では、環境変数を .env
ファイル経由でローカルで管理することもできます。RACK_ENV
を、ローカル環境の場合は development
に、Puma の場合は PORT
に設定します。Heroku にプッシュする前に、RACK_ENV
を production
に設定してテストします。production
は、Heroku アプリが実行される環境です。
$ echo "RACK_ENV=development" >>.env
$ echo "PORT=3000" >> .env
.env
ファイルを使用して環境変数をローカルで使用する代わりの方法として dotenv gem があります。
これはローカル環境の設定専用であるため、.env
を .gitignore
に追加します。
$ echo ".env" >> .gitignore
$ git add .gitignore
$ git commit -m "add .env to .gitignore"
Foreman を使用して、Procfile をローカルでテストします。local
を使用して Web サーバーを起動します。
$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
3:12:10 PM web.1 | Puma starting in single mode...
3:12:10 PM web.1 | * Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
3:12:10 PM web.1 | * Min threads: 5
3:12:10 PM web.1 | * Max threads: 5
3:12:10 PM web.1 | * Environment: development
3:12:10 PM web.1 | * PID: 56126
3:12:11 PM web.1 | * Listening on http://0.0.0.0:3000
3:12:11 PM web.1 | Use Ctrl-C to stop
テストが成功した場合は、前の例のようになります。Ctrl+C
または CMD+C
を押して終了し、変更を Heroku にデプロイします。
$ git add .
$ git commit -m "use puma via procfile"
$ git push heroku main || git push heroku master
ps
を確認します。web
プロセスが新しいコマンドを使用するようになり、Web サーバーとして Puma を指定しています。
$ heroku ps
Free dyno hours quota remaining this month: 1000h 0m (100%)
Free dyno usage for this app: 0h 0m (0%)
For more information on dyno sleeping and how to upgrade, see:
https://devcenter.heroku.com/articles/dyno-sleeping
=== web (Free): bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development} (1)
web.1: up 2021/10/21 15:12:37 -0700 (~ 4s ago)
ログにも、Puma が使用されていることが反映されています。
$ heroku logs
Rails アセットパイプライン
Heroku にデプロイするときに、Rails アセットパイプラインを起動するためのオプションはいくつかあります。アセットパイプラインに関する一般的な情報については、「Rails 3.1+ Asset Pipeline on Heroku Cedar」(Heroku Cedar での Rails 3.1+ アセットパイプライン) の記事を確認してください。
Rails 7 では、必要がなくなった config.assets.initialize_on_precompile
オプションが削除されました。さらに、アセットコンパイルで何らかのエラーが発生するとプッシュが失敗するようになりました。Rails 7 アセットパイプラインのサポートについては、Ruby サポートのページを参照してください。
トラブルシューティング
Heroku にデプロイされたアプリがクラッシュした (heroku ps
で状態 crashed
が表示された) 場合は、そのアプリのログを確認して原因を判定してください。次のセクションでは、アプリのクラッシュの一般的な原因について説明します。
開発またはテスト gem でのランタイムの依存関係
デプロイ中に gem が存在しない場合は、Bundler グループをチェックしてください。Heroku では、アプリが development
または test
グループなしでビルドされ、アプリの実行をこれらのいずれかのグループの gem に依存している場合は、それをグループから除外します。
一般的な例として、Rakefile
での RSpec タスクの使用があります。このエラーは多くの場合、次のようになります。
$ heroku run rake -T
Running `bundle exec rake -T` attached to terminal... up, ps.3
rake aborted!
no such file to load -- rspec/core/rake_task
まず、開発またはテスト gem グループなしで bundle install
を実行して、問題をローカルに複製します。
$ bundle install --without development:test
…
$ bundle exec rake -T
rake aborted!
no such file to load -- rspec/core/rake_task
Bundler での --without
オプションはスティッキーです。このオプションを取り除くには、bundle config --delete without
を実行します。
これらの Rake タスクを gem のロード中に条件付きにすることによってエラーを解決します。次に例を示します。
begin
require "rspec/core/rake_task"
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w[--color]
t.pattern = 'spec/**/*_spec.rb'
end
rescue LoadError
end
これがローカルで動作することを確認してから、Heroku にプッシュします。
次のステップ
これで完了です。最初の Rails 7 アプリケーションが Heroku にデプロイされました。次に、次の記事を確認してください。
- 「Ruby サポートカテゴリ」では、Heroku での Ruby および Rails の使用に関する詳細を確認できます。
- 「デプロイカテゴリ」では、デプロイを効率的かつ簡単に行うことができるようにする、多くの強力な統合や機能を紹介しています。