Skip to content

Commit 7222ff3

Browse files
authored
add a way to invalidate cloudfront cache (#5419)
1 parent 586c5a3 commit 7222ff3

File tree

4 files changed

+493
-2
lines changed

4 files changed

+493
-2
lines changed

packages/websites-server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

packages/websites-server/scripts/deploy-with-s3.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const { move } = require('fs-extra')
1111

1212
const { productionPrefix, s3Prefix } = require('../src/config')
1313

14-
// eslint-disable-next-line import-x/order
1514
const 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.
1819
const 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

3537
if (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

148154
main().catch(e => {

0 commit comments

Comments
 (0)