鯛めしのお釜

ウェブに関する技術的なメモを書いていきます。

チュートリアルに沿って Rails5.0 でウェブアプリを作る(第1章その2:Heroku へのデプロイ)

今回は、前回用意した Rails アプリをHeroku へデプロイする。ということで Heroku の設定とデプロイ内容についてメモしておきたい。

devcenter.heroku.com

 

---

作業の流れは次の通り。

  1. CentOS7 に Heroku CLI をインストールする
  2. Heroku にデプロイする

Heroku を使うのは1〜2年ぶりくらいだったが、料金体系が変わって、無料プランでもまた 24 時間稼働できるようになっていたのを知ってちょっと嬉しかった。

とはいえ複数アプリをまたいで上限時間があるとか色々ようわからんようになってて説明ページをそっ閉じした。

 

---

1. CentOS7 にHeroku CLI をインストールする

基本的には上記ドキュメントに書いてある通りで、その内容に沿って CentOS7 上で次の作業をおこなった。

wget -qO- https://toolbelt.heroku.com/install.sh | sh

上記コマンドを叩くと、次のコマンドを打てと指示が出るのでその通りにする。

$ echo 'PATH="/usr/local/heroku/bin:$PATH"' >> ~/.profile

$ heroku --version

heroku-toolbelt/3.43.15 (x86_64-linux) ruby/2.3.3

heroku-cli/5.5.8-df05424 (linux-amd64) go1.7.4

これで完了。

なお、インストール時には sudo が必要なため、入れていない場合は yum install しておくこと。

$ yum -y install sudo

 

---

2. Heroku にデプロイする

まずは Heroku へのデプロイ準備をする。

$ heroku login

$ cd {デプロイしたいディレクトリ}

$ heroku create

この時点で Heroku 側に箱だけ用意されて URL が発行される。

それでは中身をデプロイする。

$ git push heroku master

fatal: 'heroku' does not appear to be a git repository

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

ええ・・・。

ドキュメントを調べてみたところ、もしやこれか?

devcenter.heroku.com

ということで試してみる。

$ heroku git:remote -a {割り振られた Heroku の ID}

set git remote heroku to https://git.heroku.com/{割り振られた Heroku の ID}.git

 

$ git remote -v

heroku https://git.heroku.com/{割り振られた Heroku の ID}.git (fetch)

heroku https://git.heroku.com/{割り振られた Heroku の ID}.git (push)

origin https://github.com/redsnapperrice/rails5_test.git (fetch)

origin https://github.com/redsnapperrice/rails5_test.git (push)

おお〜。

これで git push すればいけるはず。

$ git push heroku master

(中略)

remote:  !     No default language could be detected for this app.

remote: HINT: This occurs when Heroku cannot detect the buildpack to use for this application automatically.

remote: See https://devcenter.heroku.com/articles/buildpacks

失敗した。

指示に従い https://devcenter.heroku.com/articles/buildpacks を確認、言われるがまま次のように buildpack を設定して再チャレンジ。

$ heroku buildpacks:set heroku/ruby

$ git push heroku master

remote: -----> Failed to detect set buildpack https://codon-buildpacks.s3.amazonaws.com/buildpacks/heroku/ruby.tgz

remote: More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure

 buildpack の設定に失敗している???

 調べてみたらこんな投稿を見つけた。

stackoverflow.com

まさか私も同じようにディレクトリ階層の持ち方が悪いんじゃないかと気付いた。

というのも、

  • GitHub は「rails5_test」直下に、rails new した時にできた「hello_app」ディレクトリを持っている(そこに全ソースを置いている)
  • Heroku は「rails5_test」直下に向いている

という状態になっていたため、Heroku 側からしてみれば「ソースどこやねん?」と迷子になっていたと思われる。いやそもそもそんな中途半端なディレクトリ構造のまま放置した私が悪い・・・。

そこで「rails5_test」直下にソースを全て移し、GitHub と Heroku それぞれ push してみた。

f:id:red_snapper:20161210220542p:plain

きた!!!

やれやれ、これでとりあえず第1章はおしまい。