Skip to content

Instantly share code, notes, and snippets.

@igorkosta
Last active November 19, 2017 20:44
Show Gist options
  • Save igorkosta/02aa04d3fe82267962579b863a8ed48a to your computer and use it in GitHub Desktop.
Save igorkosta/02aa04d3fe82267962579b863a8ed48a to your computer and use it in GitHub Desktop.
Deploy gzip-ped `vuejs` to S3
#!/bin/bash
# make file executable
set -e
instructions ()
{
echo "*********************************************"
echo "Run this script as an npm task *"
echo "$ npm run deploy <env> *"
echo "env: eg. stage, prod *"
echo "for example: $ npm run deploy stage *"
echo "*********************************************"
}
if [ $# -eq 0 ]; then
instructions
exit 1
elif [ "$1" == stage ]; then
BUCKET="<name-of-your-s3-bucket-for-stage>"
elif [ "$1" == prod ]; then
BUCKET="<name-of-your-s3-bucket-for-production>"
else
instructions
exit 1
fi
DIST="dist"
TMP="tmp"
echo "remove tmp directory created previously"
rm -rf $TMP
# mv all the files needed to run the app to a `tmp` directory
echo "create a tmp directory and copy all the files there"
rsync -avz --include 'manifest.*.js' --exclude '*.js' --exclude '*.css' --exclude '*.map' dist tmp
# omit all .gz file extensions
echo "Omit the .gz file extension"
for x in $(find tmp -name \*.gz); do
mv $x $(echo "$x" | sed 's/\.gz$//')
done
# clean the bucket
echo "Clean the bucket from previous vomit"
aws s3 rm s3://$BUCKET --recursive
# sync to the S3 bucket
# aws-cli is needed
echo "Copying files over to S3"
aws s3 sync ./$TMP/$DIST s3://$BUCKET --acl public-read
# set the metadata to notify s3 that we're using gzip
echo "Set 'Content-Encoding:gzip' on affected files"
aws s3 cp s3://$BUCKET/static/js s3://$BUCKET/static/js --recursive --exclude 'manifest*' --metadata-directive REPLACE --content-encoding 'gzip' --acl public-read
aws s3 cp s3://$BUCKET/static/css s3://$BUCKET/static/css --recursive --metadata-directive REPLACE --content-encoding 'gzip' --acl public-read
echo "๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€ ๐Ÿš€ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment