Skip to content

Commit ce39dfc

Browse files
authored
Fix cf invalidation (#24)
* Fixed the bug with cloudfront invalidation paths * Adding flag to delete stale files from s3
1 parent 63f1bc8 commit ce39dfc

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

push-to-s3-and-invalidate-cloudfront/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Files are pushed to the S3 bucket depending on the user inputs supplied when the
66
Following examples describe how to use the action.
77

88
## Uploading to S3 based on user input
9-
This example will **upload** the specified folder (`build-directory`) to a folder named `blog` on the S3 bucket(`myS3Bucket`). The action will not exclude any files while uploading them. The action will also invalidate the cloudfront cache. The action will attempt a max of `5` (based on `aws-retry-attempts` value) retries for any AWS operations before erroring out.
9+
This example will **upload** the specified folder (`build-directory`) to a folder named `blog` on the S3 bucket(`myS3Bucket`). The action will not exclude any files while uploading them. The action will also invalidate the cloudfront cache. The action will attempt a max of `5` (based on `aws-retry-attempts` value) retries for any AWS operations before erroring out. This example will also delete files from S3 that do not exist in the source.
1010

1111
```yaml
1212
- uses: XanaduAI/cloud-actions/push-to-s3-and-invalidate-cloudfront@main
@@ -19,10 +19,11 @@ This example will **upload** the specified folder (`build-directory`) to a folde
1919
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
2020
s3-bucket: myS3Bucket
2121
s3-action: "upload"
22-
s3-dir-to-upload-to: "blog"
22+
s3-directory: "blog"
2323
s3-files-to-exclude: ""
2424
invalidate-cloudfront-cache: "true"
2525
aws-retry-attempts: 5
26+
s3-delete-stale-files: "true"
2627
```
2728
2829
## Deleting from S3 based on user input
@@ -40,13 +41,13 @@ This example will **delete** the specified folder (`build-directory`) from the S
4041
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4142
s3-bucket: myS3Bucket
4243
s3-action: "delete"
43-
s3-dir-to-delete-from: "blog"
44+
s3-directory: "blog"
4445
invalidate-cloudfront-cache: "true"
4546
aws-retry-attempts: 5
4647
```
4748

4849
## Uploading/Deleting to/from S3 with minimal user unput
49-
If this action is called when a `pull_request` is `opened` or `syncronized`, then the example will **upload** the specified folder (`build-directory`) to a folder named `pr-previews/PR-123` on the S3 bucket (`myS3Bucket`). The action will exclude any files that start with the default `pr-previews` prefix. The action will not invalidate the cloudfront cache.
50+
If this action is called when a `pull_request` is `opened` or `syncronized`, then the example will **upload** the specified folder (`build-directory`) to a folder named `pr-previews/PR-123` on the S3 bucket (`myS3Bucket`). The action will exclude any files that start with the default `pr-previews` prefix. The action will not invalidate the cloudfront cache. This example will not delete files from S3 that do not exist in the source.
5051

5152
If this action is called when a `pull_request` is `closed`, then the example will **delete** the folder named `pr-previews/PR-123` on the S3 bucket (`myS3Bucket`).
5253
The action will attempt a max of `2` (default) retries for any AWS operations before erroring out.

push-to-s3-and-invalidate-cloudfront/action.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ inputs:
5656
Refer to https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-retries.html for the usage of retry retry attempts
5757
required: false
5858
default: "2"
59+
s3-delete-stale-files:
60+
description: |
61+
A flag to indicate if the aws sync command should be run with the `--delete` flag. This will delete the files that exist in the destination but not in the source.
62+
More information on this can be found at https://docs.aws.amazon.com/cli/latest/reference/s3/sync.html
63+
required: false
64+
default: "false"
5965

6066
outputs:
6167
dir_name:
@@ -64,6 +70,9 @@ outputs:
6470
files_to_exlude:
6571
description: Files to exclude from S3 when using the sync command.
6672
value: ${{ steps.exclude.files_to_exclude }}
73+
delete_stale_files:
74+
description: Compute if the aws s3 sync command should run with the `--delete` flag based on user input.
75+
value: ${{ steps.stale.delete_stale_files }}
6776

6877
runs:
6978
using: composite
@@ -81,12 +90,12 @@ runs:
8190
run: |
8291
if [[ "${{ inputs.s3-directory }}" == "pr-previews" ]]
8392
then
84-
dir_name="${{ inputs.s3-directory }}/PR-${{ inputs.pull-request-number }}"
93+
dir_name="${{ inputs.s3-directory }}/PR-${{ inputs.pull-request-number }}/"
8594
elif [[ "${{ inputs.s3-directory }}" == "/" ]]
8695
then
8796
dir_name=""
8897
else
89-
dir_name="${{ inputs.s3-directory }}"
98+
dir_name="${{ inputs.s3-directory }}/"
9099
fi
91100
echo "dir_name=$dir_name" >> $GITHUB_OUTPUT
92101
@@ -108,6 +117,17 @@ runs:
108117
files_to_exlude="--exclude \"${{ inputs.s3-files-to-exclude }}\""
109118
fi
110119
echo "files_to_exlude=$files_to_exlude" >> $GITHUB_OUTPUT
120+
121+
- name: Compute if stale files should be deleted
122+
shell: bash
123+
id: stale
124+
run: |
125+
delete_stale_files=""
126+
if [[ "${{ inputs.s3-delete-stale-files }}" == "true" ]]
127+
then
128+
delete_stale_files="--delete"
129+
fi
130+
echo "delete_stale_files=$delete_stale_files" >> $GITHUB_OUTPUT
111131
112132
- name: Upload the files to S3
113133
shell: bash
@@ -118,7 +138,7 @@ runs:
118138
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
119139
AWS_MAX_ATTEMPTS: ${{ inputs.aws-retry-attempts }}
120140
run: |
121-
aws s3 sync ${{ inputs.build-directory }}/ s3://${{ inputs.s3-bucket }}/${{ steps.directory.outputs.dir_name }} ${{ steps.exclude.outputs.files_to_exlude }}
141+
aws s3 sync ${{ inputs.build-directory }}/ s3://${{ inputs.s3-bucket }}/${{ steps.directory.outputs.dir_name }} ${{ steps.exclude.outputs.files_to_exlude }} ${{ steps.stale.outputs.delete_stale_files }}
122142
123143
- name: Delete the files on S3
124144
shell: bash
@@ -128,7 +148,7 @@ runs:
128148
AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
129149
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
130150
AWS_MAX_ATTEMPTS: ${{ inputs.aws-retry-attempts }}
131-
run: aws s3 rm s3://${{ inputs.s3-bucket }}/${{ steps.directory.outputs.dir_name }}/ --recursive ${{ steps.exclude.outputs.files_to_exlude }}
151+
run: aws s3 rm s3://${{ inputs.s3-bucket }}/${{ steps.directory.outputs.dir_name }} --recursive ${{ steps.exclude.outputs.files_to_exlude }}
132152

133153
- name: Invalidate the cloudfront cache
134154
shell: bash
@@ -139,4 +159,4 @@ runs:
139159
AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
140160
AWS_MAX_ATTEMPTS: ${{ inputs.aws-retry-attempts }}
141161
run: |
142-
aws cloudfront create-invalidation --distribution-id ${{ inputs.aws-cloudfront-distribution-id }} --paths "/${{ steps.directory.outputs.dir_name }}/*"
162+
aws cloudfront create-invalidation --distribution-id ${{ inputs.aws-cloudfront-distribution-id }} --paths "/${{ steps.directory.outputs.dir_name }}*"

0 commit comments

Comments
 (0)