LoginSignup
0
0

More than 5 years have passed since last update.

Azure Web AppsでNode.jsな環境でデプロイをカスタム

Posted at

背景

諸事情によりAzure Web Appsを使いはじめたが、デプロイ時にDBのmigrate走らせようとしたらよくわからんかった。
解決したので手順メモ。

TL;DR

azure-cliを入れて

$ azure site deploymentscript --node

すれば.deploymentとdeploy.shが吐かれるのでdeploy.shイジる。

流れ

具体的にやりたいことはsequelize-cliを使って以下を実行すること。

$ sequelize db:migrate

npm scriptsでやってみる(失敗)

とりあえずnpm scriptsに

package.json
{
  "scripts": {
    "start": "npm run db:migrate && node www.js",
    "test": "NODE_ENV=test ./node_modules/.bin/mocha --recursive",
    "db:migrate": "./node_modules/.bin/sequelize db:migrate",
    "db:reset": "./node_modules/.bin/sequelize db:migrate:undo:all"
  }
}

とかいてみたけど、Web AppsはWindowsな環境なので動かない。

.deploymentをかいてみる(失敗)

デプロイ時の挙動をカスタムするやり方はkudu(Web Appsのバックエンド)のドキュメント に書いてあり、
「.deploymentファイル置いてイジる」と書いてあるので、
「PowerShellとかまじかよ」と思いながらも

[config]
command = powershell -NoProfile -NoLogo -ExecutionPolicy Unrestricted -Command "& "node $pwd\node_modules\.bin\sequelize db:migrate" 2>&1"

としてみるが動かない。
エラーログを見ると実行ディレクトリがgitのワークディレクトリらしく、node_modulesがないのでsequelize-cliが実行できない。

azure site deploymentscriptする(解決)

以下はGulpの例だが、基本的にコレに則る。

Using Gulp in Node.js Azure WebApps
https://blogs.msdn.microsoft.com/azureossds/2015/10/22/using-gulp-in-node-js-azure-webapps/

以下を実行すると.deploymentとdeploy.shが吐かれる。
bash動いたのか...

$ azure site deploymentscript --node

最後の方に以下のようにあるので

##################################################################################################################################
# Deployment
# ----------

echo Handling node.js deployment.

# 1. KuduSync
if [[ "$IN_PLACE_DEPLOYMENT" -ne "1" ]]; then
  "$KUDU_SYNC_CMD" -v 50 -f "$DEPLOYMENT_SOURCE" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;.deployment;deploy.sh"
  exitWithMessageOnError "Kudu Sync failed"
fi

# 2. Select node version
selectNodeVersion

# 3. Install npm packages
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  cd "$DEPLOYMENT_TARGET"
  eval $NPM_CMD install --production
  exitWithMessageOnError "npm failed"
  cd - > /dev/null
fi

##################################################################################################################################

# db migration
cd "$DEPLOYMENT_TARGET"
eval $NPM_CMD run sequelize db:migrate

と足した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0