Repository navigation
173 lines (148 loc) · 6.26 KB
/
Copy pathrevalidate-content.yml
File metadata and controls
173 lines (148 loc) · 6.26 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
name: Revalidate Content
on:
push:
branches:
- main
paths:
- "content/**/*.md"
- "content/**/*.mdx"
jobs:
revalidate-content:
name: Revalidate Changed Pages
runs-on: ubuntu-latest
permissions:
contents: read
actions: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history to compare all commits in push
- name: Get changed MDX files
id: changed-files
run: |
# Get all MDX files changed in this push (handles multiple commits)
set +e
CHANGED_FILES=$(git diff --name-only \
"${{ github.event.before }}" "${{ github.event.after }}" \
-- 'content/**/*.md' 'content/**/*.mdx')
EXIT_CODE=$?
set -e
# Check for git errors
if [ $EXIT_CODE -ne 0 ]; then
echo "::error::git diff failed with exit code $EXIT_CODE"
exit 1
fi
# Handle empty result (no files changed)
if [ -z "$CHANGED_FILES" ]; then
echo "No MDX files changed in /content/ directory"
echo "filePaths=[]" >> $GITHUB_OUTPUT
echo "frontmatterChanged=false" >> $GITHUB_OUTPUT
exit 0
fi
# Detect whether any changed file has frontmatter changes
FRONTMATTER_CHANGED=false
extract_frontmatter() {
local ref="$1"
local path="$2"
if ! git cat-file -e "${ref}:${path}" 2>/dev/null; then
return 0
fi
git show "${ref}:${path}" | awk '
NR == 1 && $0 != "---" { exit }
NR == 1 && $0 == "---" { in_frontmatter = 1; next }
in_frontmatter && $0 == "---" { exit }
in_frontmatter { print }
'
}
while IFS= read -r FILE; do
[ -z "$FILE" ] && continue
OLD_FRONTMATTER=$(extract_frontmatter "${{ github.event.before }}" "$FILE")
NEW_FRONTMATTER=$(extract_frontmatter "${{ github.event.after }}" "$FILE")
if [ "$OLD_FRONTMATTER" != "$NEW_FRONTMATTER" ]; then
FRONTMATTER_CHANGED=true
echo "Frontmatter changed in: $FILE"
break
fi
done <<< "$CHANGED_FILES"
# Strip 'content/' prefix and convert to JSON array
FILE_PATHS_JSON=$(echo "$CHANGED_FILES" | sed 's|^content/||' | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "File paths JSON: $FILE_PATHS_JSON"
echo "frontmatterChanged: $FRONTMATTER_CHANGED"
echo "filePaths=$FILE_PATHS_JSON" >> $GITHUB_OUTPUT
echo "frontmatterChanged=$FRONTMATTER_CHANGED" >> $GITHUB_OUTPUT
- name: Call revalidation API
if: steps.changed-files.outputs.filePaths != '[]'
run: |
ALL_PATHS='${{ steps.changed-files.outputs.filePaths }}'
TOTAL=$(echo "$ALL_PATHS" | jq 'length')
BATCH_SIZE=50
SUCCEEDED=0
FAILED=0
BATCH_NUM=0
echo "Revalidating $TOTAL files in batches of $BATCH_SIZE"
for ((i = 0; i < TOTAL; i += BATCH_SIZE)); do
BATCH_NUM=$((BATCH_NUM + 1))
BATCH=$(echo "$ALL_PATHS" | jq -c ".[$i:$((i + BATCH_SIZE))]")
CHUNK_SIZE=$(echo "$BATCH" | jq 'length')
echo ""
echo "=== Batch $BATCH_NUM ($CHUNK_SIZE paths, $((i + CHUNK_SIZE))/$TOTAL) ==="
echo "$BATCH" | jq -r '.[]'
PAYLOAD=$(jq -n --argjson paths "$BATCH" '{filePaths: $paths}')
HTTP_CODE=$(curl -X POST "${{ secrets.DOCS_SITE_URL }}/api/revalidate/markdown" \
-H "Authorization: Bearer ${{ secrets.DOCS_SITE_API_KEY }}" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
--max-time 120 \
-o response.json \
-w "%{http_code}" \
-s) || true
if [ "$HTTP_CODE" = "200" ]; then
SUCCESS=$(jq -r '.success' response.json)
if [ "$SUCCESS" = "true" ]; then
COUNT=$(jq -r '(.revalidated // []) | length' response.json)
echo "✅ Batch $BATCH_NUM: $COUNT paths revalidated"
SUCCEEDED=$((SUCCEEDED + COUNT))
else
echo "::warning::Batch $BATCH_NUM: HTTP 200 but success=false"
jq -r '(.errors // [])[]?' response.json
jq -r '[(.revalidated // [])[]? | select(.error) | .error] | .[]?' response.json
ITEM_COUNT=$(jq -r '(.revalidated // []) | length' response.json)
if [ "$ITEM_COUNT" -gt 0 ]; then
if [ "$ITEM_COUNT" -gt "$CHUNK_SIZE" ]; then
ITEM_COUNT=$CHUNK_SIZE
fi
ERRORS=$(jq -r '[(.revalidated // [])[]? | select(.error)] | length' response.json)
ITEM_SUCCEEDED=$((ITEM_COUNT - ERRORS))
UNREPORTED=$((CHUNK_SIZE - ITEM_COUNT))
SUCCEEDED=$((SUCCEEDED + ITEM_SUCCEEDED))
FAILED=$((FAILED + ERRORS + UNREPORTED))
else
FAILED=$((FAILED + CHUNK_SIZE))
fi
fi
else
echo "::error::Batch $BATCH_NUM: HTTP $HTTP_CODE"
jq '.' response.json 2>/dev/null || cat response.json 2>/dev/null || true
FAILED=$((FAILED + CHUNK_SIZE))
fi
if [ $((i + BATCH_SIZE)) -lt "$TOTAL" ]; then
sleep 2
fi
done
echo ""
echo "=== Revalidation complete ==="
echo "Total: $TOTAL | Succeeded: $SUCCEEDED | Failed: $FAILED"
if [ "$FAILED" -gt 0 ]; then
echo "::error::$FAILED paths failed to revalidate"
exit 1
fi
echo "::notice::✅ Successfully revalidated $SUCCEEDED files"
- name: Trigger reindex if frontmatter changed
if: steps.changed-files.outputs.frontmatterChanged == 'true'
run: |
echo "Frontmatter changes detected, triggering content indexer..."
gh workflow run index-main-content.yml
echo "::notice::✅ Triggered index-main-content.yml workflow"
env:
GH_TOKEN: ${{ github.token }}