Skip to content

Commit

Permalink
chore: invalidate cloudfront in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
theworkflow committed Oct 8, 2019
1 parent ea71223 commit 36ace07
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ before_deploy:
- npm run build
deploy:
provider: script
script: DEPLOY_BUCKET=$AWS_S3_BUCKET npm run deploy
script: DEPLOY_BUCKET=$AWS_S3_BUCKET CLOUDFRONT_DISTRIBUTION_ID=$AWS_CLOUDFRONT_DISTRO_ID npm run deploy
skip_cleanup: true
# on:
# branch: master
# tags: true
after_deploy:
- pip install --user awscli
- aws cloudfront create-invalidation --distribution-id $AWS_CLOUDFRONT_DISTRO_ID --paths /
32 changes: 30 additions & 2 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ const mime = require('mime');
const path = require('path');
const s3diff = require('s3-diff');
const s3 = new AWS.S3({ region: 'us-east-1' });
const cloudfront = new AWS.CloudFront();

const { DEPLOY_BUCKET: bucket } = process.env;
const {
DEPLOY_BUCKET: bucket,
CLOUDFRONT_DISTRIBUTION_ID: distro
} = process.env;

const localFolder = path.resolve(__dirname, '..', 'build');

Expand All @@ -33,7 +37,7 @@ s3diff({
remove(data)
.then(() => update(data))
.then(() => add(data))
// .then(() => doInvalidation())
.then(() => doInvalidation())
.then(() => {
console.log('Finished uploading');
});
Expand Down Expand Up @@ -76,3 +80,27 @@ function upload(file, cb) {
ACL: 'public-read'
}, () => cb());
}

function doInvalidation() {
const params = {
DistributionId: distro,
InvalidationBatch: {
CallerReference: `INVALIDATION-${new Date().getTime()}`,
Paths: {
Quantity: 1,
Items: [
'/*'
]
}
}
};

cloudfront.createInvalidation(params, function(err, data) {
if (err) {
console.error(err, err.stack);
process.exit(1);
} else {
console.log(data);
}
});
}

0 comments on commit 36ace07

Please sign in to comment.