Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Last active January 25, 2021 22:42
Show Gist options
  • Save jeffsheets/8eb6821b04a532d0dbf23443d986d84e to your computer and use it in GitHub Desktop.
Save jeffsheets/8eb6821b04a532d0dbf23443d986d84e to your computer and use it in GitHub Desktop.
AWS S3 Deploy sync w/ Cloudfront Invalidation and Cache-Control headers for Create-React-App
//Config for s3 websync with cloudfront invalidation
// and Cache-Control headers for a Create-React-App application
//https://www.npmjs.com/package/websync
module.exports = {
source: "build/",
target: `s3://yourcompany-us-east-1-${process.env.CI_ENVIRONMENT_NAME}-createreactapp/`,
invalidateDeletes: true,
modifiers: {
"static/**": { CacheControl: "max-age=31536000" },
"!static/**": { CacheControl: "no-cache" }
}
};
//...snippet
"devDependencies": {
"websync": "^0.0.13"
}
#...snippet
#Requires AWS credentials setup in gitlab ci/cd config
#Reusable script definition
.deploy-script:
script: &deploy_script
- echo "Syncing content files to S3 and invalidating Cloudfront CDN"
- yarn websync --config ./websyncConfig.js
- echo "Done!"
#Deploys to development environment when PR is merged to develop branch
deploy-dev:
stage: deploy
only:
- main
environment:
name: development
url: https://createreactapp.development.yourcompany.com/
script: *deploy_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment