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