Heroku スターターガイド (Rails 7.x)
この記事の英語版に更新があります。ご覧の翻訳には含まれていない変更点があるかもしれません。
最終更新日 2022年11月17日(木)
Table of Contents
Heroku dyno、Heroku Postgres、および Heroku Data for Redis の無料プランは 2022 年 11 月 28 日で提供を終了します。
低料金プランを使用してこのチュートリアルを完了することをお勧めします。資格のある学生の皆様は、新しい Heroku for GitHub Students プログラムを通じてプラットフォームクレジットを申請できます。
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 CLI のローカルにインストールされたバージョン
- 確認済みの Heroku アカウント
- Eco dyno プランのサブスクリプション (推奨)
ローカルの設定
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
Successfully installed rails-7.0.4
1 gem installed
Rails アプリを作成します。
$ 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
Database 'myapp_development' already exists
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.1.2 を定義する次の例に示すように、Gemfile
内の ruby
DSL で正確なバージョンを指定します。
ruby "3.1.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>]
[--super-prefix=<path>] [--config-env=<name>=<envvar>]
このコマンドで出力が生成されないか、または 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 apps:create --stack=heroku-22
Creating app... done, calm-citadel-85993, stack is heroku-22
https://calm-citadel-85993.herokuapp.com/ | https://git.heroku.com/calm-citadel-85993.git
この Heroku CLI によって Git リモートが自動的に追加されます。git config
を使用して、それが設定されていることを確認します。
$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/calm-citadel-85993.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-22 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.10
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.1.2
remote: -----> Installing dependencies using bundler 2.3.10
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 rake 13.0.6
remote: Installing rake 13.0.6
remote: Fetching minitest 5.16.3
remote: Fetching builder 3.2.4
remote: Fetching concurrent-ruby 1.1.10
remote: Fetching erubi 1.11.0
remote: Installing builder 3.2.4
remote: Installing minitest 5.16.3
remote: Installing concurrent-ruby 1.1.10
remote: Installing erubi 1.11.0
remote: Fetching mini_portile2 2.8.0
remote: Using racc 1.6.0
remote: Fetching crass 1.0.6
remote: Fetching rack 2.2.4
remote: Installing mini_portile2 2.8.0
remote: Fetching nio4r 2.5.8
remote: Installing crass 1.0.6
remote: Fetching websocket-extensions 0.1.5
remote: Installing rack 2.2.4
remote: Installing websocket-extensions 0.1.5
remote: Fetching marcel 1.0.2
remote: Installing nio4r 2.5.8 with native extensions
remote: Fetching mini_mime 1.1.2
remote: Installing marcel 1.0.2
remote: Fetching timeout 0.3.0
remote: Installing mini_mime 1.1.2
remote: Using bundler 2.3.10
remote: Installing timeout 0.3.0
remote: Fetching method_source 1.0.0
remote: Fetching msgpack 1.6.0
remote: Fetching thor 1.2.1
remote: Installing method_source 1.0.0
remote: Installing msgpack 1.6.0 with native extensions
remote: Installing thor 1.2.1
remote: Fetching zeitwerk 2.6.6
remote: Installing zeitwerk 2.6.6
remote: Fetching pg 1.4.4
remote: Fetching nokogiri 1.13.9
remote: Installing pg 1.4.4 with native extensions
remote: Installing nokogiri 1.13.9 with native extensions
remote: Fetching websocket-driver 0.7.5
remote: Installing websocket-driver 0.7.5 with native extensions
remote: Fetching i18n 1.12.0
remote: Installing i18n 1.12.0
remote: Fetching tzinfo 2.0.5
remote: Installing tzinfo 2.0.5
remote: Fetching mail 2.7.1
remote: Installing mail 2.7.1
remote: Fetching rack-test 2.0.2
remote: Installing rack-test 2.0.2
remote: Fetching sprockets 4.1.1
remote: Fetching net-protocol 0.1.3
remote: Installing sprockets 4.1.1
remote: Installing net-protocol 0.1.3
remote: Fetching puma 5.6.5
remote: Fetching activesupport 7.0.4
remote: Installing puma 5.6.5 with native extensions
remote: Installing activesupport 7.0.4
remote: Fetching bootsnap 1.13.0
remote: Installing bootsnap 1.13.0 with native extensions
remote: Fetching net-imap 0.3.1
remote: Installing net-imap 0.3.1
remote: Fetching net-pop 0.1.2
remote: Installing net-pop 0.1.2
remote: Fetching net-smtp 0.3.3
remote: Installing net-smtp 0.3.3
remote: Fetching globalid 1.0.0
remote: Installing globalid 1.0.0
remote: Fetching activemodel 7.0.4
remote: Installing activemodel 7.0.4
remote: Fetching activejob 7.0.4
remote: Installing activejob 7.0.4
remote: Fetching activerecord 7.0.4
remote: Installing activerecord 7.0.4
remote: Fetching rails-dom-testing 2.0.3
remote: Fetching loofah 2.19.0
remote: Installing loofah 2.19.0
remote: Installing rails-dom-testing 2.0.3
remote: Fetching rails-html-sanitizer 1.4.3
remote: Installing rails-html-sanitizer 1.4.3
remote: Fetching actionview 7.0.4
remote: Installing actionview 7.0.4
remote: Fetching actionpack 7.0.4
remote: Fetching jbuilder 2.11.5
remote: Installing jbuilder 2.11.5
remote: Installing actionpack 7.0.4
remote: Fetching actioncable 7.0.4
remote: Fetching activestorage 7.0.4
remote: Fetching actionmailer 7.0.4
remote: Fetching railties 7.0.4
remote: Installing actioncable 7.0.4
remote: Installing activestorage 7.0.4
remote: Installing actionmailer 7.0.4
remote: Fetching sprockets-rails 3.4.2
remote: Installing railties 7.0.4
remote: Installing sprockets-rails 3.4.2
remote: Fetching actiontext 7.0.4
remote: Fetching actionmailbox 7.0.4
remote: Installing actionmailbox 7.0.4
remote: Installing actiontext 7.0.4
remote: Fetching rails 7.0.4
remote: Fetching importmap-rails 1.1.5
remote: Fetching stimulus-rails 1.1.1
remote: Fetching turbo-rails 1.3.2
remote: Installing stimulus-rails 1.1.1
remote: Installing importmap-rails 1.1.5
remote: Installing rails 7.0.4
remote: Installing turbo-rails 1.3.2
remote: Bundle complete! 15 Gemfile dependencies, 55 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 (35.89s)
remote: Cleaning up the bundler cache.
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: Running: rake assets:precompile
remote: I, [2022-11-09T20:50:08.860584 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js
remote: I, [2022-11-09T20:50:08.860763 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js.gz
remote: I, [2022-11-09T20:50:08.860947 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css
remote: I, [2022-11-09T20:50:08.861062 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css.gz
remote: I, [2022-11-09T20:50:08.861188 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js
remote: I, [2022-11-09T20:50:08.861278 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js.gz
remote: I, [2022-11-09T20:50:08.861393 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js
remote: I, [2022-11-09T20:50:08.861503 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js.gz
remote: I, [2022-11-09T20:50:08.861924 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js
remote: I, [2022-11-09T20:50:08.862037 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/hello_controller-549135e8e7c683a538c3d6d517339ba470fcfb79d62f738a0a089ba41851a554.js.gz
remote: I, [2022-11-09T20:50:08.862166 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js
remote: I, [2022-11-09T20:50:08.862309 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js.gz
remote: I, [2022-11-09T20:50:08.862437 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo-8794998d8fb36a2ae3fcb23679cd330faef003414be4662cf01cde0756461abd.js
remote: I, [2022-11-09T20:50:08.862584 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo-8794998d8fb36a2ae3fcb23679cd330faef003414be4662cf01cde0756461abd.js.gz
remote: I, [2022-11-09T20:50:08.862740 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js
remote: I, [2022-11-09T20:50:08.862817 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js.gz
remote: I, [2022-11-09T20:50:08.862961 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min.js-cdf476cab7616917e39a89c91399dcb01097d08a2d4787b0ee3b52d576cb06ec.map
remote: I, [2022-11-09T20:50:08.863058 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/turbo.min.js-cdf476cab7616917e39a89c91399dcb01097d08a2d4787b0ee3b52d576cb06ec.map.gz
remote: I, [2022-11-09T20:50:08.863219 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js
remote: I, [2022-11-09T20:50:08.863325 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js.gz
remote: I, [2022-11-09T20:50:08.863439 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js
remote: I, [2022-11-09T20:50:08.863527 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js.gz
remote: I, [2022-11-09T20:50:08.863670 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css
remote: I, [2022-11-09T20:50:08.863760 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css.gz
remote: I, [2022-11-09T20:50:08.863915 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims-16719834c9bbcdd75f1f99da713bd0c89de488be94d4c5df594511f39cffe7c1.js
remote: I, [2022-11-09T20:50:08.864010 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims-16719834c9bbcdd75f1f99da713bd0c89de488be94d4c5df594511f39cffe7c1.js.gz
remote: I, [2022-11-09T20:50:08.864160 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js
remote: I, [2022-11-09T20:50:08.864266 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js.gz
remote: I, [2022-11-09T20:50:08.864415 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map
remote: I, [2022-11-09T20:50:08.864526 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/es-module-shims.js-32db422c5db541b7129a2ce936aed905edc2cd481748f8d67ffe84e28313158a.map.gz
remote: I, [2022-11-09T20:50:08.864677 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-d094f4a1919726d8e6080201a149255e6b1561342bff3d739bc227018b6bbc80.js
remote: I, [2022-11-09T20:50:08.864780 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-d094f4a1919726d8e6080201a149255e6b1561342bff3d739bc227018b6bbc80.js.gz
remote: I, [2022-11-09T20:50:08.865025 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2022-11-09T20:50:08.865151 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2022-11-09T20:50:08.865416 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2022-11-09T20:50:08.865919 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2022-11-09T20:50:08.866089 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote: I, [2022-11-09T20:50:08.866199 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote: I, [2022-11-09T20:50:08.866356 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js
remote: I, [2022-11-09T20:50:08.866492 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js.gz
remote: I, [2022-11-09T20:50:08.866634 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote: I, [2022-11-09T20:50:08.866722 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote: I, [2022-11-09T20:50:08.867067 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote: I, [2022-11-09T20:50:08.867566 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote: I, [2022-11-09T20:50:08.868434 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote: I, [2022-11-09T20:50:08.868667 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote: I, [2022-11-09T20:50:08.880863 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min.js-b59104bd3d943049392c2908fc25a7dcbc5f918a632174f80f6bb34e86e49a8a.map
remote: I, [2022-11-09T20:50:08.881219 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/stimulus.min.js-b59104bd3d943049392c2908fc25a7dcbc5f918a632174f80f6bb34e86e49a8a.map.gz
remote: I, [2022-11-09T20:50:08.881573 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js
remote: I, [2022-11-09T20:50:08.881936 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js.gz
remote: I, [2022-11-09T20:50:08.882240 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js
remote: I, [2022-11-09T20:50:08.882497 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js.gz
remote: I, [2022-11-09T20:50:08.882722 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js
remote: I, [2022-11-09T20:50:08.882879 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js.gz
remote: I, [2022-11-09T20:50:08.883064 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js
remote: I, [2022-11-09T20:50:08.883209 #1182] INFO -- : Writing /tmp/build_3d2c59b5/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js.gz
remote: Asset precompilation completed (1.16s)
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: 37.6M
remote: -----> Launching...
remote: ! The following add-ons were automatically provisioned: heroku-postgresql. These add-ons may incur additional cost, which is prorated to the second. Run `heroku addons` for more info.
remote: Released v6
remote: https://calm-citadel-85993.herokuapp.com/ deployed to Heroku
remote:
remote: Starting November 28th, 2022, free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis® will no longer be available.
remote:
remote: If you have apps using any of these resources, you must upgrade to paid plans by this date to ensure your apps continue to run and to retain your data. For students, we will announce a new program by the end of September. Learn more at https://blog.heroku.com/next-chapter
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/calm-citadel-85993.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
▸ Starting November 28th, 2022, free Heroku Dynos will no longer be
▸ available. To keep your apps running, subscribe to Eco or upgrade to
▸ another paid tier. Learn more in our blog
▸ (https://blog.heroku.com/new-low-cost-plans).
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 2022/11/09 12:50:25 -0800 (~ 1s ago)
前の例では、1 つの web
プロセスが実行されています。
heroku open
を使用して、ブラウザでアプリを起動します。
$ heroku open
ブラウザに、前に定義された「Hello World」というテキストが表示されます。表示されない場合、またはエラーが存在する場合は、ウェルカムページのコンテンツをレビューして確認してください。
Heroku では、開発中のすべてのアプリケーションにデフォルトの Web URL が提供されます。アプリケーションを本番環境用にスケールアップする準備ができたら、カスタムドメインを追加します。
アプリケーションログの表示
アプリが正しく実行されないか、またはエラーを生成する場合、アプリログは貴重なツールです。
実行中のアプリに関する情報は、Heroku CLI のログコマンド heroku logs
を使用して表示します。出力例を次に示します。
$ heroku logs
2022-11-09T20:49:22.967927+00:00 app[api]: Initial release by user developer@example.com2022-11-09T20:49:22.967927+00:00 app[api]: Release v1 created by user developer@example.com2022-11-09T20:49:23.123785+00:00 app[api]: Enable Logplex by user developer@example.com2022-11-09T20:49:23.123785+00:00 app[api]: Release v2 created by user developer@example.com2022-11-09T20:49:25.000000+00:00 app[api]: Build started by user developer@example.com2022-11-09T20:50:15.759414+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.com2022-11-09T20:50:15.759414+00:00 app[api]: Release v3 created by user developer@example.com2022-11-09T20:50:17.118438+00:00 app[api]: Running release v4 commands by user developer@example.com2022-11-09T20:50:17.118438+00:00 app[api]: Attach DATABASE (@ref:postgresql-contoured-75359) by user developer@example.com2022-11-09T20:50:17.136705+00:00 app[api]: Release v5 created by user developer@example.com2022-11-09T20:50:17.136705+00:00 app[api]: @ref:postgresql-contoured-75359 completed provisioning, setting DATABASE_URL. by user developer@example.com2022-11-09T20:50:17.517963+00:00 app[api]: Deploy 3e67ada9 by user developer@example.com2022-11-09T20:50:17.517963+00:00 app[api]: Release v6 created by user developer@example.com2022-11-09T20:50:17.534131+00:00 app[api]: Scaled to console@0:Free rake@0:Free web@1:Free by user developer@example.com2022-11-09T20:50:19.949404+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2022-11-09T20:50:23.218318+00:00 app[web.1]: => Booting Puma
2022-11-09T20:50:23.218355+00:00 app[web.1]: => Rails 7.0.4 application starting in production
2022-11-09T20:50:23.218355+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2022-11-09T20:50:24.603033+00:00 app[web.1]: Puma starting in single mode...
2022-11-09T20:50:24.603101+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
2022-11-09T20:50:24.603129+00:00 app[web.1]: * Min threads: 5
2022-11-09T20:50:24.603146+00:00 app[web.1]: * Max threads: 5
2022-11-09T20:50:24.603162+00:00 app[web.1]: * Environment: production
2022-11-09T20:50:24.603183+00:00 app[web.1]: * PID: 4
2022-11-09T20:50:24.603486+00:00 app[web.1]: * Listening on http://0.0.0.0:11193
2022-11-09T20:50:24.609496+00:00 app[web.1]: Use Ctrl-C to stop
2022-11-09T20:50:25.010279+00:00 heroku[web.1]: State changed from starting to up
2022-11-09T20:50:30.072797+00:00 app[web.1]: I, [2022-11-09T20:50:30.072682 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Started GET "/" for 13.110.54.11 at 2022-11-09 20:50:30 +0000
2022-11-09T20:50:30.077315+00:00 app[web.1]: I, [2022-11-09T20:50:30.077223 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Processing by WelcomeController#index as HTML
2022-11-09T20:50:30.080505+00:00 app[web.1]: I, [2022-11-09T20:50:30.080420 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Rendered welcome/index.html.erb within layouts/application (Duration: 0.7ms | Allocations: 222)
2022-11-09T20:50:30.091341+00:00 app[web.1]: I, [2022-11-09T20:50:30.091242 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Rendered layout layouts/application.html.erb (Duration: 11.6ms | Allocations: 2733)
2022-11-09T20:50:30.091812+00:00 app[web.1]: I, [2022-11-09T20:50:30.091750 #4] INFO -- : [1f916fca-22c1-45b2-913b-2c931e999b05] Completed 200 OK in 14ms (Views: 13.6ms | Allocations: 3716)
2022-11-09T20:50:30.095252+00:00 heroku[router]: at=info method=GET path="/" host=calm-citadel-85993.herokuapp.com request_id=1f916fca-22c1-45b2-913b-2c931e999b05 fwd="13.110.54.11" dyno=web.1 connect=0ms service=29ms status=200 bytes=3408 protocol=https
2022-11-09T20:50:30.262089+00:00 heroku[router]: at=info method=GET path="/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css" host=calm-citadel-85993.herokuapp.com request_id=f37745b2-876e-4c29-8450-22950fa90937 fwd="13.110.54.11" dyno=web.1 connect=0ms service=2ms status=200 bytes=575 protocol=https
2022-11-09T20:50:30.262831+00:00 heroku[router]: at=info method=GET path="/assets/es-module-shims.min-d89e73202ec09dede55fb74115af9c5f9f2bb965433de1c2446e1faa6dac2470.js" host=calm-citadel-85993.herokuapp.com request_id=92b09871-4957-4616-98fc-90d1fdda1758 fwd="13.110.54.11" dyno=web.1 connect=0ms service=3ms status=200 bytes=11261 protocol=https
2022-11-09T20:50:30.395242+00:00 heroku[router]: at=info method=GET path="/assets/application-37f365cbecf1fa2810a8303f4b6571676fa1f9c56c248528bc14ddb857531b95.js" host=calm-citadel-85993.herokuapp.com request_id=7ba4308c-aed4-47f6-a822-b888e58cd22b fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=323 protocol=https
2022-11-09T20:50:30.460956+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-7ab2ea9f35bae4a4d65b552f9b93524099f267a8ba3a2e07002aaa7bff8ae4cf.js" host=calm-citadel-85993.herokuapp.com request_id=0a5d8de3-a146-4eb4-b418-e03a203f407e fwd="13.110.54.11" dyno=web.1 connect=0ms service=2ms status=200 bytes=22015 protocol=https
2022-11-09T20:50:30.513920+00:00 heroku[router]: at=info method=GET path="/assets/stimulus.min-2dae3fdcdb1a5ee8172d3dc02a2a10cd6d5f022cc7782b3888cedc06bab7388a.js" host=calm-citadel-85993.herokuapp.com request_id=a31a5c2b-23c0-4253-a8f7-7a6c5ed76703 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=9020 protocol=https
2022-11-09T20:50:30.517850+00:00 heroku[router]: at=info method=GET path="/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js" host=calm-citadel-85993.herokuapp.com request_id=a6518616-8e2e-4e4d-b0b9-13fd4de52a7c fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=1202 protocol=https
2022-11-09T20:50:30.551655+00:00 heroku[router]: at=info method=GET path="/assets/controllers/index-2db729dddcc5b979110e98de4b6720f83f91a123172e87281d5a58410fc43806.js" host=calm-citadel-85993.herokuapp.com request_id=1f3c8f5b-ed5a-4fb1-8762-fe947c2847e5 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=444 protocol=https
2022-11-09T20:50:30.713009+00:00 heroku[router]: at=info method=GET path="/assets/controllers/application-368d98631bccbf2349e0d4f8269afb3fe9625118341966de054759d96ea86c7e.js" host=calm-citadel-85993.herokuapp.com request_id=a5994257-aaa5-41e4-b6c5-b6522fae4bf7 fwd="13.110.54.11" dyno=web.1 connect=0ms service=1ms status=200 bytes=349 protocol=https
アプリのログの完全なライブストリームを表示するには、コマンドに -t
/--tail
を追加します。
$ heroku logs --tail
dyno のスリープとスケーリング
新しいアプリケーションは、デフォルトでは Eco dyno にデプロイされます。アイドル状態が一定期間続いた後、Eco アプリはリソースを節約するために「スリープ」します。Heroku の Eco dyno の動作についての詳細は、「Eco dyno 時間」を参照してください。
dyno がスリープしないようにするには、「dyno タイプ」の記事で説明されている Basic または 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
12:50:35 PM web.1 | Puma starting in single mode...
12:50:35 PM web.1 | * Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
12:50:35 PM web.1 | * Min threads: 5
12:50:35 PM web.1 | * Max threads: 5
12:50:35 PM web.1 | * Environment: development
12:50:35 PM web.1 | * PID: 20843
12:50:36 PM web.1 | * Listening on http://0.0.0.0:3000
12:50:36 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
▸ Starting November 28th, 2022, free Heroku Dynos will no longer be
▸ available. To keep your apps running, subscribe to Eco or upgrade to
▸ another paid tier. Learn more in our blog
▸ (https://blog.heroku.com/new-low-cost-plans).
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: starting 2022/11/09 12:50:53 -0800 (~ 5s 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 の使用に関する詳細を確認できます。
- 「デプロイカテゴリ」では、デプロイを効率的かつ簡単に行うことができるようにする、多くの強力な統合や機能を紹介しています。