Skip Navigation
Show nav
Dev Center
  • Get Started
  • ドキュメント
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
    • .NET
  • ドキュメント
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku のアーキテクチャ
    • Dyno (アプリコンテナ)
      • Dyno Management
      • Dyno Concepts
      • Dyno Behavior
      • Dyno Reference
      • Dyno Troubleshooting
    • スタック (オペレーティングシステムイメージ)
    • ネットワーキングと DNS
    • プラットフォームポリシー
    • プラットフォームの原則
  • Developer Tools
    • コマンドライン
    • Heroku VS Code Extension
  • デプロイ
    • Git を使用したデプロイ
    • Docker によるデプロイ
    • デプロイ統合
  • 継続的デリバリーとインテグレーション
    • 継続的統合
  • 言語サポート
    • Node.js
      • Working with Node.js
      • Node.js Behavior in Heroku
      • Troubleshooting Node.js Apps
    • Ruby
      • Rails のサポート
      • Bundler の使用
      • Working with Ruby
      • Ruby Behavior in Heroku
      • Troubleshooting Ruby Apps
    • Python
      • Working with Python
      • Python でのバックグランドジョブ
      • Python Behavior in Heroku
      • Django の使用
    • Java
      • Java Behavior in Heroku
      • Working with Java
      • Maven の使用
      • Spring Boot の使用
      • Troubleshooting Java Apps
    • PHP
      • PHP Behavior in Heroku
      • Working with PHP
    • Go
      • Go の依存関係管理
    • Scala
    • Clojure
    • .NET
      • Working with .NET
  • データベースとデータ管理
    • Heroku Postgres
      • Postgres の基礎
      • Postgres スターターガイド
      • Postgres のパフォーマンス
      • Postgres のデータ転送と保持
      • Postgres の可用性
      • Postgres の特別なトピック
      • Migrating to Heroku Postgres
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • その他のデータストア
  • AI
    • Working with AI
    • Heroku Inference
      • Inference API
      • Quick Start Guides
      • AI Models
      • Inference Essentials
    • Vector Database
    • Model Context Protocol
  • モニタリングとメトリクス
    • ログ記録
  • アプリのパフォーマンス
  • アドオン
    • すべてのアドオン
  • 共同作業
  • セキュリティ
    • アプリのセキュリティ
    • ID と認証
      • シングルサインオン (SSO)
    • Private Space
      • インフラストラクチャネットワーキング
    • コンプライアンス
  • Heroku Enterprise
    • Enterprise Accounts
    • Enterprise Team
    • Heroku Connect (Salesforce 同期)
      • Heroku Connect の管理
      • Heroku Connect のリファレンス
      • Heroku Connect のトラブルシューティング
  • パターンとベストプラクティス
  • Heroku の拡張
    • Platform API
    • アプリの Webhook
    • Heroku Labs
    • アドオンのビルド
      • アドオン開発のタスク
      • アドオン API
      • アドオンのガイドラインと要件
    • CLI プラグインのビルド
    • 開発ビルドパック
    • Dev Center
  • アカウントと請求
  • トラブルシューティングとサポート
  • Salesforce とのインテグレーション
  • 言語サポート
  • Ruby
  • Rails のサポート
  • Heroku スターターガイド (Rails 5.x)

Heroku スターターガイド (Rails 5.x)

日本語 — Switch to English

この記事の英語版に更新があります。ご覧の翻訳には含まれていない変更点があるかもしれません。

最終更新日 2024年05月16日(木)

This article is a work in progress, or documents a feature that is not yet released to all users. This article is unlisted. Only those with the link can access it.

Table of Contents

  • ローカルの設定
  • 新しい Rails アプリの作成 (または既存の Rails アプリのアップグレード)
  • pg gem の追加
  • ウェルカムページの作成
  • Heroku gem
  • Ruby バージョンの指定
  • Git でのアプリの保存
  • Heroku アプリの作成
  • データベースのプロビジョニング
  • Heroku へのアプリケーションのデプロイ
  • データベースの移行
  • アプリケーションへのアクセス
  • ログの表示
  • dyno のスリープおよびスケーリング
  • Rails コンソールの実行
  • Rake コマンドの実行
  • Web サーバーの設定
  • Rails アセットパイプライン
  • トラブルシューティング
  • 次のステップ
  • アプリとアドオンの削除

この記事はアーカイブされています。これは更新を受け取らなくなりました。履歴の参照用にのみここに置いています。 記述がまだ正しいか、手順が今も機能するかは保証できません。 このバージョンの Rails は Rails コアでサポートされなくなりました​。 新しいアプリケーションを開始する場合は、Rails の最新リリースバージョンを使用することをお勧めします。

 

Heroku dyno、Heroku Postgres、および Heroku Data for Redis の無料プランは 2022 年 11 月 28 日で提供を終了します​。

低料金プラン​を使用してこのチュートリアルを完了することをお勧めします。資格のある学生の皆様は、新しい Heroku for GitHub Students プログラム​を通じてプラットフォームクレジットを申請できます。

Ruby on Rails は、Ruby​ で記述され、広く使われている Web フレームワークです。このガイドでは、Heroku での Rails 5 の使用について説明します。Heroku での Rails 旧バージョンの実行についての詳細は、Rails 4.x​ または Rails 3.x​ に関するチュートリアルを参照してください。

このガイドでは、以下が必要となります。

  • Ruby/Rails および Git の基本的な知識
  • Ruby 2.2.0+、Rubygems、Bundler、Rails 5 のローカルにインストールされたバージョン+
  • 確認済みの Heroku アカウント
  • Eco dyno プラン​のサブスクリプション (推奨)

ローカルの設定

Heroku CLI​ を開発用マシンにインストールします。

インストールされたら、heroku​ コマンドをターミナルから利用できます。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​ キーをアップロードするか新しいキーを作成します。

新しい Rails アプリの作成 (または既存の Rails アプリのアップグレード)

以前のバージョンの Rails を使用する既存のアプリから開始する場合は、続行する前に、Rails 5 にアップグレード​してください。既存のアプリから開始しない場合は、vanilla Rails 5 アプリがサンプルアプリとして役立ちます。

新しいアプリを作成するには、最初に、rails -v​ を実行して、Rails 5.x を使用していることを確認してください。必要に応じて、Rails の新しいバージョンは、以下を実行すると取得できます。

$ gem install rails --no-document
Successfully installed rails-7.0.4.3
1 gem installed

続いて新しいアプリを作成し、ルートディレクトリに移動します。

$ rails new myapp --database=postgresql

作成したら、アプリケーションのディレクトリに移動します。

$ cd myapp
$ bundle lock --add-platform x86_64-linux --add-platform ruby
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Writing lockfile to /Users/rschneeman/Documents/projects/rundoc/test/fixtures/rails_5/tmp/myapp/Gemfile.lock
$ bundle install

ローカルでデータベースを作成します。

$ bin/rake db:create
Database 'myapp_development' already exists
Database 'myapp_test' already exists

pg gem の追加

--database=postgresql​ を指定せずに作成された既存のアプリを使用している場合は、Rails プロジェクトに pg​ gem を追加する必要があります。Gemfile​ を編集し、以下の行を変更します。

gem 'sqlite3'

変更後は次のようになります。

gem 'pg'

開発には PostgreSQL を使用することを強くお勧めします。処理中の開発と​開発環境を一致させることで、環境の違いによる微妙なバグの発生が阻止されます。Postgres はローカルにインストールしてください​ (まだシステムにない場合)。

(新しい Gemfile.lock​ を生成するために) 依存関係を再インストールします。

$ bundle install

Sqlite3 の代わりに Postgres を推奨する理由に関する詳細は、「why you cannot use Sqlite3 on Heroku​」 (Heroke で Sqlite3 を使用できない理由) を参照してください。

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​ の末尾で sql​ を省略すると、アプリケーションは機能しません。

ウェルカムページの作成

Rails 5 の本番環境には、デフォルトで静的なインデックスページがありません。新しいアプリを使用するとき、本番環境にはルートページがないため、ルートページを作成する必要があります。最初に、welcome​ と呼ばれるコントローラーを作成し、ホームページが本番稼働するようにします。

$ rails generate controller welcome

次に、インデックスページを追加します。

ファイル app/views/welcome/index.html.erb​ で、以下のように記述します。

<h2>Hello World</h2>
<p>
  The time is now: <%= Time.now %>
</p>

その後、Rails をこのアクションにルーティングする必要があります。config/routes.rb​ を編集して、新しいメソッドにインデックスページを設定します。

ファイル config/routes.rb​ の 2 行目に以下を追加します。

  root 'welcome#index'

サーバーを実行して、このページが存在することを検証できます。

$ rails server

さらに、ブラウザで http://localhost:3000​ にアクセスします。ページが表示されない場合、サーバーに出力されたログを使用​してデバッグします。

Heroku gem

以前のバージョンの Rails では、Heroku で静的アセットの提供やログ記録を有効にするには、プロジェクト rails_12factor​ に gem を追加する必要がありました。新しいアプリケーションをデプロイしている場合、この 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 5 では、Ruby 2.2.0 以上が必要です。Heroku にはデフォルトで Ruby の最新バージョンがインストールされていますが、Gemfile​ で ruby​ DSL を使用して、特定のバージョンを指定することができます。現在実行している Ruby のバージョンに応じて、次のように表示されます。

ruby "2.7.8"

同じ Ruby のバージョンをローカルでも実行している必要があります。$ ruby -v​ を実行することによりこれを確認できます。ここから、Heroku での Ruby バージョンの指定​に関する詳細を確認できます。

Git でのアプリの保存

Heroku では、プロジェクトのデプロイを分散型ソース管理ツールである Git​ に依存しています。プロジェクトがまだ Git で管理されていない場合は、システムに 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 が機能することを検証した後、$ 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
On branch main
nothing to commit, working tree clean

アプリケーションが Git にコミットされ、Heroku にデプロイできるようになりました。

Heroku アプリの作成

dyno とデータベースを使用してこのチュートリアルを完了した場合、使用量のカウントに入ります。コストを抑制するために、完了したらすぐにアプリを削除​し、データベースも削除​してください。

Rails アプリが含まれるディレクトリにいることを確認してから、Heroku でアプリを作成します。

$ heroku create --stack heroku-20
Creating app... done, fathomless-escarpment-09880, stack is heroku-20
https://fathomless-escarpment-09880.herokuapp.com/ | https://git.heroku.com/fathomless-escarpment-09880.git

以下を実行して、リモートがプロジェクトに追加されていることを確認できます。

$ git config --list --local | grep heroku
remote.heroku.url=https://git.heroku.com/fathomless-escarpment-09880.git
remote.heroku.fetch=+refs/heads/*:refs/remotes/heroku/*

fatal: not in a git directory​ が表示される場合は、ディレクトリが正しくない可能性があります。表示されなければ、コードをデプロイできます。コードをデプロイした後、データベースを移行する必要があります。データベースが適切にスケールされていることを確認し、問題はログを使用してすべてデバッグしてください。

業界での変更に従い、Heroku ではデフォルトの Git ブランチ名を main​ に更新しました。デプロイ中のプロジェクトでデフォルトのブランチ名として master​ を使用している場合は、git push heroku master​ を使用してください。

データベースのプロビジョニング

アドオンを使用して Postgresql データベースをプロビジョニングします。

essential-0​ Postgres のサイズのコストは月額 5 ドルで、分単位で課金されます​。このチュートリアルの最後で、データベースを削除​して、コストを最小限に抑えるように求められます。

$ heroku addons:create heroku-postgresql:essential-0
Creating heroku-postgresql:essential-0 on ⬢ shrouded-anchorage-34700... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-encircled-75487 is being created in the background. The app will restart when complete...
Use heroku addons:info postgresql-encircled-75487 to check creation progress
Use heroku addons:docs heroku-postgresql to view documentation

Heroku アプリで Postgresql データベースにアクセスできるようになりました。資格情報は DATABASE_URL​ 環境変数に格納され、Rails が規約によってそれに接続します。

Heroku へのアプリケーションのデプロイ

コードをデプロイします。

$ git push heroku main
remote: Updated 80 paths from 53ee028
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.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.7.8
remote: -----> Installing dependencies using bundler 2.3.25
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 builder 3.2.4
remote:        Fetching erubi 1.12.0
remote:        Fetching concurrent-ruby 1.2.2
remote:        Fetching minitest 5.18.0
remote:        Installing minitest 5.18.0
remote:        Fetching racc 1.6.2
remote:        Installing builder 3.2.4
remote:        Installing erubi 1.12.0
remote:        Fetching crass 1.0.6
remote:        Installing concurrent-ruby 1.2.2
remote:        Fetching rack 2.2.7
remote:        Installing racc 1.6.2 with native extensions
remote:        Installing crass 1.0.6
remote:        Installing rack 2.2.7
remote:        Fetching nio4r 2.5.9
remote:        Fetching websocket-extensions 0.1.5
remote:        Installing nio4r 2.5.9 with native extensions
remote:        Installing websocket-extensions 0.1.5
remote:        Fetching marcel 1.0.2
remote:        Fetching mini_mime 1.1.2
remote:        Installing mini_mime 1.1.2
remote:        Installing marcel 1.0.2
remote:        Fetching date 3.3.3
remote:        Fetching timeout 0.3.2
remote:        Installing timeout 0.3.2
remote:        Fetching msgpack 1.7.1
remote:        Installing msgpack 1.7.1 with native extensions
remote:        Installing date 3.3.3 with native extensions
remote:        Using bundler 2.3.25
remote:        Fetching method_source 1.0.0
remote:        Installing method_source 1.0.0
remote:        Fetching thor 1.2.2
remote:        Installing thor 1.2.2
remote:        Fetching zeitwerk 2.6.8
remote:        Installing zeitwerk 2.6.8
remote:        Fetching pg 1.5.3
remote:        Installing pg 1.5.3 with native extensions
remote:        Fetching redis 4.8.1
remote:        Installing redis 4.8.1
remote:        Fetching rack-test 2.1.0
remote:        Installing rack-test 2.1.0
remote:        Fetching i18n 1.13.0
remote:        Installing i18n 1.13.0
remote:        Fetching tzinfo 2.0.6
remote:        Installing tzinfo 2.0.6
remote:        Fetching sprockets 4.2.0
remote:        Installing sprockets 4.2.0
remote:        Fetching websocket-driver 0.7.5
remote:        Installing websocket-driver 0.7.5 with native extensions
remote:        Fetching net-protocol 0.2.1
remote:        Installing net-protocol 0.2.1
remote:        Fetching nokogiri 1.15.1 (x86_64-linux)
remote:        Installing nokogiri 1.15.1 (x86_64-linux)
remote:        Fetching puma 5.6.5
remote:        Installing puma 5.6.5 with native extensions
remote:        Fetching activesupport 7.0.4.3
remote:        Installing activesupport 7.0.4.3
remote:        Fetching net-imap 0.3.4
remote:        Installing net-imap 0.3.4
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 loofah 2.21.3
remote:        Installing loofah 2.21.3
remote:        Fetching bootsnap 1.16.0
remote:        Installing bootsnap 1.16.0 with native extensions
remote:        Fetching rails-dom-testing 2.0.3
remote:        Installing rails-dom-testing 2.0.3
remote:        Fetching globalid 1.1.0
remote:        Installing globalid 1.1.0
remote:        Fetching activemodel 7.0.4.3
remote:        Installing activemodel 7.0.4.3
remote:        Fetching mail 2.8.1
remote:        Installing mail 2.8.1
remote:        Fetching rails-html-sanitizer 1.5.0
remote:        Installing rails-html-sanitizer 1.5.0
remote:        Fetching activejob 7.0.4.3
remote:        Installing activejob 7.0.4.3
remote:        Fetching activerecord 7.0.4.3
remote:        Fetching actionview 7.0.4.3
remote:        Installing actionview 7.0.4.3
remote:        Installing activerecord 7.0.4.3
remote:        Fetching actionpack 7.0.4.3
remote:        Installing actionpack 7.0.4.3
remote:        Fetching jbuilder 2.11.5
remote:        Installing jbuilder 2.11.5
remote:        Fetching actioncable 7.0.4.3
remote:        Fetching actionmailer 7.0.4.3
remote:        Installing actioncable 7.0.4.3
remote:        Installing actionmailer 7.0.4.3
remote:        Fetching railties 7.0.4.3
remote:        Fetching sprockets-rails 3.4.2
remote:        Installing sprockets-rails 3.4.2
remote:        Fetching activestorage 7.0.4.3
remote:        Installing activestorage 7.0.4.3
remote:        Installing railties 7.0.4.3
remote:        Fetching actionmailbox 7.0.4.3
remote:        Installing actionmailbox 7.0.4.3
remote:        Fetching actiontext 7.0.4.3
remote:        Installing actiontext 7.0.4.3
remote:        Fetching importmap-rails 1.1.6
remote:        Fetching rails 7.0.4.3
remote:        Installing importmap-rails 1.1.6
remote:        Installing rails 7.0.4.3
remote:        Fetching stimulus-rails 1.2.1
remote:        Fetching turbo-rails 1.4.0
remote:        Installing stimulus-rails 1.2.1
remote:        Installing turbo-rails 1.4.0
remote:        Bundle complete! 16 Gemfile dependencies, 56 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 (24.28s)
remote:        Cleaning up the bundler cache.
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        I, [2023-05-22T21:28:40.619608 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js
remote:        I, [2023-05-22T21:28:40.619750 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/manifest-b84bfa46a33d7f0dc4d2e7b8889486c9a957a5e40713d58f54be71b66954a1ff.js.gz
remote:        I, [2023-05-22T21:28:40.619853 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css
remote:        I, [2023-05-22T21:28:40.619926 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css.gz
remote:        I, [2023-05-22T21:28:40.620507 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/application-6472b71b26d30a0e6525e3872d53125ac65db0d91d4217d27b92d9323cefbb16.js
remote:        I, [2023-05-22T21:28:40.621111 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/application-6472b71b26d30a0e6525e3872d53125ac65db0d91d4217d27b92d9323cefbb16.js.gz
remote:        I, [2023-05-22T21:28:40.621198 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo-4851a9a0b1d947e810dfd0448a72aef261d455183ebea681f4f28a73640a9ece.js
remote:        I, [2023-05-22T21:28:40.621242 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo-4851a9a0b1d947e810dfd0448a72aef261d455183ebea681f4f28a73640a9ece.js.gz
remote:        I, [2023-05-22T21:28:40.621300 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo.min-f309baafa3ae5ad6ccee3e7362118b87678d792db8e8ab466c4fa284dd3a4700.js
remote:        I, [2023-05-22T21:28:40.621338 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo.min-f309baafa3ae5ad6ccee3e7362118b87678d792db8e8ab466c4fa284dd3a4700.js.gz
remote:        I, [2023-05-22T21:28:40.621397 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo.min.js-8bc8f4a58d1c106d58dec8bef6c638ff12ff4d078f19d8ebd8c4277f4c9bc85a.map
remote:        I, [2023-05-22T21:28:40.621434 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/turbo.min.js-8bc8f4a58d1c106d58dec8bef6c638ff12ff4d078f19d8ebd8c4277f4c9bc85a.map.gz
remote:        I, [2023-05-22T21:28:40.621800 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js
remote:        I, [2023-05-22T21:28:40.621879 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actiontext-28c61f5197c204db043317a8f8826a87ab31495b741f854d307ca36122deefce.js.gz
remote:        I, [2023-05-22T21:28:40.621946 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js
remote:        I, [2023-05-22T21:28:40.621986 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/trix-1563ff9c10f74e143b3ded40a8458497eaf2f87a648a5cbbfebdb7dec3447a5e.js.gz
remote:        I, [2023-05-22T21:28:40.622046 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css
remote:        I, [2023-05-22T21:28:40.622084 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/trix-ac629f94e04ee467ab73298a3496a4dfa33ca26a132f624dd5475381bc27bdc8.css.gz
remote:        I, [2023-05-22T21:28:40.622140 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims-69d0cb4dc1d01c9dc2ed52f2ab66874fd545fe7e35c7841009b4e8c55f231dee.js
remote:        I, [2023-05-22T21:28:40.622178 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims-69d0cb4dc1d01c9dc2ed52f2ab66874fd545fe7e35c7841009b4e8c55f231dee.js.gz
remote:        I, [2023-05-22T21:28:40.623862 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims.min-4ca9b3dd5e434131e3bb4b0c1d7dff3bfd4035672a5086deec6f73979a49be73.js
remote:        I, [2023-05-22T21:28:40.626819 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims.min-4ca9b3dd5e434131e3bb4b0c1d7dff3bfd4035672a5086deec6f73979a49be73.js.gz
remote:        I, [2023-05-22T21:28:40.627052 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims.js-c69f1a5dd068dfc08a4cedc0ad77b792985bf256e162852bd03cdf764b666c4a.map
remote:        I, [2023-05-22T21:28:40.627154 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/es-module-shims.js-c69f1a5dd068dfc08a4cedc0ad77b792985bf256e162852bd03cdf764b666c4a.map.gz
remote:        I, [2023-05-22T21:28:40.627406 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-1bd52683afde5c8ff5572f5d49429cea5bf7744ca636fcb830c015d8cccf353e.js
remote:        I, [2023-05-22T21:28:40.627589 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-1bd52683afde5c8ff5572f5d49429cea5bf7744ca636fcb830c015d8cccf353e.js.gz
remote:        I, [2023-05-22T21:28:40.627744 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote:        I, [2023-05-22T21:28:40.628465 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote:        I, [2023-05-22T21:28:40.628605 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote:        I, [2023-05-22T21:28:40.628670 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote:        I, [2023-05-22T21:28:40.628756 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote:        I, [2023-05-22T21:28:40.628849 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote:        I, [2023-05-22T21:28:40.628982 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus.min-d03cf1dff41d6c5698ec2c5d6a501615a7a33754dbeef8d1edd31c928d17c652.js
remote:        I, [2023-05-22T21:28:40.629075 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus.min-d03cf1dff41d6c5698ec2c5d6a501615a7a33754dbeef8d1edd31c928d17c652.js.gz
remote:        I, [2023-05-22T21:28:40.629340 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js
remote:        I, [2023-05-22T21:28:40.629715 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-autoloader-c584942b568ba74879da31c7c3d51366737bacaf6fbae659383c0a5653685693.js.gz
remote:        I, [2023-05-22T21:28:40.629892 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js
remote:        I, [2023-05-22T21:28:40.629999 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-importmap-autoloader-db2076c783bf2dbee1226e2add52fef290b5d31b5bcd1edd999ac8a6dd31c44a.js.gz
remote:        I, [2023-05-22T21:28:40.630250 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js
remote:        I, [2023-05-22T21:28:40.631067 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus-loading-1fc59770fb1654500044afd3f5f6d7d00800e5be36746d55b94a2963a7a228aa.js.gz
remote:        I, [2023-05-22T21:28:40.631286 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus.min.js-0f3bbd3e2e72e4d7178153a52d180de4086e47082ecfc388ce82a90d8a3d7480.map
remote:        I, [2023-05-22T21:28:40.631422 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/stimulus.min.js-0f3bbd3e2e72e4d7178153a52d180de4086e47082ecfc388ce82a90d8a3d7480.map.gz
remote:        I, [2023-05-22T21:28:40.632405 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js
remote:        I, [2023-05-22T21:28:40.633638 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/activestorage-3ab61e47dd4ee2d79db525ade1dca2ede0ea2b7371fe587e408ee37b7ade265d.js.gz
remote:        I, [2023-05-22T21:28:40.633845 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js
remote:        I, [2023-05-22T21:28:40.634773 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/activestorage.esm-01f58a45d77495cdfbdfcc872902a430426c4391634ec9c3da5f69fbf8418492.js.gz
remote:        I, [2023-05-22T21:28:40.634977 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js
remote:        I, [2023-05-22T21:28:40.635109 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actioncable-5433453f9b6619a9de91aaab2d7fc7ff183e5260c0107cbc9a1aa0c838d9a74e.js.gz
remote:        I, [2023-05-22T21:28:40.635854 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js
remote:        I, [2023-05-22T21:28:40.635946 #937]  INFO -- : Writing /tmp/build_4b70cc8b/public/assets/actioncable.esm-e01089c3ec4fe7817fa9abcad06cab6bdc387f95f0ca6aab4bf7ba7537f70690.js.gz
remote:        Asset precompilation completed (0.93s)
remote:        Cleaning assets
remote:        Running: rake assets:clean
remote: -----> Detecting rails configuration
remote:
remote: ###### WARNING:
remote:
remote:        Potential EOL Ruby Version
remote:
remote:        You are using a Ruby version that has either reached its End of Life (EOL)
remote:        or will reach its End of Life on December 25th of this year.
remote:
remote:        We suggest you upgrade to Ruby 3.0.x or later
remote:
remote:        Once a Ruby version becomes EOL, it will no longer receive
remote:        security updates from Ruby core and may have serious vulnerabilities.
remote:
remote:        Please upgrade your Ruby version.
remote:
remote:        For a list of supported Ruby versions see:
remote:          https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
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: 33M
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://fathomless-escarpment-09880.herokuapp.com/ deployed to Heroku
remote:
remote: This app is using the Heroku-20 stack, however a newer stack is available.
remote: To upgrade to Heroku-22, see:
remote: https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/fathomless-escarpment-09880.git
 * [new branch]      main -> main

出力に警告やエラーがないかどうかを必ず確認してください。何も問題がなければ、データベースを移行できます。

データベースの移行

アプリケーションでデータベースを使用している場合は、Heroku CLI を使用して One-off dyno​ (Heroku の構成の基本単位である軽量コンテナ) を起動し、db:migrate​ を実行することによって移行をトリガーします。

$ heroku run rake db:migrate

heroku run​ の後のコマンドはすべて Heroku dyno​ で実行されます。$ heroku run bash​ を実行して、対話型シェルセッションを取得できます。

アプリケーションへのアクセス

コードが Heroku にデプロイされ、プロセスタイプを実行するように Heroku に指示できるようになりました。Heroku では、これは dyno​ (Heroku の構成の基本単位である軽量コンテナ) で関連付けられているコマンドを実行して行われます。

1 つの dyno によって web​ プロセスタイプが実行されていることを確認します。

$ heroku ps:scale web=1

アプリの dyno の状態をチェックできます。heroku ps​ コマンドにより、アプリケーションで実行中の dyno の一覧が表示されます。

$ heroku ps
=== web (Basic): bin/rails server -p ${PORT:-5000} -e $RAILS_ENV (1)
web.1: up 2023/05/22 16:28:53 -0500 (~ 1s ago)

ここでは、1 つの dyno が実行中です。

heroku open​ を使用して、ブラウザでアプリにアクセスできます。

$ heroku open

すると、先ほど挿入したテキスト「Hello World」が表示されます。

Heroku では、開発中の複雑さをなくすため、デフォルトの URL が提供されます。スケールアップして Heroku を本番環境で使用する準備が整っている場合は、独自のカスタムドメイン​を追加できます。

ログの表示

アプリが適切に機能しないような問題が発生した場合は、ログをチェックする必要があります。

ログコマンド​の 1 つである heroku logs​ を使って、実行中のアプリに関する情報を表示できます。

$ heroku logs
2023-05-22T21:28:08.718911+00:00 app[api]: Initial release by user developer@example.com2023-05-22T21:28:08.718911+00:00 app[api]: Release v1 created by user developer@example.com2023-05-22T21:28:08.851399+00:00 app[api]: Enable Logplex by user developer@example.com2023-05-22T21:28:08.851399+00:00 app[api]: Release v2 created by user developer@example.com2023-05-22T21:28:10.000000+00:00 app[api]: Build started by user developer@example.com2023-05-22T21:28:45.872424+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.com2023-05-22T21:28:45.872424+00:00 app[api]: Release v3 created by user developer@example.com2023-05-22T21:28:46.525631+00:00 app[api]: Attach DATABASE (@ref:postgresql-rectangular-32248) by user developer@example.com2023-05-22T21:28:46.525631+00:00 app[api]: Running release v4 commands by user developer@example.com2023-05-22T21:28:46.538137+00:00 app[api]: @ref:postgresql-rectangular-32248 completed provisioning, setting DATABASE_URL. by user developer@example.com2023-05-22T21:28:46.538137+00:00 app[api]: Release v5 created by user developer@example.com2023-05-22T21:28:46.915284+00:00 app[api]: Deploy a6165823 by user developer@example.com2023-05-22T21:28:46.915284+00:00 app[api]: Release v6 created by user developer@example.com2023-05-22T21:28:46.932771+00:00 app[api]: Scaled to console@0:Basic rake@0:Basic web@1:Basic by user developer@example.com2023-05-22T21:28:49.000000+00:00 app[api]: Build succeeded
2023-05-22T21:28:49.014168+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2023-05-22T21:28:51.987150+00:00 app[web.1]: => Booting Puma
2023-05-22T21:28:51.987176+00:00 app[web.1]: => Rails 7.0.4.3 application starting in production
2023-05-22T21:28:51.987176+00:00 app[web.1]: => Run `bin/rails server --help` for more startup options
2023-05-22T21:28:53.054026+00:00 app[web.1]: Puma starting in single mode...
2023-05-22T21:28:53.054052+00:00 app[web.1]: * Puma version: 5.6.5 (ruby 2.7.8-p225) ("Birdie's Version")
2023-05-22T21:28:53.054053+00:00 app[web.1]: *  Min threads: 5
2023-05-22T21:28:53.054053+00:00 app[web.1]: *  Max threads: 5
2023-05-22T21:28:53.054053+00:00 app[web.1]: *  Environment: production
2023-05-22T21:28:53.054053+00:00 app[web.1]: *          PID: 2
2023-05-22T21:28:53.054285+00:00 app[web.1]: * Listening on http://0.0.0.0:39748
2023-05-22T21:28:53.058959+00:00 app[web.1]: Use Ctrl-C to stop
2023-05-22T21:28:53.256928+00:00 heroku[web.1]: State changed from starting to up
2023-05-22T21:28:57.527532+00:00 app[web.1]: I, [2023-05-22T21:28:57.527458 #2]  INFO -- : [869dd3e0-d2f8-4caa-b2ac-024ed75e6b53] Started GET "/" for 165.225.32.83 at 2023-05-22 21:28:57 +0000
2023-05-22T21:28:57.529547+00:00 app[web.1]: I, [2023-05-22T21:28:57.529496 #2]  INFO -- : [869dd3e0-d2f8-4caa-b2ac-024ed75e6b53] Processing by WelcomeController#index as HTML
2023-05-22T21:28:57.531098+00:00 app[web.1]: I, [2023-05-22T21:28:57.531054 #2]  INFO -- : [869dd3e0-d2f8-4caa-b2ac-024ed75e6b53]   Rendered welcome/index.html.erb within layouts/application (Duration: 0.3ms | Allocations: 120)
2023-05-22T21:28:57.534975+00:00 app[web.1]: I, [2023-05-22T21:28:57.534930 #2]  INFO -- : [869dd3e0-d2f8-4caa-b2ac-024ed75e6b53]   Rendered layout layouts/application.html.erb (Duration: 4.3ms | Allocations: 1002)
2023-05-22T21:28:57.535151+00:00 app[web.1]: I, [2023-05-22T21:28:57.535125 #2]  INFO -- : [869dd3e0-d2f8-4caa-b2ac-024ed75e6b53] Completed 200 OK in 6ms (Views: 5.2ms | Allocations: 1618)
2023-05-22T21:28:57.536745+00:00 heroku[router]: at=info method=GET path="/" host=fathomless-escarpment-09880.herokuapp.com request_id=869dd3e0-d2f8-4caa-b2ac-024ed75e6b53 fwd="165.225.32.83" dyno=web.1 connect=0ms service=15ms status=200 bytes=2530 protocol=https
2023-05-22T21:28:57.606204+00:00 heroku[router]: at=info method=GET path="/assets/application-e0cf9d8fcb18bf7f909d8d91a5e78499f82ac29523d475bf3a9ab265d5e2b451.css" host=fathomless-escarpment-09880.herokuapp.com request_id=96e80556-1b99-43ef-9551-4a6f89d49f59 fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=575 protocol=https
2023-05-22T21:28:57.848847+00:00 heroku[router]: at=info method=GET path="/assets/es-module-shims.min-4ca9b3dd5e434131e3bb4b0c1d7dff3bfd4035672a5086deec6f73979a49be73.js" host=fathomless-escarpment-09880.herokuapp.com request_id=0860764c-4a8d-4b17-aa2d-964723b8b4a5 fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=12472 protocol=https
2023-05-22T21:28:57.851876+00:00 heroku[router]: at=info method=GET path="/assets/application-6472b71b26d30a0e6525e3872d53125ac65db0d91d4217d27b92d9323cefbb16.js" host=fathomless-escarpment-09880.herokuapp.com request_id=79fd01bb-6b5f-4972-ad42-f51ff8995443 fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=313 protocol=https
2023-05-22T21:28:57.900828+00:00 heroku[router]: at=info method=GET path="/assets/turbo.min-f309baafa3ae5ad6ccee3e7362118b87678d792db8e8ab466c4fa284dd3a4700.js" host=fathomless-escarpment-09880.herokuapp.com request_id=100ef50c-ae69-4457-9df2-46b0c34c802e fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=22749 protocol=https
2023-05-22T21:28:58.022168+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=fathomless-escarpment-09880.herokuapp.com request_id=006bc4b2-654c-4eb2-aba7-2c3d44f1c0bd fwd="165.225.32.83" dyno=web.1 connect=0ms service=1ms status=200 bytes=143 protocol=https

以下のように --tail​ フラグオプションを使用してログコマンドを実行することで、完全なログストリームを入手することもできます。

$ heroku logs --tail

dyno のスリープおよびスケーリング

デフォルトでは、新しいアプリケーションは Eco dyno にデプロイされます。Eco アプリは、リソースを節約するために “スリープ” します。Eco dyno の動作​について読むと、この動作の詳細がわかります。

dyno がスリープしないようにするには、「dyno タイプ​」の記事で紹介されている Basic または Professional の dyno タイプにアップグレードできます。たとえば、アプリを Professional dyno に移行すると、Heroku に特定の数の dyno の実行を指示するコマンドを実行し、各 dyno で Web プロセスタイプを実行させて、アプリを簡単にスケールすることができます。

Rails コンソールの実行

Heroku では、heroku run​ コマンドを使用して、One-off dyno​ (必要な場合にのみ実行するスクリプトおよびアプリケーション) でコマンドを実行できます。これを使用して、アプリの環境で試行するためにローカルターミナルにアタッチされた Rails コンソールプロセスを起動します。

$ heroku run rails console
irb(main):001:0> puts 1+1
2

デバッグに役立つもう 1 つのコマンドが $ heroku run bash​ であり、新しい dyno を迅速に起動し、bash セッションにアクセスできるようにします。

Rake コマンドの実行

Rake は、コンソールと同様に、アタッチされたプロセスとして実行できます。

$ heroku run rake db:migrate

Web サーバーの設定

デフォルトで、アプリの Web プロセスでは、Rails 5 で Puma を使用する rails server​ が実行されます。アプリをアップグレードする場合は、アプリケーション Gemfile​ に puma​ を追加する必要があります。

gem 'puma'

次に、以下を実行します。

$ bundle install

これで、アプリで Puma が使用されるように設定する準備が整いました。このチュートリアルでは、Rails 5 に付属するデフォルトの config/puma.rb​ を使用しますが、Puma のドキュメントを読んで​、パフォーマンスを最大限にするためのアプリケーションの設定について確認することをお勧めします。

最後に、アプリケーションディレクトリのルートで Procfile​ を作成し、Rails の実行方法を Heroku に伝える必要があります。

Procfile の作成

Procfile​ を呼ばれるファイルを作成し、以下を入力して、Web プロセスを起動するのに使用するコマンドを変更します。

ファイル Procfile​ で、以下のように記述します。

web: bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}

このファイルは、正確に Procfile​ と命名する必要があります。

パフォーマンスを最大限にするために、Puma のドキュメント​に基づいて設定ファイルを生成することをお勧めします。

Procfile をローカルで使用するには、heroku local​ を使用できます。

Procfile​ でコマンドを実行するだけでなく、heroku local​ は、.env​ ファイルを通じてローカルで環境変数を管理する場合にも役立ちます。ローカルの RACK_ENV​ をご使用の環境での開発に設定し、接続先の PORT​ を設定します。Heroku にプッシュする前に、Heroku アプリが実行される環境となる本番環境に設定された RACK_ENV​ でテストする必要があります。

$ echo "RACK_ENV=development" >>.env
$ echo "PORT=3000" >> .env

​.env​ ファイルを使用してローカルで環境変数を使用するもう 1 つの代替方法が dotenv​ gem です。

さらに、ローカル環境の設定として、.env​ を .gitignore​ に追加する必要があります。

$ echo ".env" >> .gitignore
$ git add .gitignore
$ git commit -m "add .env to .gitignore"

Foreman を使用して、Procfile をローカルでテストします。これで、以下を実行して Web サーバーを起動できます。

$ heroku local
[OKAY] Loaded ENV .env File as KEY=VALUE Format
4:29:03 PM web.1 |  Puma starting in single mode...
4:29:04 PM web.1 |  * Puma version: 5.6.5 (ruby 2.7.8-p225) ("Birdie's Version")
4:29:04 PM web.1 |  *  Min threads: 5
4:29:04 PM web.1 |  *  Max threads: 5
4:29:04 PM web.1 |  *  Environment: development
4:29:04 PM web.1 |  *          PID: 63271
4:29:05 PM web.1 |  * Listening on http://0.0.0.0:3000
4:29:05 PM web.1 |  Use Ctrl-C to stop

問題がなければ Ctrl+C​ を押して終了し、変更を Heroku にデプロイできます。

$ git add .
$ git commit -m "use puma via procfile"
$ git push heroku main

ps​ を確認します。Web プロセスによって、Web サーバーとして Puma を指定している新しいコマンドが使用されていることが分かります。

$ heroku ps
=== web (Basic): bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development} (1)
web.1: up 2023/05/22 16:29:24 -0500 (~ 0s ago)

ログにも、Puma が使用されていることが反映されています。

$ heroku logs

Rails アセットパイプライン

Heroku へのデプロイ時に Rails アセットパイプライン​を起動する方法は複数あります。アセットパイプラインに関する一般的な情報については、「Rails 3.1+ Asset Pipeline on Heroku Cedar​」 (Heroku Ceder での Rails 3.1 およびアセットパイプライン) の記事を参照してください。

config.assets.initialize_on_precompile​ オプションは削除済みであり、Rails 5 では必要ありません。また、アセットコンパイルにおけるエラーはプッシュが失敗する原因となります。Rails 5 アセットパイプラインのサポートについては、Ruby サポート​ページを参照してください。

トラブルシューティング

プッシュしたアプリがクラッシュした (heroku ps​ でステータス crashed​ が表示された) 場合は、ログをチェックしてクラッシュの原因を見つけてください。一般的な問題には、たとえば以下があります。

開発/テスト gem でのランタイムの依存関係

デプロイ時に gem がない場合は、Bundler グループをチェックしてください。Heroku では、アプリは development​ や test​ グループなしで構築されます。アプリの実行がこれらのグループのいずれかからの gem に依存している場合、これをグループから除外します。

最もよくある例の 1 つとして、Rakefile​ で Rspec タスクを使用する場合があります。Heroku のデプロイで以下が表示されます。

$ 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

そして、以下の問題が発生します。最初に、問題をローカルにコピーします。

$ 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 5 アプリケーションをデプロイできました。調査を継続するには、次に、以下の記事を確認してください。

  • 「Ruby サポートカテゴリ​」では、Heroku での Ruby および Rails の使用に関する詳細を確認できます。
  • 「デプロイカテゴリ​」では、デプロイを効率的かつ簡単に行うことができるようにする、多くの強力な統合や機能を紹介しています。

アプリとアドオンの削除

このアプリケーションとデータベースが不要な場合は、ここでアカウントから削除できます。使用したリソースに対してのみ課金されます。

この操作により、アドオンが削除され、データベースに保存されたすべてのデータが失われます。

$ heroku addons:destroy heroku-postgresql

この操作により、アプリケーションが削除されます

$ heroku apps:destroy

以下のコマンドによって、アドオンとアプリが消去されたことを確認できます。

$ heroku addons --all
$ heroku apps -all

これで、​アプリをデプロイする​準備ができました。

関連カテゴリー

  • Rails のサポート
Unicorn を使用した Rails アプリケーションのデプロイ Heroku スターターガイド (Rails 7.x)

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure
  • .NET

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing
  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Github
  • LinkedIn
  • © 2025 Salesforce, Inc. All rights reserved. Various trademarks held by their respective owners. Salesforce Tower, 415 Mission Street, 3rd Floor, San Francisco, CA 94105, United States
  • heroku.com
  • Legal
  • Terms of Service
  • Privacy Information
  • Responsible Disclosure
  • Trust
  • Contact
  • Cookie Preferences
  • Your Privacy Choices