Skip to content

Commit 2c5dfbf

Browse files
committed
Add includeOriginPrefix option
This adds origin paths to the invalidation paths which is helpful when paths are rewritten with lambdas.
1 parent 064152f commit 2c5dfbf

File tree

9 files changed

+68
-18
lines changed

9 files changed

+68
-18
lines changed

.github/workflows/deploy.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ jobs:
3333
id: update-stack
3434
with:
3535
githubToken: ${{ secrets.GITHUB_TOKEN }}
36-
stackName: 'static-example-richardwillis-cloudformation-stack'
37-
template: './cloudformation/cloudformation-s3bucket-example.yml'
36+
stackName: 'badsyntax-github-action-example-aws-cloudfront'
37+
template: './cloudformation/s3-cloudfront.yml'
3838
applyChangeSet: ${{ github.event_name != 'repository_dispatch' }}
3939
awsRegion: 'us-east-1'
4040
parameters: |
41-
S3BucketName=static-example-richardwillis-info-us-east-1&
42-
S3AllowedOrigins=https://static-example.richardwillis.info,https://*.preview.static-example.richardwillis.info&
43-
CloudFrontRootHosts=static-example.richardwillis.info&
44-
CertificateARN=arn:aws:acm:us-east-1:008215002370:certificate/39df7626-7d2f-42e9-94f4-a3ce61ca3d5e
41+
S3BucketName=badsyntax-github-action-example-aws-cloudfront-us-east-1&
42+
S3AllowedOrigins=https://cloudfront-action.richardwillis.info&
43+
CloudFrontRootHosts=cloudfront-action.richardwillis.info&
44+
CertificateARN=arn:aws:acm:us-east-1:008215002370:certificate/cfb34309-b9bb-4f63-9af3-51026b8107d6
4545
4646
- uses: badsyntax/github-action-aws-s3@9537cd6d2013140b1d4e11ff526c5d75084c01df
4747
name: Sync mutable HTML files to S3
@@ -76,4 +76,5 @@ jobs:
7676
awsRegion: 'us-east-1'
7777
originPrefix: 'root'
7878
invalidatePaths: ${{ steps.sync-html-s3.outputs.S3SyncedFiles }}
79-
defaultRootObject: 'index.html'
79+
defaultRootObject: 'index'
80+
includeOriginPrefix: true

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ inputs:
2222
awsRegion:
2323
require: true
2424
description: 'The AWS region. For example: us-east-1'
25+
includeOriginPrefix:
26+
require: true
27+
description: "Whether to include origin prefix paths. Useful when paths ere rewritten by Lambda's"
2528
runs:
2629
using: 'node16'
2730
main: 'dist/index.js'

dist/index.js

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/main.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ describe('getSanitisedInvalidationPaths', () => {
88
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
99
['index.html', 'blog.html', 'css/styles.css'],
1010
'',
11-
defaultRootObject
11+
defaultRootObject,
12+
false
1213
);
1314
expect(objectKeyWithoutExtension).toEqual([
1415
'/index.html',
@@ -22,7 +23,8 @@ describe('getSanitisedInvalidationPaths', () => {
2223
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
2324
['root/index', '/root/css/styles.css', '/'],
2425
'root',
25-
defaultRootObject
26+
defaultRootObject,
27+
false
2628
);
2729
expect(objectKeyWithoutExtension).toEqual([
2830
'/index',
@@ -35,8 +37,25 @@ describe('getSanitisedInvalidationPaths', () => {
3537
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
3638
[],
3739
'root',
38-
defaultRootObject
40+
defaultRootObject,
41+
false
3942
);
4043
expect(objectKeyWithoutExtension).toEqual([]);
4144
});
45+
46+
it('should generate correct sanitised paths including origin prefix', () => {
47+
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
48+
['root/index', '/root/css/styles.css', '/'],
49+
'root',
50+
defaultRootObject,
51+
true
52+
);
53+
expect(objectKeyWithoutExtension).toEqual([
54+
'/index',
55+
'/',
56+
'/root/index',
57+
'/css/styles.css',
58+
'/root/css/styles.css',
59+
]);
60+
});
4261
});

src/cloudfront.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ async function waitForInvalidationToComplete(
3636
export function getSanitisedInvalidationPaths(
3737
invalidatePaths: string[], // eg ['/root/index', 'root/css/styles.css']
3838
originPrefix: string, // eg root
39-
defaultRootObject: string // eg 'index.html'
39+
defaultRootObject: string, // eg 'index.html'
40+
includeOriginPrefix: boolean
4041
): string[] {
4142
const defaultRootObjectWithoutExtension = defaultRootObject
4243
.split('.')
@@ -50,10 +51,16 @@ export function getSanitisedInvalidationPaths(
5051
})
5152
.map((path) => {
5253
if (originPrefix) {
53-
return path.replace(`/${originPrefix}`, '');
54+
const pathWithoutOrigin = path.replace(`/${originPrefix}`, '');
55+
if (includeOriginPrefix) {
56+
return [pathWithoutOrigin, path];
57+
} else {
58+
return pathWithoutOrigin;
59+
}
5460
}
5561
return path;
5662
})
63+
.flat()
5764
.map((path) => {
5865
const lowerCasePath = path.toLowerCase();
5966
if (

src/inputs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ export function getInputs() {
2828
trimWhitespace: true,
2929
});
3030

31+
const includeOriginPrefix =
32+
getInput('includeOriginPrefix', {
33+
required: true,
34+
trimWhitespace: true,
35+
}).toLowerCase() === 'true';
36+
3137
return {
3238
invalidatePaths,
3339
distributionId,
3440
region,
3541
originPrefix,
3642
defaultRootObject,
43+
includeOriginPrefix,
3744
};
3845
}

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export async function run(): Promise<void> {
2020
const sanitisedInvalidatePaths = getSanitisedInvalidationPaths(
2121
inputs.invalidatePaths.split(',').filter(Boolean),
2222
inputs.originPrefix,
23-
inputs.defaultRootObject
23+
inputs.defaultRootObject,
24+
inputs.includeOriginPrefix
2425
);
2526

2627
await invalidateCloudFrontCacheWithPaths(

0 commit comments

Comments
 (0)