ponkiti's blog

主に自分用、イベント参加メモや備忘録として利用

Ruby on Rails チュートリアル「第1章 ゼロからデプロイまで」の環境構築メモ(3) 〜Herokuにデプロイする〜

Herokuのユーザ登録

https://www.heroku.com/

f:id:pyoonn:20141022105114p:plain

toolbeltのインストール

herokuコマンドを使えるようにするため、toolbeltをインストールしておく。

https://toolbelt.heroku.com/standalone

f:id:pyoonn:20141022104809p:plain

f:id:pyoonn:20141022104909p:plain

表示されたコマンドを実行する。

[vagrant@localhost first_app]$ wget -qO- https://toolbelt.heroku.com/install.sh | sh
This script requires superuser access to install software.
You will be prompted for your password by sudo.
Add the Heroku CLI to your PATH using:
$ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.profile
Installation complete

次にPATHを通すが、CentOSの場合は~/.bash_profileに追記すること。

[vagrant@localhost first_app]$ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.bash_profile
[vagrant@localhost first_app]$ source ~/.bash_profile

herokuコマンドが有効か確認する。

[vagrant@localhost first_app]$ heroku --version
heroku-toolbelt/3.13.0 (x86_64-linux) ruby/2.0.0

Railsチュートリアルに合わせた事前処理

この記事に沿っているので、Herokuにデプロイするだけであればスキップしていい。

Gemfileの編集

RailsがHerokuのPostgreSQLと通信できるよう、Gemfileの編集をしておく。

[vagrant@localhost first_app]$ vi Gemfile
[vagrant@localhost first_app]$ cat Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.5'

group :development do
  gem 'sqlite3', '1.3.8'
end

gem 'sass-rails', '4.0.2'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
gem 'therubyracer'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

# Railsチュートリアル(リスト1.9)
group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end

bundle install

本番用のgemをローカルの環境にインストールしないよう--without productionオプションをつけて実行する。

[vagrant@localhost first_app]$ bundle install --without production
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 10.3.2
(省略)
Using uglifier 2.1.1
Your bundle is complete!
Gems in the group production were not installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

git commit

Gemfile.lockの更新情報をコミットしておく。

[vagrant@localhost first_app]$ git commit -a -m "Update Gemfile.lock for Heroku"
[master 119a4fe] Update Gemfile.lock for Heroku
 2 files changed, 14 insertions(+), 0 deletions(-)

herokuにログイン

秘密鍵と公開鍵があることが前提なので、未作成の場合は環境構築メモ(2) のように「秘密鍵と公開鍵の作成」を行なう。

Herokuに登録済みのメールアドレスとパスワードを入力する。

[vagrant@localhost first_app]$ heroku login
Enter your Heroku credentials.
Email: メールアドレス
Password (typing will be hidden):
Your Heroku account does not have a public ssh key uploaded.
Found an SSH public key at /home/vagrant/.ssh/id_rsa.pub
Would you like to upload it to Heroku? [Yn] y
Uploading SSH public key /home/vagrant/.ssh/id_rsa.pub... done
Authentication successful.

herokuに新規アプリケーションを作成

Herokuにデプロイしたアプリケーションの内容は、heroku createで生成されたアドレスにアクセスすると確認できる。

[vagrant@localhost first_app]$ heroku create
Creating lit-ocean-8795... done, stack is cedar
http://lit-ocean-8795.herokuapp.com/ | git@heroku.com:lit-ocean-8795.git
Git remote heroku added

herokuにリポジトリをpush

[vagrant@localhost first_app]$ git push heroku master
The authenticity of host 'heroku.com (50.19.85.156)' can't be established.
RSA key fingerprint is XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'heroku.com,50.19.85.156' (RSA) to the list of known hosts.
Initializing repository, done.
Counting objects: 67, done.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (67/67), 14.53 KiB, done.
Total 67 (delta 6), reused 57 (delta 2)

(省略)

To git@heroku.com:lit-ocean-8795.git
 * [new branch]      master -> master

Herokuのダッシュボードにも新規作成したアプリケーション画面が作成される。

f:id:pyoonn:20141022114343p:plain

アプリケーション名は変更可能

アプリケーション名に使用可能な文字列

使用可能なのは小文字、数字、ダッシュ(-)のみで、アンダーバー(_)は指定できない。

下記は、使用可能な文字列以外を指定した場合のエラー。

[vagrant@localhost first_app]$ heroku rename rubyonrail_stutorial_lesson
Renaming lit-ocean-8795 to rubyonrail_stutorial_lesson... failed
 !    Name must start with a letter and can only contain lowercase letters, numbers, and dashes.

ユニークなアプリケーション名を指定

Heroku内ですでに使用されているアプリケーション名は指定できない。

[vagrant@localhost first_app]$ heroku rename rubyonrailstutorial
Renaming lit-ocean-8795 to rubyonrailstutorial... failed
 !    Name is already taken

アプリケーション名の変更完了

下記は、アプリケーション名が無事変更できた時のメッセージ。

[vagrant@localhost first_app]$ heroku rename rubyonrailstutoriallesson
Renaming lit-ocean-8795 to rubyonrailstutoriallesson... done
http://rubyonrailstutoriallesson.herokuapp.com/ | git@heroku.com:rubyonrailstutoriallesson.git
Git remote heroku updated

Herokuのダッシュボード上でもアプリケーション名の変更が確認できた。

f:id:pyoonn:20141022114540p:plain

Settings画面でアプリケーション名を変更することも可能。

f:id:pyoonn:20141022114633p:plain

参考