Skip to content

Instantly share code, notes, and snippets.

@fabdrol
Created February 10, 2021 17:30
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 fabdrol/7209fa635d0b883bbc84a11b7fecde90 to your computer and use it in GitHub Desktop.
Save fabdrol/7209fa635d0b883bbc84a11b7fecde90 to your computer and use it in GitHub Desktop.
Build, sync to S3, invalidate CloudFront Cache and push to Git script. To be used as a local script or in a CI environment
#!/bin/bash
CLOUDFRONT_DISTRIBUTION="<CLOUDFRONT DISTRIBUTION>"
S3_BUCKET="<BUCKET NAME>"
VERSION=$(cat package.json | jq '.version' | tr -d '"')
echo "**** [1/5] Building application v${VERSION}"
yarn build
echo "**** [2/5] Syncing build/* to s3://$S3_BUCKET..."
aws s3 sync build/ s3://${S3_BUCKET} --cache-control max-age=172800 --delete
echo "**** [3/5] Creating CloudFront invalidation..."
aws configure set preview.cloudfront true
INVALIDATION_ID=$(aws cloudfront create-invalidation --distribution-id ${CLOUDFRONT_DISTRIBUTION} --paths "/index.html" "/static/" "/assets/" "/*" "/static/*" "/assets/*" | jq '.Invalidation.Id' | tr -d '"')
echo "**** [4/5] Waiting for CloudFront invalidation ${INVALIDATION_ID} to complete..."
aws cloudfront wait invalidation-completed --id ${INVALIDATION_ID} --distribution-id ${CLOUDFRONT_DISTRIBUTION}
echo "*** [5/5] Committing and pushing to GitHub"
git add --all . && git commit -am "Release v$VERSION" && git tag -a "v$VERSION" -m "Release v$VERSION" && git push && git push --tags
echo "**** All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment