fix: update versioning parameters in README and action.yml for clarity #27

Merged
Michael.Seele merged 1 commit from bugfix/publish-static-contents-versioning into main 2026-05-19 15:23:04 +00:00
2 changed files with 39 additions and 15 deletions

View file

@ -9,7 +9,8 @@ Syncs frontend assets to S3 and invalidates a CloudFront distribution. Optionall
| `dist_dir` | No | `frontend/dist` | Path to the frontend dist directory |
| `s3_bucket_name` | Yes | — | Name of the S3 bucket to sync assets to |
| `cloudfront_distribution_ids` | No | `''` | Space-separated list of CloudFront distribution IDs to invalidate |
| `versioned_static_prefix` | No | `''` | 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 |
| `versioning` | No | `false` | When `true`, enables versioned builds. Old versions older than 7 days are deleted, keeping at least the 2 newest |
| `versioning_prefix` | No | `''` | S3 prefix under which versioned builds are stored (e.g. `_static``_static/1234567890/`). When omitted, versions are stored at the bucket root (e.g. `1234567890/`) |
## Usage
@ -20,20 +21,30 @@ Syncs frontend assets to S3 and invalidates a CloudFront distribution. Optionall
cloudfront_distribution_ids: ${{ vars.CLOUDFRONT_DISTRIBUTION_ID }}
```
With versioned static assets:
With versioned static assets at the bucket root:
```yaml
- uses: https://schmalz-git.git.onstackit.cloud/schmalz/shared-actions/publish-static-contents@publish-static-contents-v1
with:
dist_dir: frontend/dist
s3_bucket_name: my-bucket
cloudfront_distribution_ids: ${{ vars.CLOUDFRONT_DISTRIBUTION_ID }}
versioned_static_prefix: _static
versioning: true
```
With versioned static assets under a prefix:
```yaml
- uses: https://schmalz-git.git.onstackit.cloud/schmalz/shared-actions/publish-static-contents@publish-static-contents-v1
with:
s3_bucket_name: my-bucket
cloudfront_distribution_ids: ${{ vars.CLOUDFRONT_DISTRIBUTION_ID }}
versioning: true
versioning_prefix: _static
```
## Notes
- When `versioned_static_prefix` is set, all assets are synced with `Cache-Control: public, max-age=31536000, immutable`.
- When `versioning` is `true`, all assets are synced with `Cache-Control: public, max-age=31536000, immutable`.
- `index.html` is always synced separately with `Cache-Control: no-cache, max-age=300` so browsers pick up new deployments quickly.
- Old versioned builds are pruned: any version folder older than 7 days is deleted, with at least the 2 newest versions always retained.
- CloudFront invalidation is skipped when `cloudfront_distribution_ids` is empty.

View file

@ -13,8 +13,12 @@ inputs:
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.
versioning:
description: 'When set to true, enables versioned builds. Old versions older than 7 days are deleted, keeping at least the 2 newest.'
required: false
default: false
versioning_prefix:
description: 'S3 prefix under which versioned builds are stored (e.g. "_static" → _static/1234567890/). When omitted, versions are stored at the bucket root (e.g. 1234567890/).'
required: false
default: ''
@ -36,21 +40,25 @@ runs:
env:
INPUT_DIST_DIR: ${{ inputs.dist_dir }}
INPUT_S3_BUCKET_NAME: ${{ inputs.s3_bucket_name }}
INPUT_VERSIONED_STATIC_PREFIX: ${{ inputs.versioned_static_prefix }}
INPUT_VERSIONING: ${{ inputs.versioning }}
INPUT_VERSIONING_PREFIX: ${{ inputs.versioning_prefix }}
run: |
CACHE_CONTROL_ARG=""
if [[ -n "${INPUT_VERSIONED_STATIC_PREFIX}" ]]; then
if [[ "${INPUT_VERSIONING}" == "true" ]]; 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
if [[ "${INPUT_VERSIONING}" == "true" && -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 != '' }}
if: ${{ inputs.versioning == 'true' }}
shell: bash
env:
INPUT_DIST_DIR: ${{ inputs.dist_dir }}
INPUT_S3_BUCKET_NAME: ${{ inputs.s3_bucket_name }}
run: |
if [[ -f "${INPUT_DIST_DIR}/index.html" ]]; then
aws s3 cp "${INPUT_DIST_DIR}/index.html" "s3://${INPUT_S3_BUCKET_NAME}/index.html" \
@ -59,13 +67,18 @@ runs:
fi
- name: Clean up old versioned static builds
if: ${{ inputs.versioned_static_prefix != '' }}
if: ${{ inputs.versioning == 'true' }}
shell: bash
env:
INPUT_S3_BUCKET_NAME: ${{ inputs.s3_bucket_name }}
INPUT_VERSIONED_STATIC_PREFIX: ${{ inputs.versioned_static_prefix }}
INPUT_VERSIONING_PREFIX: ${{ inputs.versioning_prefix }}
run: |
aws s3 ls "s3://$INPUT_S3_BUCKET_NAME/$INPUT_VERSIONED_STATIC_PREFIX/" \
S3_PATH="s3://$INPUT_S3_BUCKET_NAME"
if [[ -n "${INPUT_VERSIONING_PREFIX}" ]]; then
S3_PATH="s3://$INPUT_S3_BUCKET_NAME/$INPUT_VERSIONING_PREFIX"
fi
aws s3 ls "${S3_PATH}/" \
| grep -oP '(?<=PRE )[0-9]+' \
| sort --stable --reverse \
| tail -n +3 \
@ -75,7 +88,7 @@ runs:
# 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"
aws s3 rm --recursive "${S3_PATH}/$version"
fi
done