Skip to content

Instantly share code, notes, and snippets.

@georgeOsdDev
Last active March 15, 2023 06:54
Show Gist options
  • Save georgeOsdDev/a1c9fa0925b43f70c28e384d5e7ac806 to your computer and use it in GitHub Desktop.
Save georgeOsdDev/a1c9fa0925b43f70c28e384d5e7ac806 to your computer and use it in GitHub Desktop.

Tips for speed up Oryx remote build/deploy time for App Service on Linux for Node.js

Cache node_modules for each hash of package-lock.json, Node version and NPM version

/home/site/deploy_cache/node_modules-${md5 hash_of_package_lock.json}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz

graph TB;
  Start([Start]);
  Start-->CheckLockVersion{if package-lock.json changed};
  CheckLockVersion-->|True| DefaultInstall;
  CheckLockVersion-->|False| CheckCachedModule;
  CheckCachedModule{if cached module found};
  CheckCachedModule-->|True| CopyCache;
  CheckCachedModule-->|False| DefaultInstall;

  DefaultInstall[npm install]-->DefaultBuild;
  DefaultBuild[npm build];
  DefaultBuild-->OryxCopyNodeModules[Oryx make node_modules.tar.gz];
  OryxCopyNodeModules-->CreateCache;
  CreateCache[cp ./node_modules.tar.gz $cached_node_modules_archive]-->End;

  CopyCache[tar -xzf $cached_node_modules_archive -C ./node_modules]-->CacheBuild;
  CacheBuild[npm build];
  CacheBuild-->RemoveNodeModule[rm -rf ./node_modules]-->End;

  End([End]);
Loading

Oryx/nodejs.md at main · microsoft/Oryx

Application Settings

[
  {
    "name": "CUSTOM_BUILD_COMMAND",
    "value": "sh azureappservice_custombuild.sh",
    "slotSetting": false
  },
  {
    "name": "POST_BUILD_COMMAND",
    "value": "sh azureappservice_postbuild.sh",
    "slotSetting": false
  },
  {
    "name": "PRE_BUILD_COMMAND",
    "value": "sh azureappservice_prebuild.sh",
    "slotSetting": false
  }
]
#!/usr/bin/env bash
# run this script with CUSTOM_BUILD_COMMAND(https://github.com/microsoft/Oryx/blob/main/doc/runtimes/nodejs.md#build)
echo "custombuild started"
echo "current working dir: $PWD"
NODE_VERSION=$(node --version)
NPM_VERSION=$(npm --version)
if [ -f "./USING_CACHE" ]; then
echo "Using cached node_modules, skipping npm install"
npm run build
# Avoid compress and copies of node_modules by Oryx
echo "Remove node_modules"
rm -rf ./node_modules
# Keep cached node_modules as /home/site/node_modules.tar.gz
hash=$(md5sum package-lock.json | awk '{print $1}')
echo "package-lock.json hash: ${hash}"
cached_node_modules_archive="/home/site/deploy_cache/node_modules-${hash}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz"
cp -f $cached_node_modules_archive /home/site/node_modules.tar.gz
else
echo "cache not found"
echo "Running 'npm install --unsafe-perm --no-audit --no-fund'"
npm install --unsafe-perm --no-audit --no-fund
echo "Running 'npm run build'..."
npm run build
fi
echo "custombuild end"
#!/usr/bin/env bash
# /home/site/wwwroot/node_modules will be compressed as /home/site/wwwroot/node_modules.tar.gz by Oryx
# This script will copy /home/site/wwwroot/node_modules.tar.gz to /home/site/deploy_cache/node_modules-${md5 hash_of_package_lock.json}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz
# run this script with POST_BUILD_COMMAND(https://github.com/microsoft/Oryx/blob/main/doc/runtimes/nodejs.md#build)
echo "postbuild started"
echo "current working dir: $PWD"
NODE_VERSION=$(node --version)
NPM_VERSION=$(npm --version)
if [ -f "./package-lock.json" ]; then
# get hash value of package-lock.json
hash=$(md5sum package-lock.json | awk '{print $1}')
echo "package-lock.json hash: ${hash}"
cached_node_modules_archive="/home/site/deploy_cache/node_modules-${hash}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz"
if [ -f $cached_node_modules_archive ]; then
echo "$cached_node_modules_archive already exists"
else
echo "copying ./node_modules.tar.gz to $cached_node_modules_archive"
mkdir -p /home/site/deploy_cache
cp ./node_modules.tar.gz $cached_node_modules_archive
fi
fi
echo "postbuild end"
#!/usr/bin/env bash
# Copy /home/site/deploy_cache/node_modules-${md5 hash_of_package_lock.json}-}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz to working directory
# tar.gz will be cereated by postbuild.sh on previous deployment
# run this script with PRE_BUILD_COMMAND(https://github.com/microsoft/Oryx/blob/main/doc/runtimes/nodejs.md#build)
echo "prebuild started"
echo "current working dir: $PWD"
NODE_VERSION=$(node --version)
NPM_VERSION=$(npm --version)
if [ -f "./package-lock.json" ]; then
# get hash value of package-lock.json
hash=$(md5sum package-lock.json | awk '{print $1}')
echo "package-lock.json hash: ${hash}"
cached_node_modules_archive="/home/site/deploy_cache/node_modules-${hash}-node${NODE_VERSION}-npm${NPM_VERSION}.tar.gz"
if [ -f $cached_node_modules_archive ]; then
echo "$cached_node_modules_archive found, extaracting to working dir"
mkdir ./node_modules
tar -xzf $cached_node_modules_archive -C ./node_modules
touch ./USING_CACHE
else
echo "$cached_node_modules_archive not found"
fi
fi
echo "prebuild end"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment