Skip to content

Instantly share code, notes, and snippets.

@tlbdk
Last active June 16, 2017 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlbdk/f3f306640b4a1c6756277072152e7c02 to your computer and use it in GitHub Desktop.
Save tlbdk/f3f306640b4a1c6756277072152e7c02 to your computer and use it in GitHub Desktop.
node_modules folder caching on build servers
#!/bin/bash
# set ACCESS_TOKEN with gcloud auth print-access-token
UPLOAD_URL="https://storage.googleapis.com/<project>-npmcache"
SCRIPT_PATH=$(cd `dirname ${0}`; pwd)
YARN_LOCK_HASH=`cat yarn.lock package.json|sha1sum|cut -d' ' -f1`
if [ ! -d "node_modules" ]; then
echo "Trying to extract cached yarn dump"
curl -L -v "$UPLOAD_URL/$YARN_LOCK_HASH.tar.gz" | tar -xz
fi
if [ -f "node_modules/$YARN_LOCK_HASH.yarncache" ]; then
echo "Skipping yarn because we already got a node_modules folder"
else
yarn
if [[ -n "$ACCESS_TOKEN" ]]; then
echo "Creating $YARN_LOCK_HASH.tar.gz of node_modules"
touch "node_modules/$YARN_LOCK_HASH.yarncache"
tar -czf $YARN_LOCK_HASH.tar.gz node_modules
curl -L -v -H "Authorization: Bearer $ACCESS_TOKEN" --upload-file $YARN_LOCK_HASH.tar.gz "$UPLOAD_URL/"
else
echo "No ACCESS_TOKEN set, so not uploading $YARN_LOCK_HASH.tar.gz";
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment