Last active
June 16, 2017 14:51
-
-
Save tlbdk/f3f306640b4a1c6756277072152e7c02 to your computer and use it in GitHub Desktop.
node_modules folder caching on build servers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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