(追記) sub directory をデプロイしたり、いちレポジトリで multi applications のデプロイをサポートする buildpack があると元同僚の id:kamatama41 から教えてもらったので追記しました。ありがとうございます!
以下のように Git root directory ではなく sub directory にデプロイしたいアプリケーションが存在する場合を想定。
$ tree -L 2
.
├── .git
├── backend
│ ├── Gemfile
│ ├── Gemfile.lock
│ ├── LICENSE
│ ├── README.md
│ ├── Rakefile
│ ├── app
│ ├── bin
│ ├── config
│ ├── config.ru
│ ├── db
│ ├── lib
│ ├── public
│ ├── test
│ └── vendor
└── frontend
git subtree split を使う
git subtree split で切り出して push する。
$ git push --force heroku `git subtree split --prefix backend HEAD`:master
--force
しているしあまり良くなさそう。もっと良いやり方が あると思う あったがとりあえずメモを残しておく
いま趣味で作っている Good First Issues を探す web application のバックエンドはこの方式でデプロイしてい る。 たが、heroku-buildpack-monorepo を使うやり方に切り替えた。
https://goofi.netlify.com/
github.com
(以下、追記)
buildpack を使うやり方
heroku-buildpack-select-subdir
heroku-buildpack-select-subdir を使うやり方。
Deploy Lerna Packages to Heroku | Jake Trent を参考にしてやってみた。ほとんど手順が同じなので詳述はしないが、実行したコマンドだけ載せておく。
$ cd backend && heroku create ohbarye-monorepo-test
$ heroku buildpacks:set -a ohbarye-monorepo-test https://github.com/Pagedraw/heroku-buildpack-select-subdir
$ heroku config:add BUILDPACK='backend=https://github.com/heroku/heroku-buildpack-ruby' -a ohbarye-monorepo-test
$ heroku git:remote -a ohbarye-monorepo-test
$ git push heroku master
$ open https://ohbarye-monorepo-test.herokuapp.com
Procfile の書き方
1点だけハマったのは、Procfile に書いた起動コマンドでアプリケーションの起動に失敗したこと。
web: bundle exec rails server -p $PORT
これはこのコマンドを実行するコンテキストが root directory のままだったことによる。この buildpack はそこまで面倒を見てくれない。Procfile に以下のように cd コマンドを追記することで解決できた。
web: cd backend && bundle exec rails server -p $PORT
heroku-buildpack-monorepo
heroku-buildpack-monorepo を使うやり方。
こちらも実行したコマンドだけ載せておく。
$ cd backend && heroku create ohbarye-monorepo-test
$ heroku buildpacks:add -a ohbarye-monorepo-test https://github.com/lstoll/heroku-buildpack-monorepo
$ heroku config:add APP_BASE=backend -a ohbarye-monorepo-test
$ heroku buildpacks:add heroku/ruby
$ heroku git:remote -a ohbarye-monorepo-test
$ git push heroku master
$ open https://ohbarye-monorepo-test.herokuapp.com
Procfile の書き方
heroku-buildpack-select-subdir
と異なり cd backend
のようなディレクトリ移動は必要ない。
web: bundle exec rails server -p $PORT
これは heroku-buildpack-monorepo
が APP_BASE
環境変数に指定したディレクトリをそのまま root に持ってきているからだと、 git push 時の log を見るとわかる。実際に buildpack のコードを見てもわかる(けっこう力技だ)。
$ git push heroku master
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 363 bytes | 363.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Monorepo app detected
remote: Copied backend to root of app successfully