Rails

部署你的 Rails App 至 Heroku

Heroku 是一個 platform as a service 平台,輕鬆部署專案在此吧!

楊竑昕
人生比寫 Code 難一點點

--

Heroku 首頁

安裝 heroku-cli

https://devcenter.heroku.com/articles/heroku-cli

# 將 heroku 加入 brew 的來源
$ brew tap heroku/brew && brew install heroku

登入 Heroku 帳號

$ heroku login

一般我們使用 git 來部署應用程式至 Heroku,在下個步驟前,確認你的專案已經使用 git 做版本控制了。

設定 git remote

新建一個專案叫 app_name:

$ heroku create app_name

已經在Heroku 上建立專案了則使用:

# set git remote heroku to https://git.heroku.com/app_name.git$ heroku git:remote -a app_name

設定 Rails app

使用 Postgres 作為資料庫,將下列這行加入 Gemfile :

# Gemfilegem 'pg'

設定資料庫連線資訊:

# config/database.ymlproduction:
url: <%= ENV['DATABASE_URL'] %>

config/environments/production.rb 中對 assets 與 log 做設定

# config/environments/production.rbconfig.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 版本,新增檔案 .ruby-version填入:

# .ruby-versionruby-2.6.0

在專案的根目錄下建立一個 Procfile 檔案 ($ touch Procfile),並填入以下內容:

# Procfile
web: bundle exec puma -C config/puma.rb
sidekid: bundle exec sidekiq -C config/sidekiq.yml

設定 Heroku

Heroku 管理介面 > app_name > Settings > Reveal Config Vars 查看DATABASE_URL這個環境變數,這邊填入的是你的 production database 的連線位址與帳號密碼,預設使用 Heroku Postgres 提供的資料庫。

接著 Add buildpack,加入 heroku/nodejs buildpack 並放在 heroku/ruby 之前(有用 yarn 者才需要做這個步驟):

部署

在部署前 git commit 把要部署的專案放置在 master 分支下,然後:

# push master 分支到 heroku remote
$ git push heroku master

有新的 database migrations 時執行:

# heroku run + 要運行的指令,這次要做 database migrate
$ heroku run rails db:migrate

--

--