File tree Expand file tree Collapse file tree 4 files changed +493
-2
lines changed
Expand file tree Collapse file tree 4 files changed +493
-2
lines changed Original file line number Diff line number Diff line change 2424 "build:start" : " yarn build && yarn start"
2525 },
2626 "dependencies" : {
27+ "@aws-sdk/client-cloudfront" : " 3.830.0" ,
2728 "@aws-sdk/client-s3" : " 3.821.0" ,
2829 "@dvcorg/gatsby-theme" : " workspace:^" ,
2930 "@hapi/wreck" : " 18.1.0" ,
Original file line number Diff line number Diff line change 1+ const {
2+ CloudFrontClient,
3+ CreateInvalidationCommand
4+ } = require ( '@aws-sdk/client-cloudfront' )
5+
6+ module . exports = async function ( ) {
7+ const {
8+ CLOUDFRONT_DISTRIBUTION_ID ,
9+ CONTEXT ,
10+ DVCORG_AWS_ACCESS_KEY_ID ,
11+ DVCORG_AWS_SECRET_ACCESS_KEY
12+ } = process . env
13+ if ( CONTEXT !== 'production' ) {
14+ console . log (
15+ 'Skipping CloudFront cache purge because CONTEXT is not set to production.'
16+ )
17+ return
18+ }
19+
20+ if ( ! CLOUDFRONT_DISTRIBUTION_ID ) {
21+ console . error (
22+ 'Skipping CloudFront cache purge because CLOUDFRONT_DISTRIBUTION_ID is not set.'
23+ )
24+ return
25+ }
26+
27+ const client = new CloudFrontClient ( {
28+ region : 'us-east-1' ,
29+ credentials : {
30+ accessKeyId : DVCORG_AWS_ACCESS_KEY_ID ,
31+ secretAccessKey : DVCORG_AWS_SECRET_ACCESS_KEY
32+ }
33+ } )
34+ const input = {
35+ DistributionId : CLOUDFRONT_DISTRIBUTION_ID ,
36+ InvalidationBatch : {
37+ CallerReference : Date . now ( ) . toString ( ) ,
38+ Paths : {
39+ Quantity : 1 ,
40+ Items : [ '/*' ]
41+ }
42+ }
43+ }
44+ const command = new CreateInvalidationCommand ( input )
45+ const response = await client . send ( command )
46+ console . log (
47+ `Cleared CloudFront cache successfully: ${ response . Invalidation . Id } `
48+ )
49+ }
Original file line number Diff line number Diff line change @@ -11,8 +11,9 @@ const { move } = require('fs-extra')
1111
1212const { productionPrefix, s3Prefix } = require ( '../src/config' )
1313
14- // eslint-disable-next-line import-x/order
1514const clearCloudflareCache = require ( './clear-cloudflare-cache' )
15+ // eslint-disable-next-line import-x/order
16+ const clearCloudfrontCache = require ( './clear-cloudfront-cache' )
1617// Generate deploy options from a comma separated string in the DEPLOY_OPTIONS
1718// env var. If DEPLOY_OPTIONS isn't set, use a default setting.
1819const deployOptions = DEPLOY_OPTIONS
@@ -29,7 +30,8 @@ const deployOptions = DEPLOY_OPTIONS
2930 retry : true ,
3031 upload : true ,
3132 clean : true ,
32- clearCloudflareCache : true
33+ clearCloudflareCache : true ,
34+ clearCloudfrontCache : true
3335 }
3436
3537if ( deployOptions . logSteps ) {
@@ -143,6 +145,10 @@ async function main() {
143145 if ( deployOptions . clearCloudflareCache ) {
144146 await clearCloudflareCache ( )
145147 }
148+
149+ if ( deployOptions . clearCloudfrontCache ) {
150+ await clearCloudfrontCache ( )
151+ }
146152}
147153
148154main ( ) . catch ( e => {
You can’t perform that action at this time.
0 commit comments