Skip to content

Commit

Permalink
Merge pull request #8 from badsyntax/modified-files
Browse files Browse the repository at this point in the history
Return modified keys for all actions
  • Loading branch information
badsyntax authored Dec 13, 2021
2 parents 1484cce + 169cdd3 commit 9195548
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sync-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ jobs:
echo "Synced Immutable Files: $S3SyncedImmutableFiles"
env:
# Use outputs from the S3 step
S3SyncedHTMLFiles: ${{ steps.sync-html-s3.outputs.S3SyncedFiles }}
S3SyncedImmutableFiles: ${{ steps.sync-immutable-s3.outputs.S3SyncedFiles }}
S3SyncedHTMLFiles: ${{ steps.sync-html-s3.outputs.modifiedKeys }}
S3SyncedImmutableFiles: ${{ steps.sync-immutable-s3.outputs.modifiedKeys }}
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ jobs:

- name: Output Synced Files
run: |
echo "Synced HTML Files: $S3SyncedHTMLFiles"
echo "Synced object keys: $S3SyncedFiles"
env:
# Use outputs from previous sync steps
S3SyncedHTMLFiles: ${{ steps.sync-html-s3.outputs.S3SyncedFiles }}
S3SyncedFiles: ${{ steps.sync-html-s3.outputs.modifiedKeys }}
```
## Action Inputs
| key | description | example |
| Name | Description | Example |
| ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
| `bucket` | The name of the S3 bucket | `example-bucket-us-east-1` |
| `action` | The action to perform. Accepted values: `sync` or `clean` | `sync` |
Expand All @@ -90,6 +90,12 @@ jobs:
| `stripExtensionGlob` (optional) | Glob pattern to strip extension (if using the sync action) | `**/**.html` |
| `acl` (optional) | Access control list (options: `authenticated-read, aws-exec-read, bucket-owner-full-control, bucket-owner-read, private, public-read, public-read-write`) | `private` |

## Action Outputs

| Name | Description | Example |
| -------------- | ------------------------------------------------------------------------- | ------------------------- |
| `modifiedKeys` | A comma separated list of modified object keys (either synced or removed) | `file1,folder1/file2.ext` |

## Related GitHub Actions

- [badsyntax/github-action-aws-cloudfront](https://github.com/badsyntax/github-action-aws-cloudfront)
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ inputs:
default: ''
description: '"authenticated-read" | "aws-exec-read" | "bucket-owner-full-control" | "bucket-owner-read" | "private" | "public-read" | "public-read-write"'
outputs:
S3SyncedFiles:
description: 'A comma separated list of objects keys for files synced to S3. For example: file1,folder1/file2'
modifiedKeys:
description: 'A comma separated list of modified object keys (either synced or removed). For example: file1,folder1/file2.ext'
runs:
using: 'node16'
main: 'dist/index.js'
16 changes: 10 additions & 6 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.

2 changes: 1 addition & 1 deletion out/blog.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<link rel="stylesheet" href="./css/styles.css" />
</head>
<body>
BLOG 1
BLOG 2
</body>
</html>
2 changes: 1 addition & 1 deletion out/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<link rel="stylesheet" href="./css/styles.css" />
</head>
<body>
hello there 1
hello there 2
</body>
</html>
2 changes: 1 addition & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { info } from '@actions/core';
export const workspace = process.env.GITHUB_WORKSPACE;

export function logOutputParameters(syncedFiles: string[]): void {
info(`::set-output name=S3SyncedFiles::${syncedFiles.join(',')}`);
info(`::set-output name=modifiedKeys::${syncedFiles.join(',')}`);
}
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ export async function run(): Promise<void> {
);
logOutputParameters(syncedFiles);
} else if (inputs.action === 'clean') {
const totalObjectsCleaned = await emptyS3Directory(
const cleanedFiles = await emptyS3Directory(
s3Client,
inputs.bucket,
inputs.prefix
);
logOutputParameters(cleanedFiles);
info(
`Cleaned ${totalObjectsCleaned} from s3://${inputs.bucket}/${inputs.prefix}`
`Cleaned ${cleanedFiles.length} from s3://${inputs.bucket}/${inputs.prefix}`
);
}
} catch (error) {
Expand Down
12 changes: 8 additions & 4 deletions src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export async function emptyS3Directory(
client: S3Client,
s3BucketName: string,
prefix: string,
initialObjectsCleaned = 0
): Promise<number> {
initialObjectsCleaned: string[] = []
): Promise<string[]> {
const objects = await client.send(
new ListObjectsV2Command({
Bucket: s3BucketName,
Expand All @@ -200,14 +200,18 @@ export async function emptyS3Directory(
return initialObjectsCleaned;
}

const objectKeys = objects.Contents.map(({ Key }) => ({ Key }));

await client.send(
new DeleteObjectsCommand({
Bucket: s3BucketName,
Delete: { Objects: objects.Contents.map(({ Key }) => ({ Key })) },
Delete: { Objects: objectKeys },
})
);

const totalObjectsCleaned = initialObjectsCleaned + objects.Contents.length;
const totalObjectsCleaned = initialObjectsCleaned
.concat(objectKeys.map(({ Key }) => Key || ''))
.filter(Boolean);

if (objects.IsTruncated) {
return await emptyS3Directory(
Expand Down

0 comments on commit 9195548

Please sign in to comment.