Invalidating CloudFront cache when deploying

This commit is contained in:
Jeff Avallone 2018-05-29 17:08:22 -04:00
parent 41676a370f
commit 16e900e5df
3 changed files with 34 additions and 16 deletions

View File

@ -41,6 +41,7 @@ Several environment variables are used to configure the application at build-tim
* `GA_PROPERTY` - Google Analytics property ID.
* `SENTRY_KEY` - Sentry.io DSN key for error reporting.
* `CIRCLE_BRANCH`, `CIRCLE_BUILD_NUM`, and `CIRCLE_SHA1` - CircleCI values used to generate build ID. Displayed in application footer and used in Sentry.io error reports.
* `CLOUD_FRONT_ID` - AWS CloudFront distribution ID to invalidating when running `yarn deploy`
* `DEPLOY_BUCKET` - AWS S3 bucket to deploy application to when running `yarn deploy`.
* `DEPLOY_ENV` - Environment the applicatoin will be deployed to. Used to report environment in Sentry.io error reports. Typically set to either "preview" or "production".
* `BANNER` - Text to display in header banner. Generally generated from `NODE_ENV`

View File

@ -1,7 +1,8 @@
const path = require('path');
module.exports = {
bucket: process.env.DEPLOY_BUCKET,
s3Bucket: process.env.DEPLOY_BUCKET,
cloudFrontId: process.env.CLOUD_FRONT_ID,
deployFrom: path.resolve(__dirname, 'build'),
paths: [
{

View File

@ -8,6 +8,7 @@ const AWS = require('aws-sdk');
const mime = require('mime-types');
const s3 = new AWS.S3();
const cloudFront = new AWS.CloudFront();
const config = require(path.resolve(process.argv[2]));
const configFor = path => {
@ -49,7 +50,7 @@ Promise.all([bucketContents, uploadDetails]).then(([bucket, upload]) => {
const uploadPromises = upload.map(params => {
console.log(`Starting upload for ${ params.Key }`);
return s3.upload({
Bucket: config.bucket,
Bucket: config.s3Bucket,
...params
}).promise()
.then(() => console.log(colors.green(`${ params.Key } successful`)))
@ -59,20 +60,35 @@ Promise.all([bucketContents, uploadDetails]).then(([bucket, upload]) => {
});
});
return Promise.all(uploadPromises).then(() => {
console.log(`Deleting ${ deleteKeys.length } stale files`);
return s3.deleteObjects({
Bucket: config.bucket,
Delete: {
Objects: deleteKeys.map(key => ({ Key: key }))
}
}).promise()
.then(() => console.log(colors.green('Delete successful')))
.catch(err => {
console.error(colors.red.bold('Delete failed'));
return Promise.reject(err);
});
});
return Promise.all(uploadPromises)
.then(() => {
console.log(`Deleting ${ deleteKeys.length } stale files`);
return s3.deleteObjects({
Bucket: config.s3Bucket,
Delete: {
Objects: deleteKeys.map(key => ({ Key: key }))
}
}).promise()
.then(() => console.log(colors.green('Delete successful')))
.catch(err => {
console.error(colors.red.bold('Delete failed'));
return Promise.reject(err);
});
})
.then(() => {
return cloudFront.createInvalidation({
DistributionId: config.cloudFrontId,
InvalidationBatch: {
CallerReference: `circleci-deploy-${ process.env.CIRCLE_BRANCH }-${ process.env.CIRCLE_BUILD_NUM }-${ process.env.CIRCLE_SHA1 }`,
Paths: {
Quantity: bucket.length,
Items: [
'/*'
]
}
}
}).promise();
});
})
.catch(err => {
console.error(colors.red.bold('Error:'), err);