Created
July 1, 2016 14:16
-
-
Save kieranajp/374a3b7abce357d64b6b17bbd6b9f579 to your computer and use it in GitHub Desktop.
Caches NPM install on S3 based on whether the shrinkwrap's changed.
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 | |
########## | |
# Run this to use a cached version of node_modules stored on Amazon S3. | |
# If npm-shrinkwrap has changed, the cache is updated. | |
# | |
# Ensure the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environtment variables are set | |
# Then run the script. | |
# | |
# Example: | |
# AWS_ACCESS_KEY_ID=xxx AWS_SECRET_ACCESS_KEY=xxx ./npm_s3 | |
# | |
# These are already set on Codeship. | |
########## | |
# Config | |
S3_BUCKET="" | |
S3_URL="https://$S3_BUCKET.s3-eu-west-1.amazonaws.com" | |
S3_FOLDER="npm-packages" | |
echo "Pulling shrinkwrap checksum from S3..." | |
# Check whether shrinkwrap file has changed | |
LOCAL_SHA=$(cat npm-shrinkwrap.json | shasum -p) | |
AWS_SHA=$(curl $S3_URL/$S3_FOLDER/npm-shrinkwrap.json | shasum -p) | |
echo $LOCAL_SHA | |
echo $AWS_SHA | |
if [ "$LOCAL_SHA" == "$AWS_SHA" ] | |
then | |
echo "Checksums match, pulling node_modules from S3..." | |
# Grab existing node_modules from S3 | |
curl ${S3_URL}/${S3_FOLDER}/node_modules.tar.gz | tar xz | |
echo "Done!" | |
exit 0 | |
fi | |
echo "Checksum invalid, proceeding to build..." | |
# Build and tar up node_modules | |
source ~/.nvm/nvm.sh | |
nvm use 6 | |
npm set progress=false | |
echo "Installing node_modules..." | |
npm install | |
# Detect platform | |
PLATFORM=$(uname) | |
echo "'$PLATFORM' detected, downloading appropriate gof3r binary..." | |
GOF3R_URL='https://github.com/rlmcpherson/s3gof3r/releases/download/v0.5.0' | |
GOF3R_ARCHIVE='' | |
GOF3R_FORMAT='' | |
if [[ "$PLATFORM" == 'Linux' ]]; then | |
GOF3R_ARCHIVE='gof3r_0.5.0_linux_amd64' | |
GOF3R_FORMAT='tar.gz' | |
elif [[ "$PLATFORM" == 'Darwin' ]]; then | |
GOF3R_ARCHIVE='gof3r_0.5.0_darwin_amd64' | |
GOF3R_FORMAT='zip' | |
fi | |
curl -L $GOF3R_URL/$GOF3R_ARCHIVE.$GOF3R_FORMAT | tar xzf - -C bin/ --strip-components=1 $GOF3R_ARCHIVE/gof3r | |
put() { | |
echo "Uploading $1" | |
bin/gof3r put --endpoint s3-eu-west-1.amazonaws.com -b $S3_BUCKET -k "/$S3_FOLDER/$1" -m x-amz-acl:public-read | |
} | |
echo "Compressing node_modules..." | |
tar czf - node_modules | put node_modules.tar.gz | |
cat npm-shrinkwrap.json | put npm-shrinkwrap.json | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment