shou2017.com
JP

Steps to Deploy Rails 5 to Heroku

Sat Apr 20, 2019
Sat Aug 10, 2024

When I tried to push Rails to Heroku after a while, I found that many things had changed, so here’s a note.

This assumes that the Heroku CLI is already installed.

Environment

  • macOS Mojave 10.14.4
  • Rails 5.2.2
  • ruby 2.6.1
  • ES6
  • stimulusjs
  • heroku/7.22.10 darwin-x64 node-v11.10.1
  • PostgreSQL

Updating Heroku CLI

It’s troublesome if it’s not up-to-date, so let’s update it first.

$ brew update
$ brew upgrade heroku
$ heroku --version

Heroku Login and Create

Previously, you had to enter your ID and password to log in, but now you can also log in through your browser.

$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 
$ heroku create

Asset Precompilation

Edit config/environments/production.rb

# config/environments/production.rb
# Do not fallback to assets pipeline if a precompiled asset is missed.
[omitted]
config.assets.compile = true <= change false to true

ES6 parts cannot be compiled, so change to Uglifier.new(harmony: true). If you try to compile without changing this, you’ll get an error: Uglifier::Error: Unexpected token: punc ()). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).

# config/environments/production.rb
[omitted]
## Compress JavaScripts and CSS.
config.assets.js_compressor = Uglifier.new(harmony: true)

After this is done, compile. Always run asset precompilation before sending to Heroku when you’ve made changes to assets.

$ rails assets:precompile RAILS_ENV=production

Sending to Heroku

$ git add -A
$ git commit -m "first heroku push"
$ git push heroku master

Heroku automatically creates a database, so rails db:create is not necessary.

$ heroku run rails db:migrate RAILS_ENV=production

Once that’s done, access Heroku:

$ heroku open
See Also