Invalidating CloudFront cache when deploying
This commit is contained in:
parent
41676a370f
commit
16e900e5df
@ -41,6 +41,7 @@ Several environment variables are used to configure the application at build-tim
|
|||||||
* `GA_PROPERTY` - Google Analytics property ID.
|
* `GA_PROPERTY` - Google Analytics property ID.
|
||||||
* `SENTRY_KEY` - Sentry.io DSN key for error reporting.
|
* `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.
|
* `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_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".
|
* `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`
|
* `BANNER` - Text to display in header banner. Generally generated from `NODE_ENV`
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
bucket: process.env.DEPLOY_BUCKET,
|
s3Bucket: process.env.DEPLOY_BUCKET,
|
||||||
|
cloudFrontId: process.env.CLOUD_FRONT_ID,
|
||||||
deployFrom: path.resolve(__dirname, 'build'),
|
deployFrom: path.resolve(__dirname, 'build'),
|
||||||
paths: [
|
paths: [
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,7 @@ const AWS = require('aws-sdk');
|
|||||||
const mime = require('mime-types');
|
const mime = require('mime-types');
|
||||||
|
|
||||||
const s3 = new AWS.S3();
|
const s3 = new AWS.S3();
|
||||||
|
const cloudFront = new AWS.CloudFront();
|
||||||
const config = require(path.resolve(process.argv[2]));
|
const config = require(path.resolve(process.argv[2]));
|
||||||
|
|
||||||
const configFor = path => {
|
const configFor = path => {
|
||||||
@ -49,7 +50,7 @@ Promise.all([bucketContents, uploadDetails]).then(([bucket, upload]) => {
|
|||||||
const uploadPromises = upload.map(params => {
|
const uploadPromises = upload.map(params => {
|
||||||
console.log(`Starting upload for ${ params.Key }`);
|
console.log(`Starting upload for ${ params.Key }`);
|
||||||
return s3.upload({
|
return s3.upload({
|
||||||
Bucket: config.bucket,
|
Bucket: config.s3Bucket,
|
||||||
...params
|
...params
|
||||||
}).promise()
|
}).promise()
|
||||||
.then(() => console.log(colors.green(`${ params.Key } successful`)))
|
.then(() => console.log(colors.green(`${ params.Key } successful`)))
|
||||||
@ -59,20 +60,35 @@ Promise.all([bucketContents, uploadDetails]).then(([bucket, upload]) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(uploadPromises).then(() => {
|
return Promise.all(uploadPromises)
|
||||||
console.log(`Deleting ${ deleteKeys.length } stale files`);
|
.then(() => {
|
||||||
return s3.deleteObjects({
|
console.log(`Deleting ${ deleteKeys.length } stale files`);
|
||||||
Bucket: config.bucket,
|
return s3.deleteObjects({
|
||||||
Delete: {
|
Bucket: config.s3Bucket,
|
||||||
Objects: deleteKeys.map(key => ({ Key: key }))
|
Delete: {
|
||||||
}
|
Objects: deleteKeys.map(key => ({ Key: key }))
|
||||||
}).promise()
|
}
|
||||||
.then(() => console.log(colors.green('Delete successful')))
|
}).promise()
|
||||||
.catch(err => {
|
.then(() => console.log(colors.green('Delete successful')))
|
||||||
console.error(colors.red.bold('Delete failed'));
|
.catch(err => {
|
||||||
return Promise.reject(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 => {
|
.catch(err => {
|
||||||
console.error(colors.red.bold('Error:'), err);
|
console.error(colors.red.bold('Error:'), err);
|
||||||
|
Loading…
Reference in New Issue
Block a user