Skip to content

Add includeOriginPrefix option #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ jobs:
id: update-stack
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
stackName: 'static-example-richardwillis-cloudformation-stack'
template: './cloudformation/cloudformation-s3bucket-example.yml'
stackName: 'badsyntax-github-action-example-aws-cloudfront'
template: './cloudformation/s3-cloudfront.yml'
applyChangeSet: ${{ github.event_name != 'repository_dispatch' }}
awsRegion: 'us-east-1'
parameters: |
S3BucketName=static-example-richardwillis-info-us-east-1&
S3AllowedOrigins=https://static-example.richardwillis.info,https://*.preview.static-example.richardwillis.info&
CloudFrontRootHosts=static-example.richardwillis.info&
CertificateARN=arn:aws:acm:us-east-1:008215002370:certificate/39df7626-7d2f-42e9-94f4-a3ce61ca3d5e
S3BucketName=badsyntax-github-action-example-aws-cloudfront-us-east-1&
S3AllowedOrigins=https://cloudfront-action.richardwillis.info&
CloudFrontRootHosts=cloudfront-action.richardwillis.info&
CertificateARN=arn:aws:acm:us-east-1:008215002370:certificate/cfb34309-b9bb-4f63-9af3-51026b8107d6

- uses: badsyntax/github-action-aws-s3@9537cd6d2013140b1d4e11ff526c5d75084c01df
name: Sync mutable HTML files to S3
Expand Down Expand Up @@ -76,4 +76,5 @@ jobs:
awsRegion: 'us-east-1'
originPrefix: 'root'
invalidatePaths: ${{ steps.sync-html-s3.outputs.S3SyncedFiles }}
defaultRootObject: 'index.html'
defaultRootObject: 'index'
includeOriginPrefix: true
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,26 @@ jobs:
distributionId: ${{ secrets.CFDistributionId }}
awsRegion: 'us-east-1'
originPrefix: 'root'
includeOriginPrefix: true
invalidatePaths: '/index.html,/'
defaultRootObject: 'index.html'
```

## Action Inputs

| key | description | example |
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| `invalidatePaths` | Comma separated list of invalidation paths | `root/blog.html,root/index.html` |
| `distributionId` | The CloudFront Distribution Id | `ABC123DEF` |
| `originPrefix` | The prefix of the object location in origin, which will be optionally stripped from the invalidation paths, if `includeOriginPrefix` is false. For example if originPrefix is "root" and invalidationPath is "root/blog.html" then the final path will be "blog.html" | `root` |
| `includeOriginPrefix` | Whether to include origin prefix paths. Useful when paths ere rewritten by Lambda's | `true` |
| `defaultRootObject` | The object returned when a user requests the root URL for your distribution. If this path is invalidated then a slash (/) is added to the invalidation paths | `index.html` |
| `awsRegion` | The AWS region | `us-east-1` |

## Related Projects

- [github-action-aws-cloudformation](https://github.com/badsyntax/github-action-aws-cloudformation)
- [github-action-aws-s3](https://github.com/badsyntax/github-action-aws-s3)
- [badsyntax/github-action-aws-cloudformation](https://github.com/badsyntax/github-action-aws-cloudformation)
- [badsyntax/github-action-aws-s3](https://github.com/badsyntax/github-action-aws-s3)

## Debugging

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ inputs:
description: 'The prefix of the object location in origin, which will be stripped from the invalidation paths. For example if originPrefix is "root" and invalidationPath is "root/blog.html" then the final path will be "blog.html"'
defaultRootObject:
required: true
description: 'The specific object return when a user requests the root URL for your distribution. If this path is invalidated then a slash (/) is added to the invalidation paths'
description: 'The object returned when a user requests the root URL for your distribution. If this path is invalidated then a slash (/) is added to the invalidation paths'
awsRegion:
require: true
description: 'The AWS region. For example: us-east-1'
includeOriginPrefix:
require: true
description: "Whether to include origin prefix paths. Useful when paths ere rewritten by Lambda's"
runs:
using: 'node16'
main: 'dist/index.js'
20 changes: 16 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ describe('getSanitisedInvalidationPaths', () => {
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
['index.html', 'blog.html', 'css/styles.css'],
'',
defaultRootObject
defaultRootObject,
false
);
expect(objectKeyWithoutExtension).toEqual([
'/index.html',
Expand All @@ -22,7 +23,8 @@ describe('getSanitisedInvalidationPaths', () => {
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
['root/index', '/root/css/styles.css', '/'],
'root',
defaultRootObject
defaultRootObject,
false
);
expect(objectKeyWithoutExtension).toEqual([
'/index',
Expand All @@ -35,8 +37,25 @@ describe('getSanitisedInvalidationPaths', () => {
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
[],
'root',
defaultRootObject
defaultRootObject,
false
);
expect(objectKeyWithoutExtension).toEqual([]);
});

it('should generate correct sanitised paths including origin prefix', () => {
const objectKeyWithoutExtension = getSanitisedInvalidationPaths(
['root/index', '/root/css/styles.css', '/'],
'root',
defaultRootObject,
true
);
expect(objectKeyWithoutExtension).toEqual([
'/index',
'/',
'/root/index',
'/css/styles.css',
'/root/css/styles.css',
]);
});
});
11 changes: 9 additions & 2 deletions src/cloudfront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function waitForInvalidationToComplete(
export function getSanitisedInvalidationPaths(
invalidatePaths: string[], // eg ['/root/index', 'root/css/styles.css']
originPrefix: string, // eg root
defaultRootObject: string // eg 'index.html'
defaultRootObject: string, // eg 'index.html'
includeOriginPrefix: boolean
): string[] {
const defaultRootObjectWithoutExtension = defaultRootObject
.split('.')
Expand All @@ -50,10 +51,16 @@ export function getSanitisedInvalidationPaths(
})
.map((path) => {
if (originPrefix) {
return path.replace(`/${originPrefix}`, '');
const pathWithoutOrigin = path.replace(`/${originPrefix}`, '');
if (includeOriginPrefix) {
return [pathWithoutOrigin, path];
} else {
return pathWithoutOrigin;
}
}
return path;
})
.flat()
.map((path) => {
const lowerCasePath = path.toLowerCase();
if (
Expand Down
7 changes: 7 additions & 0 deletions src/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ export function getInputs() {
trimWhitespace: true,
});

const includeOriginPrefix =
getInput('includeOriginPrefix', {
required: true,
trimWhitespace: true,
}).toLowerCase() === 'true';

return {
invalidatePaths,
distributionId,
region,
originPrefix,
defaultRootObject,
includeOriginPrefix,
};
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export async function run(): Promise<void> {
const sanitisedInvalidatePaths = getSanitisedInvalidationPaths(
inputs.invalidatePaths.split(',').filter(Boolean),
inputs.originPrefix,
inputs.defaultRootObject
inputs.defaultRootObject,
inputs.includeOriginPrefix
);

await invalidateCloudFrontCacheWithPaths(
Expand Down