動作確認のためのRailsアプリをherokuにデプロイするまでのメモです。
用意しておくもの
- herokuアカウント
前提環境
- Rails環境
- 2015/7時点 での情報です。
参考
- Getting Started with Rails 4.x on Heroku
- HerokuにRailsブランクアプリケーションを設置してみる
- Rails4のアプリをHerokuで動かす
- Getting Started with Rails
デプロイメント手順
“Hello World” お試しAppを作成
herokuではRails4デフォルトページが表示されないようなので動作確認用のAppを作成する。
$ rbenv exec bundle exec rails generate controller welcome $ vi app/views/welcome/index.html.erb
<h2>Hello World</h2> <p> The time is now: <%= Time.now %> </p>
$ vi config/routes.rb
# You can have the root of your site routed with "root" root 'welcome#index'
サーバを起動して動作確認しておく。
Gemfile編集
herokuで動かすためにGemfileに幾つかのgemとrubyのバージョン指定を追加する。
#gem 'sqlite3' gem 'sqlite3', groups: %w(test development), require: false gem 'pg', groups: %w(production), require: false gem 'rails_12factor', groups: %w(production), require: false ruby "2.2.2"
追加後、Gemfile.lock更新
$ rbenv exec bundle install --without production --path vendor/bundle
gitignoreをセット
$ echo '/vendor/bundle' >> .gitignore
heroku CLIをインストール
へアクセス。Ubuntuのアイコンをクリック。
表示されたコマンドを実行。
$ wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh
herokuにログイン
コマンドラインより実行。herokuアカウントで入るとSSH公開鍵など作ってくれる。
$ heroku login
ローカルにgitリポジトリを作成してコミット。
$ git init $ git add . $ git commit -m "init"
実行時下記のメッセージがでるかもしれない。
*** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" ...
ホスト名がFQDNライクな名前になっていないと上記のようなメッセージが出てコミットできない。
上記のコマンドでユーザ情報を設定するかホスト名を変更すればOK。
コミットごとにgit configのメッセージがでるのでユーザ設定しておいたほうが良い。
$ git status $ git log
herokuへデプロイ
$ heroku create $ git push heroku master
エラーが無ければメッセージに出てくるURLをブラウザで開いて動作確認。