すずけんメモ

技術メモです

Herokuのruby-sampleを試す

なんとなく「Herokuさわってみるかなぁ」というテンションになったので、ひとまずHello Worldまでやってみた。

http://heroku.com

環境はOSX Mountain Lion。

まずherokuコマンドを使えるようにする。

-> % brew install heroku-toolbelt
==> Downloading http://assets.heroku.com.s3.amazonaws.com/heroku-client/heroku-client-2.39.4.tgz
######################################################################## 100.0%
🍺  /usr/local/Cellar/heroku-toolbelt/2.39.4: 371 files, 3.1M, built in 4 seconds

Heroku用の公開鍵が必要になるので作っておく。

-> % ssh-keygen -t rsa

リネーム。~/.ssh/id_rsa.pub -> ~/.ssh/heroku.pub, ~/.ssh/id_rsa.pub -> ~/.ssh/heroku

次にHerokuへの認証を行なう。

-> % heroku login
Enter your Heroku credentials.
Email: hoge@example.com
Password (typing will be hidden):
Found the following SSH public keys:
1) hoge.pub
2) heroku.pub
3) fuga.pub
Which would you like to use with your Heroku account? 2
Uploading SSH public key /Users/hoge/.ssh/heroku.pub... done
Authentication successful.

Heroku用公開鍵を選ぶ。これで公開鍵がHerokuにアップロードされ、公開鍵認証が利用できるようになる。

次にアプリケーションのデプロイを行っていく。サンプルアプリケーションをcloneする。

-> % cd WORKING_DIRECTORY
-> % git clone git://github.com/heroku/ruby-sample.git
Cloning into 'ruby-sample'...
remote: Counting objects: 13, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 13 (delta 2), reused 8 (delta 1)
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.

次にheroku createで環境を作成する。これにより、Heroku上でのエンドポイントが生成される。

-> % heroku create
Creating shielded-sierra-5383... done, stack is cedar
http://shielded-sierra-5383.herokuapp.com/ | git@heroku.com:shielded-sierra-5383.git
Git remote heroku added

次にHeroku側にアプリケーションをpushする。remote originをherokuとしてpushする。

-> % git push heroku master
The authenticity of host 'heroku.com (50.19.85.132)' can't be established.
RSA key fingerprint is 8b:48:5e:67:0e:c9:16:47:32:f2:87:0c:1f:c8:60:ad.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'heroku.com,50.19.85.132' (RSA) to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

しかしここで認証エラーがでてしまった。ruby-sample.git/configをみてみたら以下のようになっていた。

# 抜粋
[remote "heroku"]
        url = git@heroku.com:shielded-sierra-5383.git
        fetch = +refs/heads/*:refs/remotes/heroku/*

heroku.comgitユーザでアクセスしようとしているので、このリクエストに先ほどの公開鍵を使うようにする。~/.ssh/configに以下のように追記する。

Host heroku.com
    User git
    HostName heroku.com
    identityfile ~/.ssh/heroku

鍵を設定したので、remote origin herokuについて確認する。

-> % git remote show heroku
Warning: Permanently added the RSA host key for IP address '50.19.85.156' to the list of known hosts.
* remote heroku
  Fetch URL: git@heroku.com:shielded-sierra-5383.git
  Push  URL: git@heroku.com:shielded-sierra-5383.git
  HEAD branch: (unknown)

無事リクエストが通った。ということでherokuブランチにpushする。

-> % git push heroku master
Counting objects: 13, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 1.28 KiB, done.
Total 13 (delta 2), reused 13 (delta 2)

-----> Ruby/Rack app detected
-----> Using Ruby version: ruby-2.0.0
-----> Installing dependencies using Bundler version 1.3.2
       Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin --deployment
       Fetching gem metadata from https://rubygems.org/..........
       Fetching gem metadata from https://rubygems.org/..
       Installing daemons (1.1.9)
       Installing eventmachine (0.12.10)
       Installing rack (1.4.1)
       Installing tilt (1.3.3)
       Installing sinatra (1.1.0)
       Installing thin (1.4.1)
       Using bundler (1.3.2)
       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> WARNINGS:
       You have not declared a Ruby version in your Gemfile.
       To set your Ruby version add this line to your Gemfile:
       ruby '2.0.0'
       # See https://devcenter.heroku.com/articles/ruby-versions for more information."
-----> Discovering process types
       Procfile declares types     -> web
       Default types for Ruby/Rack -> console, rake

-----> Compiled slug size: 25.8MB
-----> Launching... done, v3
       http://shielded-sierra-5383.herokuapp.com deployed to Heroku

To git@heroku.com:shielded-sierra-5383.git
 * [new branch]      master -> master

これでもうデプロイが完了したらしい。簡単。heroku openでデプロイ先のURLをブラウザで開くことができる。

-> % heroku open
Opening shielded-sierra-5383... done