71 lines
2.5 KiB
YAML
71 lines
2.5 KiB
YAML
name: aws-s3-sync
|
|
description: Sync build artifacts to S3, clean up old versioned assets, optionally invalidate CloudFront
|
|
|
|
inputs:
|
|
source-dir:
|
|
description: Local path to sync
|
|
required: true
|
|
s3-bucket:
|
|
description: Target S3 bucket name
|
|
required: true
|
|
cache-control:
|
|
description: HTTP Cache-Control header
|
|
required: false
|
|
default: "public, max-age=31536000, immutable"
|
|
aws-role-arn:
|
|
description: IAM role via OIDC
|
|
required: true
|
|
aws-profile:
|
|
description: AWS CLI profile name
|
|
required: false
|
|
default: default
|
|
cloudfront-distribution-ids-file:
|
|
description: Path to file with space-separated CF distribution IDs. If set, creates invalidations.
|
|
required: false
|
|
default: ""
|
|
retention-days:
|
|
description: Delete versioned _static/<timestamp> prefixes older than this many days (0 = skip)
|
|
required: false
|
|
default: "7"
|
|
|
|
runs:
|
|
using: composite
|
|
steps:
|
|
- uses: schmalz/shared-actions/.github/actions/aws-configure@v1
|
|
with:
|
|
role-arn: ${{ inputs.aws-role-arn }}
|
|
aws-profile: ${{ inputs.aws-profile }}
|
|
|
|
- run: |
|
|
SOURCE_DIR="${{ inputs.source-dir }}"
|
|
S3_BUCKET="${{ inputs.s3-bucket }}"
|
|
CACHE_CONTROL="${{ inputs.cache-control }}"
|
|
AWS_PROFILE="${{ inputs.aws-profile }}"
|
|
RETENTION_DAYS="${{ inputs.retention-days }}"
|
|
CF_IDS_FILE="${{ inputs.cloudfront-distribution-ids-file }}"
|
|
|
|
# Sync
|
|
aws s3 sync "$SOURCE_DIR" "s3://$S3_BUCKET" \
|
|
--cache-control "$CACHE_CONTROL" \
|
|
--profile "$AWS_PROFILE"
|
|
|
|
# Cleanup old _static/ prefixes if retention-days > 0
|
|
if [ "$RETENTION_DAYS" -gt 0 ]; then
|
|
CUTOFF=$(date -d "-${RETENTION_DAYS} days" +%s)
|
|
aws s3 ls "s3://$S3_BUCKET/_static/" --profile "$AWS_PROFILE" | while read -r line; do
|
|
PREFIX=$(echo "$line" | awk '{print $2}')
|
|
# Extract timestamp from prefix name, compare to cutoff, delete if old
|
|
TIMESTAMP=$(echo "$PREFIX" | tr -d '/')
|
|
if [ $(date -d "$TIMESTAMP" +%s 2>/dev/null || echo 0) -lt "$CUTOFF" ]; then
|
|
aws s3 rm "s3://$S3_BUCKET/_static/$PREFIX" --recursive --profile "$AWS_PROFILE"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
# CloudFront invalidation
|
|
if [ -n "$CF_IDS_FILE" ] && [ -f "$CF_IDS_FILE" ]; then
|
|
for DIST_ID in $(cat "$CF_IDS_FILE"); do
|
|
aws cloudfront create-invalidation --distribution-id "$DIST_ID" --paths "/*" --profile "$AWS_PROFILE"
|
|
done
|
|
fi
|
|
shell: bash
|