Herokuに簡単なSinatraアプリをデプロイしてみた

参考にしたのはこのページ。

 cf. herokuでsinatraアプリをデプロイしてみた

順を追ってやってみる。

ログインと公開鍵の登録

Heroku Toolbelt のインストールは終わっているので、キーの登録から。

^o^ > heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Could not find an existing public key.
Would you like to generate one? [Yn] Y
Generating new SSH public key.
Uploading SSH public key C:/Users/hiro/.ssh/id_rsa.pub... done
Authentication successful.

ログインしようとすると public key がない、作るか?といわれるので、Y と答える。これでキーができたはず。
もう一回ログイン。

^o^ > heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.

今度はログインできた。じゃ次、public key(公開鍵)の登録。

^o^ > heroku keys:add
Found existing public key: C:/Users/takatoh/.ssh/id_rsa.pub
Uploading SSH public key C:/Users/takatoh/.ssh/id_rsa.pub... done

よし、OK。

簡単なSinatraアプリを作る

チョー簡単な、Hello, Heroku と表示するだけのアプリ。

Gemfile:

source "https://rubygems.org"

gem "sinatra"

Procfile:

web: bundle exec rackup config.ru -p $PORT

sample.rb:

require 'bundler/setup'
require 'sinatra/base'

class Sample < Sinatra::Base
  get "/" do
    "Hello, Heroku."
  end
end

config.ru:

$:.unshift(File.dirname(__FILE__))
require 'sample'

run Sample 

.gitignore:

.bundle

まずはこれだけを Git のリポジトリに commit する。

^o^ > git init
Initialized empty Git repository in C:/Users/takatoh/Documents/w/takatoh-sample/
.git/

^o^ > git add .

^o^ > git commit -m "First commit."
[master (root-commit) fa02bb8] First commit.
 5 files changed, 19 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Gemfile
 create mode 100644 Procfile
 create mode 100644 config.ru
 create mode 100644 sample.rb

ローカルで試してみる

^o^ > foreman start
'foreman' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。

あれ、ダメじゃん。foreman とやらはインストールされてないのかな。まあいいや。

デプロイしてみる

^o^ > heroku create
Creating limitless-sea-3233... done, stack is cedar
http://limitless-sea-3233.herokuapp.com/ | [email protected]:limitless-sea-3233.git

Git remote heroku added

^o^ > git remote -v
heroku  [email protected]:limitless-sea-3233.git (fetch)
heroku  [email protected]:limitless-sea-3233.git (push)

heroku create にアプリ名を与えないと適当な名前がつくらしい。上では limitless-sea-3233 ってのがそう。git のリモートリポジトリに heroku が追加されてるのがわかる。
それじゃ、Heroku にデプロイしてみよう。

^o^ > git push -u heroku master
Permission denied (publickey).
fatal: Could not read from remote repository.

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

あれ、だめだ。なんでだ。
しばらく考えて、ログを見て気がついた。さっき登録したキーは C:/Users/takatoh/.ssh/ に保存されている。けど、普段 git でつかっているキーは C:\Users\takatoh\Documents\.ssh にある。これじゃないかと思って、(バックアップを作ったうえで)上書きしてみた。今度はどうかな。

^o^ > git push -u heroku master
Initializing repository, done.
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 629 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)

-----> Ruby app detected
-----> Compiling Ruby/NoLockfile
 !
 !     Gemfile.lock required. Please check it in.
 !

 !     Push rejected, failed to compile Ruby app

To [email protected]:limitless-sea-3233.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:limitless-sea-3233.git'

まだだめだ。今度はなんだ。どうやら Gemfile.lock が必要で、チェックインしろといっている。

^o^ > git status
On branch master
Untracked files:
  (use "git add ..." to include in what will be committed)

        Gemfile.lock

nothing added to commit but untracked files present (use "git add" to track)

^o^ > git add Gemfile.lock
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.

^o^ > git commit -m "Gemfile.lock: Add."
[master f84a54a] Gemfile.lock: Add.
warning: LF will be replaced by CRLF in Gemfile.lock.
The file will have its original line endings in your working directory.
 1 file changed, 17 insertions(+)
 create mode 100644 Gemfile.lock

さあ、今度はどうだ。

^o^ > git push -u heroku master
Initializing repository, done.
Counting objects: 10, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 1009 bytes | 0 bytes/s, done.
Total 10 (delta 1), reused 0 (delta 0)

-----> Ruby app detected
-----> Compiling Ruby/Rack
-----> Using Ruby version: ruby-2.0.0
-----> Warning:
       Removing `Gemfile.lock` because it was generated on Windows.
       Bundler will do a full resolve so native gems are handled properly.
       This may result in unexpected gem versions being used in your app.
       In rare occasions Bundler may not be able to resolve your dependencies at
 all.
       https://devcenter.heroku.com/articles/bundler-windows-gemfile
-----> Installing dependencies using 1.5.2
       Running: bundle install --without development:test --path vendor/bundle -
-binstubs vendor/bundle/bin -j4
       Fetching gem metadata from https://rubygems.org/...........
       Fetching additional metadata from https://rubygems.org/..
       Resolving dependencies...
       Using bundler (1.5.2)
       Installing rack (1.5.2)
       Installing tilt (1.4.1)
       Installing rack-protection (1.5.3)
       Installing sinatra (1.4.5)
       Your bundle is complete!
       Gems in the groups development and test were not installed.
       It was installed into ./vendor/bundle
       Bundle completed (8.62s)
       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 inform
ation.

       Removing `Gemfile.lock` because it was generated on Windows.
       Bundler will do a full resolve so native gems are handled properly.
       This may result in unexpected gem versions being used in your app.
       In rare occasions Bundler may not be able to resolve your dependencies at
 all.
       https://devcenter.heroku.com/articles/bundler-windows-gemfile
-----> Discovering process types
       Procfile declares types -> web
       Default types for Ruby  -> console, rake

-----> Compressing... done, 12.1MB
-----> Launching... done, v4
       http://limitless-sea-3233.herokuapp.com/ deployed to Heroku

To [email protected]:limitless-sea-3233.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from heroku.

うまくいったかな。

^o^ > heroku open
Opening limitless-sea-3233... done
hello-heruku

うまくいった!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください