💎

Azure App ServiceにRailsをデプロイしたい

2022/03/05に公開

Summary

AzureのApp Serviceの勉強を兼ねて、LocalにあるRailsをデプロイしてうごかすところまでトライしました。
HerokuやAWSなどは非常に過去の事例が多く、難なく最後まで通るイメージですがAzure App Serviceにデプロイしている事例は激しく少なく、慣れない環境のためたくさんつまずきポイントがあったので、ログとして残したいと思います。

参考URL

基本的にはOfficialの以下の記事を参考に進めました。
https://docs.microsoft.com/ja-jp/azure/app-service/configure-language-ruby

ハマり1 - Rubyの細かなバージョンが合って無くてエラーになる

チュートリアルを参考に、WebappのCreate時には以下のようにRubyのVersionを指定していました。
(LocalのRailsが2.7だったため)

az webapp create (略) --runtime 'RUBY|2.7'

しかし、git push でインストールが走ると bundle install で怒られます。
よく見ると、azure 側は2.7.3のRubyが入っていたようでした。2.7と指定すると 2.7.0 になるのかなって勝手に想像していたので、ローカル側を2.7.0 にしていたんですが不一致で怒られました。
2.7.3 に合わせる事でBundle installが完了するようになりました。

ハマり2 - DBのマイグレーションの仕方がわからなかった件

Rubyのバージョンをあわせて、無事 git push azure main が通るようになり、早速ブラウザからアクセスしてみます。
しかし、今度は
We're sorry, but something went wrong.
と表示されてしまいました。
ひとまずAzure CLIで

az webapp log config --resource-group myResourceGroup --name myRails --docker-container-logging filesystem --level Verbose

を実行し、 az webapp log tail を試してみました。
しかしこれはDockerのログやSTDOUTなログだけ流れるようで、RailsのProduction logが流れるわけではないようでした。
(そらそうやろ、って感じなんですがw)

Azure CLIから取得する簡単な方法が見つからなかったので、一回サーバーに覗きに行くことにします。

Azure Portal からSSH

Azure portal から作成したばかりのApp Serviceを開くと、メニューにSSHがありコンテナに直接入れるようでした。
SSHでつないで階層をさぐってみると、 /home/site/wwwroot がRAILS_ROOTになっている事を発見。
このディレクトリの直下のlog から、無事production.log を覗く事ができました。

root@abe4373480d2:/home/site/wwwroot# ls -la

total 328

drwxrwxrwx 2 nobody nogroup   4096 Feb 21 10:28 .
drwxrwxrwx 2 nobody nogroup      0 Feb 21 16:14 ..
rwxrwxrwx 1 nobody nogroup 9 Feb 21 10:28 .browserslistrc
rwxrwxrwx 1 nobody nogroup 761 Feb 21 10:28 .gitignore
rwxrwxrwx 1 nobody nogroup 6 Feb 21 15:57 .ruby-version
rwxrwxrwx 1 nobody nogroup 1963 Feb 21 11:33 Gemfile
rwxrwxrwx 1 nobody nogroup 5779 Feb 21 11:33 Gemfile.lock
rwxrwxrwx 1 nobody nogroup 374 Feb 21 10:28 README.md
rwxrwxrwx 1 nobody nogroup 227 Feb 21 10:28 Rakefile
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 app
rwxrwxrwx 1 nobody nogroup 1722 Feb 21 10:28 babel.config.js
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 bin
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 config
rwxrwxrwx 1 nobody nogroup 130 Feb 21 10:28 config.ru
drwxrwxrwx 2 nobody nogroup      0 Feb 21 16:03 db
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 lib
drwxrwxrwx 2 nobody nogroup      0 Feb 22 00:06 log
rwxrwxrwx 1 nobody nogroup 355 Feb 21 10:28 package-lock.json
rwxrwxrwx 1 nobody nogroup 297 Feb 21 10:28 package.json
rwxrwxrwx 1 nobody nogroup 224 Feb 21 10:28 postcss.config.js
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 public
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 storage
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 test
drwxrwxrwx 2 nobody nogroup      0 Feb 21 11:36 tmp
drwxrwxrwx 2 nobody nogroup      0 Feb 21 10:28 vendor
rwxrwxrwx 1 nobody nogroup 305527 Feb 21 10:28 yarn.lock

ところで、今回は何でつまづいていたかというと

Completed 500 Internal Server Error in 32ms
(ActiveRecord: 9.9ms | Allocations: 582)
F, [2022-02-22T08:31:08.903850 #157] FATAL -- : [53499042-ae0f-48db-bb5a-a57dd2cfa9e1]
[53499042-ae0f-48db-bb5a-a57dd2cfa9e1] ActionView::Template::Error (SQLite3::SQLException: no such table: users):

とあり、SQLite3のMigrationが終わっていないため怒られてました。
WebappにPushしてインストールしてる作業では db:migrate もやってくれてるんだと何故か勝手に思い込んでいたんですが、どうやら違っていたようでした。

そして、 bundle exec rails db:migrate をAzure CLIからなんとかして実行できないもんかと血眼になって探したんですが、どう頑張ってもその方法は見つからず。。
PostgreSQLを別途用意し、セットアップしてから利用する方法は公式にも掲載されていたのですが SQLite3を遠隔でMigrateする方法だけは見つけられませんでした。

再びコンテナを漁り、 deploy時の実行Shellを発掘

デプロイ中に色々走らせてるコマンドがどこかに記述されているはずや・・
と再びコンテナを漁りましたところ、

root@23be1a8166f8:/home/site/deployments/tools# ls
deploy.sh  deploymentCacheKey

という具合に、 deploy.sh を発見し中身を野覗いてみるとどうやら正解なイキフンです。

この中で、 asset precompile も実施していたのでこれの直前に差し込んでみたらいけそうな感じがしたのでこうしてみました。
(最初の2行を勝手に追加)

echo "db:migrate"
bundle exec rake db:migrate

if [ "$ASSETS_PRECOMPILE" == true ]; then
  echo "running rake assets:precompile"
  bundle exec rake --trace assets:precompile
fi
exitWithMessageOnError "precompilation failed"

すると、無事にマイグレーションを実施することができRailsを動かす事ができました。
まぁ、通常は複数のフロントを何個も作成してDBは分けると思うんでこんな事で困る状況にはならなそうではあるんですが、1個だけDevで動かして動作確認したい! とかそういう時はいいかもしれないですね。

余談 - Dev環境でデプロイする場合

App Service側にDevlopment環境でデプロイする場合は、初期設定では外からアクセスできないようでした。

However, this setting alone causes the Rails server to start in development mode, which accepts localhost requests only and isn't accessible outside of the container. To accept remote client requests, set the APP_COMMAND_LINE app setting to rails server -b 0.0.0.0.

https://docs.microsoft.com/en-us/azure/app-service/configure-language-ruby#run-in-non-production-mode

おわり

Discussion