-
Notifications
You must be signed in to change notification settings - Fork 0
249 lines (231 loc) · 9.99 KB
/
Copy pathcleanup-gh-pages.yml
File metadata and controls
249 lines (231 loc) · 9.99 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# Stale GitHub Pages preview cleanup (companion to ig-publisher.yml).
#
# Purpose: Remove branch previews under gh-pages/branches/<branch>/ whose
# branch no longer exists, so gh-pages does not grow without bound as
# short-lived feature branches come and go. Each preview drops a
# .branch-name marker; this workflow deletes a preview ONLY when its
# marker names a branch that is gone. The formal publication ROOT and
# any version-like paths (e.g. 2026.0.0/) are NEVER cleanup
# candidates — they belong to -go-publish, not to previews.
#
# PROPAGATION: like ig-publisher.yml, this file is meant to propagate to
# modules created from this template (the first-run bootstrap keeps
# it) — every module's gh-pages benefits from the same sweep.
#
# Triggers: weekly schedule (Sundays 00:00 UTC) + manual workflow_dispatch
# (with a dry_run input to list stale previews without deleting).
#
# Toggle: vars.ENABLE_PREVIEW — ON by default; shares the preview
# subsystem's switch. Set the repo variable to 'false' to skip the
# sweep (it then shows as "skipped"). To purge once while previews are
# off, flip it on, dispatch this workflow, flip it back.
#
# Pages two modes (vars.PAGES_ACTIONS_ENABLED): the sweep always commits the
# pruned tree to the gh-pages branch (served by "Deploy from a branch")
# and, when PAGES_ACTIONS_ENABLED='true', additionally re-deploys the
# cleaned tree through the Pages Actions pipeline. Mirrors
# ig-publisher.yml / go-publish.yml.
#
# Fixed versions: every Action is pinned to a commit SHA (# vX.Y.Z).
#
# Prerequisite: none. If the repository has no `gh-pages` branch yet (no preview
# and no publication so far), the sweep skips with a ::notice — an
# empty repository must not produce a red scheduled run.
#
# Human-gated: none (dry_run lets a human preview the deletions first).
name: Cleanup stale GitHub Pages previews
on:
schedule:
- cron: "0 0 * * 0"
workflow_dispatch:
inputs:
dry_run:
description: "List stale previews without deleting them"
required: false
default: false
type: boolean
permissions:
contents: write
id-token: write
pages: write
concurrency:
# Share the gh-pages writer group so the sweep never races ig-publisher.yml or
# go-publish.yml. Queue, never cancel.
group: gh-pages-writes
cancel-in-progress: false
jobs:
cleanup:
if: ${{ vars.ENABLE_PREVIEW != 'false' }} # toggle: ON by default
runs-on: ubuntu-latest
outputs:
changed: ${{ steps.cleanup.outputs.changed }}
steps:
# gh-pages only exists once a preview or a formal publication has been
# deployed. A freshly created module has neither yet — that is NOT an
# error, there is simply nothing to sweep, so probe before checking out
# (a checkout of a missing ref fails the whole scheduled run).
- name: Check whether a gh-pages branch exists
id: probe
shell: bash
env:
REMOTE_URL: https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git
run: |
set -euo pipefail
if git ls-remote --heads "${REMOTE_URL}" gh-pages | grep -q .; then
echo "exists=true" >> "${GITHUB_OUTPUT}"
else
echo "exists=false" >> "${GITHUB_OUTPUT}"
echo "::notice::No gh-pages branch yet — nothing to clean up. It is created by the first branch preview (ig-publisher.yml) or the first formal publication."
fi
- name: Checkout gh-pages branch
if: ${{ steps.probe.outputs.exists == 'true' }}
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: gh-pages
fetch-depth: 1
- name: Remove only marker-backed stale previews
id: cleanup
if: ${{ steps.probe.outputs.exists == 'true' }}
shell: bash
env:
DRY_RUN: ${{ inputs.dry_run || false }}
PAGES_ACTIONS_ENABLED: ${{ vars.PAGES_ACTIONS_ENABLED }}
run: |
set -euo pipefail
remote_refs="$(git ls-remote --heads origin)"
mapfile -t active_branches < <(
awk '
{
sub("refs/heads/", "", $2)
if ($2 != "gh-pages") {
print $2
}
}
' <<< "${remote_refs}"
)
mapfile -d '' -t markers < <(
find . \
-path './.git' -prune -o \
-type f \
-name '.branch-name' \
-print0
)
printf 'Active branches: %s\n' "${#active_branches[@]}"
printf 'Marked preview deployments: %s\n' "${#markers[@]}"
stale_count=0
preserved_count=0
branch_is_active() {
local candidate="$1" active
for active in "${active_branches[@]}"; do
if [[ "${active}" == "${candidate}" ]]; then
return 0
fi
done
return 1
}
for marker in "${markers[@]}"; do
# An earlier stale ancestor may already have removed this marker.
[[ -f "${marker}" ]] || continue
deployment="$(dirname "${marker}")"
branch="$(tr -d '\r\n' < "${marker}")"
# A marker at the site root belongs to a formal publication; never a
# cleanup candidate.
if [[ "${deployment}" == "." ]]; then
echo "Preserving formal publication root"
preserved_count=$((preserved_count + 1))
continue
fi
relative_deployment="${deployment#./}"
# Version-like permanent paths (CalVer YYYY.n.n and friends) are formal.
if [[ "${relative_deployment}" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.][0-9A-Za-z.-]+)?$ ]]; then
echo "Preserving version-like permanent path ${relative_deployment}/"
preserved_count=$((preserved_count + 1))
continue
fi
if [[ -z "${branch}" ]]; then
echo "::warning::Ignoring empty preview marker at ${marker}"
preserved_count=$((preserved_count + 1))
continue
fi
if branch_is_active "${branch}"; then
echo "Preserving ${deployment#./}/ (${branch})"
preserved_count=$((preserved_count + 1))
continue
fi
# Branch names may contain slashes, so a stale preview can be an
# ancestor of another preview. Never recursively delete an active
# descendant deployment.
has_active_descendant=false
for descendant_marker in "${markers[@]}"; do
if [[ "${descendant_marker}" == "${marker}" ]] ||
[[ "${descendant_marker}" != "${deployment}/"* ]] ||
[[ ! -f "${descendant_marker}" ]]; then
continue
fi
descendant_branch="$(tr -d '\r\n' < "${descendant_marker}")"
if branch_is_active "${descendant_branch}"; then
echo "::warning::Preserving stale ancestor ${deployment#./}/ (${branch}) because it contains active preview ${descendant_branch}."
has_active_descendant=true
break
fi
done
if [[ "${has_active_descendant}" == "true" ]]; then
preserved_count=$((preserved_count + 1))
continue
fi
stale_count=$((stale_count + 1))
if [[ "${DRY_RUN}" == "true" ]]; then
echo "[dry run] Would remove ${deployment#./}/ (${branch})"
else
echo "Removing ${deployment#./}/ (${branch})"
rm -rf -- "${deployment}"
fi
done
changed=false
if [[ "${DRY_RUN}" != "true" ]]; then
if [[ "${PAGES_ACTIONS_ENABLED}" == "true" ]]; then
bytes="$(du --bytes --summarize --exclude=.git . | cut -f1)"
supported_max_bytes="1000000000"
if (( bytes > supported_max_bytes )); then
echo "::warning::The cleaned Pages site is ${bytes} bytes, above GitHub Pages' ${supported_max_bytes}-byte officially supported maximum. Cleanup and deployment will continue, but Pages does not guarantee deployments above 1 GB."
fi
fi
git add --all
if ! git diff --cached --quiet; then
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit -m "chore: remove ${stale_count} stale Pages preview(s)"
git push origin gh-pages
changed=true
fi
fi
echo "changed=${changed}" >> "${GITHUB_OUTPUT}"
{
echo "### GitHub Pages preview cleanup"
echo
echo "- Dry run: ${DRY_RUN}"
echo "- Preserved: ${preserved_count}"
echo "- Stale: ${stale_count}"
echo "- Changed: ${changed}"
} >> "${GITHUB_STEP_SUMMARY}"
- name: Package cleaned gh-pages tree as a Pages artifact
if: ${{ steps.cleanup.outputs.changed == 'true' && vars.PAGES_ACTIONS_ENABLED == 'true' }}
uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
with:
path: .
deploy:
name: Deploy cleaned Pages tree (Actions mode)
if: ${{ vars.ENABLE_PREVIEW != 'false' && needs.cleanup.outputs.changed == 'true' && vars.PAGES_ACTIONS_ENABLED == 'true' }}
needs: cleanup
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
permissions:
contents: read
id-token: write
pages: write
steps:
- name: Deploy Pages artifact
id: deployment
uses: actions/deploy-pages@cd2ce8fcbc39b97be8ca5fce6e763baed58fa128 # v5.0.0