もうすでに何日も何日も経過していますが、これは Heroku Advent Calendar 2020の 5日目、6日目はHeroku Master @zunda さんのもう少しSIGTERMと仲良くするです。毎度ありがとうございます。
最上級の小ネタで攻めます。本番は明日です。
Heroku 史上最速に静的コンテンツをアップロードする
「Heroku 静的」とかで検索すると、buildpack を使ったり index.php から index.html 読み込ませるようなやり方をしているケースが多いです。多い。実は、php の buildpack を指定して、ダイレクトに index.html させる方法はもっと単純だったりします。
答え
$ git init
$ heroku create
$ touch composer.json
$ echo 'test' > index.html
$ git add . && git commit -m 'first commit'
$ git push heroku main
$ heroku open
どうだ。
※ git config --global init.defaultBranch main 済です
解説
Heroku は、official buildpack のとあるチェックを利用して、git push されたソースコードがどの言語であるかを判定しています。default で apache をすんなり動かしてくれる php の buildpack は静的コンテンツの配信にちょうど良いので、いかにして php であるかを認識させるかが勝負です。ぶっちゃけ buildpack 指定する方法もありますが、こっちの方がコードが短い(ぉぃ
そして、php であることの判断基準は composer.json か index.php のいずれかが、プロジェクトルートディレクトリにあることです!! Ω ΩΩ< な、なんだってー!!
ファイルの中身は検閲していないので、ファイルさえ置いておけば十分です。index.php をおいてしまうと、apache がそっちを見に行ってしまうので、composer.json の空ファイルを置いておくことで、index.html を読んでくれる、という仕様です。
※ 2020/12/22 現在
build & deploy の結果
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote:
remote: NOTICE: Your 'composer.json' is completely empty!
remote:
remote: A completely empty file is not a valid JSON document.
remote:
remote: Heroku automatically corrected this problem, but it is strongly
remote: recommended you change the contents to at least '{}'.
remote:
remote: For documentation on Composer and dependency management, check
remote: out the introduction at https://getcomposer.org/doc/00-intro.md
remote:
remote: -----> Bootstrapping...
remote: -----> Installing platform packages...
remote: NOTICE: No runtime required in composer.lock; using PHP ^7.0.0
remote: - php (7.4.13)
remote: - composer (1.10.19)
remote: - apache (2.4.46)
remote: - nginx (1.18.0)
remote: -----> Installing dependencies...
remote: Composer version 1.10.19 2020-12-04 09:14:16
remote: -----> Preparing runtime environment...
remote: NOTICE: No Procfile, using 'web: heroku-php-apache2'.
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 14.1M
remote: -----> Launching...
remote: Released v3
remote: https://xxxxxx-yyyyy-000000.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
何も指定していないので、デフォルトの heroku-php-apache2 が使われていたり、composer.json の中身が空だよって言われてます。