80 lines
3.1 KiB
YAML
80 lines
3.1 KiB
YAML
name: Publish to CloudFront
|
|
description: Syncs frontend assets to S3 and invalidates the CloudFront distribution
|
|
|
|
inputs:
|
|
dist_dir:
|
|
description: Path to the frontend dist directory
|
|
required: false
|
|
default: frontend/dist
|
|
s3_bucket_name:
|
|
description: Name of the S3 bucket to sync assets to
|
|
required: true
|
|
cloudfront_distribution_ids:
|
|
description: Space-separated list of CloudFront distribution IDs to invalidate
|
|
required: false
|
|
default: ''
|
|
versioned_static_prefix:
|
|
description: S3 prefix under which versioned builds are stored (e.g. _static). When set, old versions older than 7 days are deleted, keeping at least the 2 newest.
|
|
required: false
|
|
default: ''
|
|
|
|
runs:
|
|
using: composite
|
|
steps:
|
|
- name: Publish frontend assets
|
|
shell: bash
|
|
env:
|
|
INPUT_DIST_DIR: ${{ inputs.dist_dir }}
|
|
INPUT_S3_BUCKET_NAME: ${{ inputs.s3_bucket_name }}
|
|
INPUT_VERSIONED_STATIC_PREFIX: ${{ inputs.versioned_static_prefix }}
|
|
run: |
|
|
CACHE_CONTROL_ARG=""
|
|
if [[ -n "${INPUT_VERSIONED_STATIC_PREFIX}" ]]; then
|
|
CACHE_CONTROL_ARG="--cache-control 'public, max-age=31536000, immutable'"
|
|
fi
|
|
EXCLUDE_INDEX_ARG=""
|
|
if [[ -n "${INPUT_VERSIONED_STATIC_PREFIX}" && -f "${INPUT_DIST_DIR}/index.html" ]]; then
|
|
EXCLUDE_INDEX_ARG="--exclude index.html"
|
|
fi
|
|
aws s3 sync "${INPUT_DIST_DIR}" "s3://${INPUT_S3_BUCKET_NAME}" $CACHE_CONTROL_ARG $EXCLUDE_INDEX_ARG
|
|
|
|
- name: Publish index.html without immutable cache
|
|
if: ${{ inputs.versioned_static_prefix != '' }}
|
|
shell: bash
|
|
run: |
|
|
if [[ -f "${INPUT_DIST_DIR}/index.html" ]]; then
|
|
aws s3 cp "${INPUT_DIST_DIR}/index.html" "s3://${INPUT_S3_BUCKET_NAME}/index.html" \
|
|
--cache-control "no-cache, max-age=300" \
|
|
--metadata-directive REPLACE
|
|
fi
|
|
|
|
- name: Clean up old versioned static builds
|
|
if: ${{ inputs.versioned_static_prefix != '' }}
|
|
shell: bash
|
|
env:
|
|
INPUT_S3_BUCKET_NAME: ${{ inputs.s3_bucket_name }}
|
|
INPUT_VERSIONED_STATIC_PREFIX: ${{ inputs.versioned_static_prefix }}
|
|
run: |
|
|
aws s3 ls "s3://$INPUT_S3_BUCKET_NAME/$INPUT_VERSIONED_STATIC_PREFIX/" \
|
|
| grep -oP '(?<=PRE )[0-9]+' \
|
|
| sort --stable --reverse \
|
|
| tail -n +3 \
|
|
| while read version; do
|
|
now=$(($(date +%s%N)/1000000))
|
|
diff=$(($now-$version))
|
|
# delete if older than 7 days
|
|
if [ $diff -gt 604800000 ]; then
|
|
echo "Deleting $version"
|
|
aws s3 rm --recursive "s3://$INPUT_S3_BUCKET_NAME/$INPUT_VERSIONED_STATIC_PREFIX/$version"
|
|
fi
|
|
done
|
|
|
|
- name: Invalidate CloudFront
|
|
if: ${{ inputs.cloudfront_distribution_ids != '' }}
|
|
shell: bash
|
|
env:
|
|
INPUT_CLOUDFRONT_DISTRIBUTION_IDS: ${{ inputs.cloudfront_distribution_ids }}
|
|
run: |
|
|
echo "$INPUT_CLOUDFRONT_DISTRIBUTION_IDS" \
|
|
| tr ' ' '\n' \
|
|
| xargs -I% aws cloudfront create-invalidation --distribution-id % --paths '/*'
|