|
| 1 | +name: Invalidate CloudFront on Asset Changes |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + paths: |
| 8 | + - 'theme/**/*.css' |
| 9 | + - 'theme/**/*.js' |
| 10 | + - 'theme/**/*.hbs' |
| 11 | + paths-ignore: |
| 12 | + - '.github/**' |
| 13 | + - 'book/**' |
| 14 | + workflow_dispatch: |
| 15 | + |
| 16 | +permissions: |
| 17 | + id-token: write |
| 18 | + contents: read |
| 19 | + |
| 20 | +jobs: |
| 21 | + invalidate: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + environment: prod |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + fetch-depth: 2 |
| 30 | + |
| 31 | + - name: Configure AWS credentials using OIDC |
| 32 | + uses: aws-actions/configure-aws-credentials@v3 |
| 33 | + with: |
| 34 | + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} |
| 35 | + aws-region: us-east-1 |
| 36 | + |
| 37 | + - name: Compute invalidation paths |
| 38 | + id: paths |
| 39 | + shell: bash |
| 40 | + run: | |
| 41 | + set -euo pipefail |
| 42 | +
|
| 43 | + BEFORE="${{ github.event.before }}" |
| 44 | + AFTER="${{ github.sha }}" |
| 45 | +
|
| 46 | + if [ -z "$BEFORE" ] || [ "$BEFORE" = "0000000000000000000000000000000000000000" ]; then |
| 47 | + if git rev-parse "${AFTER}^" >/dev/null 2>&1; then |
| 48 | + BEFORE="${AFTER}^" |
| 49 | + else |
| 50 | + BEFORE="" |
| 51 | + fi |
| 52 | + fi |
| 53 | +
|
| 54 | + if [ -n "$BEFORE" ]; then |
| 55 | + git diff --name-only "$BEFORE" "$AFTER" > /tmp/changed_files.txt |
| 56 | + else |
| 57 | + git ls-tree --name-only -r "$AFTER" > /tmp/changed_files.txt |
| 58 | + fi |
| 59 | +
|
| 60 | + mapfile -t files < <(grep -E '^theme/.*\.(css|js|hbs)$' /tmp/changed_files.txt || true) |
| 61 | + if [ ${#files[@]} -eq 0 ]; then |
| 62 | + echo "paths=" >> "$GITHUB_OUTPUT" |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | +
|
| 66 | + invalidate_paths=() |
| 67 | + hbs_changed=false |
| 68 | +
|
| 69 | + for f in "${files[@]}"; do |
| 70 | + if [[ "$f" == theme/* ]]; then |
| 71 | + rel="${f#theme/}" |
| 72 | + if [[ "$f" == *.hbs ]]; then |
| 73 | + hbs_changed=true |
| 74 | + else |
| 75 | + invalidate_paths+=("/$rel") |
| 76 | + fi |
| 77 | + fi |
| 78 | + done |
| 79 | +
|
| 80 | + if [ "$hbs_changed" = true ]; then |
| 81 | + invalidate_paths+=("/*") |
| 82 | + fi |
| 83 | +
|
| 84 | + printf "%s\n" "${invalidate_paths[@]}" | awk 'NF' | sort -u > /tmp/invalidate_paths.txt |
| 85 | +
|
| 86 | + if [ ! -s /tmp/invalidate_paths.txt ]; then |
| 87 | + echo "paths=" >> "$GITHUB_OUTPUT" |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + paths=$(paste -sd' ' /tmp/invalidate_paths.txt) |
| 92 | + echo "paths=$paths" >> "$GITHUB_OUTPUT" |
| 93 | +
|
| 94 | + - name: Create CloudFront invalidation |
| 95 | + if: steps.paths.outputs.paths != '' |
| 96 | + run: | |
| 97 | + aws cloudfront create-invalidation \ |
| 98 | + --distribution-id "${{ secrets.CLOUDFRONT_DISTRIBUTION_ID }}" \ |
| 99 | + --paths ${{ steps.paths.outputs.paths }} |
0 commit comments