TH's Notes
  • Home
  • Categories
  • Tags
  • Archives

使用 Figaro 設定環境變數

開發專案時, 我們常常需要在不同的環境下(例如 development、test 或 production)為環境變數設定不同的值, 倘若每次都要手動設定, 不僅浪費時間而且很容易發生錯誤; 對於這個問題, Figaro 提供了一個簡潔的方法來處理。

安裝套件

在 Gemfile 中加入

gem 'figaro'

然後執行 bundle install 進行安裝。

設定環境變數

請在專案目錄下,執行 figaro install,這命令將

  • 自動產生 config/application.yml
  • 將此檔案加到 .gitignore 裡面

然後就可以在 application.yml 裡面針對不同環境來設定變數, 如下

config/application.yml:

development:
  SMTP_USER_NAME: test@example.com
  SMTP_PASSWORD: test_password

test:
  SMTP_USER_NAME: test@example.com
  SMTP_PASSWORD: test_password

staging:
  SMTP_USER_NAME: test@example.com
  SMTP_PASSWORD: test_password

production:
  SMTP_USER_NAME: live@example.com
  SMTP_PASSWORD: live_password

設定後,請記得重新啟動伺服器以載入變數。

設定 Heroku 環境變數

Figaro 還提供了方便的指令可以一次將設定檔的環境變數輸出到 Heroku 上,如下

figaro heroku:set -e production
Comments
comments powered by Disqus

  • « 管理者登入機制
  • 信用卡交易服務: Stripe »

Published

Jan 16, 2015

Category

Web

Tags

  • rails 63
  • Powered by Pelican. Theme: Elegant by Talha Mansoor