How to run NPM scripts (or do other stuff) when deploying to azure from github
development, web, configuration, azure, node, and github
How to modify the deployment process of microsoft azure is one of those things that I keep forgetting how to do, or find myself making more complicated than it has to be.
This post assumes that you’re deploying with continuous deployment from github, but it should be the same for most of the deployment processes that you can use with azure, except for maybe ftp deployment.
The short answer on how to do this is: add a deploy.cmd, or .sh depending on what you’re comfortable with, to your repository. This should contain the code that you want to run during deployment. Then add a .deployment file pointing to your deploy.[cmd,sh] file and presto.
Seems easy enough right? Well the problem here is that azure won’t do anything other than what you tell it to in the deploy file, since adding this is basically you telling azure that you’re taking over the deployment. The easiest way to get around this is to get a hold of the default azure deploy.cmd file.
One way to get that is to retrieve it from an existing node app running in azure via ftp. Another way is to get it using the azure cli.
npm install azure-cli -g
azure site deploymentscript --node
This will generate a .deployment and deploy.cmd file in your current directory. Put these in your repositorys root directory and modify the deploy.cmd file to do what you want.
##Example: run NPM script after deployment and npm dependencies install.
Let’s first take a look at the code that we’re going to add to our deploy.cmd file.
The first two lines are just for our sake, the first line is just a comment in the file, and the echo line will show up in azure logs.
The pushd "%DEPLOYMENT_TARGET%" line is for moving into the deployment directory (wwwroot) since we’re currently in the repository directory.
We then run call :ExecuteCmd !NPM_CMD! run myscipt to execute our script (change myscript to the name you’ve given your script in package.json).
We then run popd to reset which directory we’re in, as to not interfere with the rest of the deployment process.
Well, were do with put this code snippet then?
##deploy.cmd
Here’s a pristine azure node deploy file. We would insert our code snippet as the 4th step under the deployment step (line #107)