@@ -11,6 +11,7 @@ permissions:
1111
1212env :
1313 FORCE_JAVASCRIPT_ACTIONS_TO_NODE24 : true
14+ RETAINED_GH_PAGES_BUILDS : 3
1415
1516jobs :
1617 build :
@@ -191,22 +192,92 @@ jobs:
191192 </html>
192193 EOF
193194
194- - name : Deploy build to gh-pages
195+ - name : Stage demo deployment
196+ run : |
197+ SHORT_SHA="${{ steps.sha.outputs.short }}"
198+ mkdir -p "staging/demo/build/${SHORT_SHA}" "staging/demo/build/latest"
199+ cp index.html "staging/demo/build/${SHORT_SHA}/index.html"
200+ cp hra.jsdos "staging/demo/build/${SHORT_SHA}/hra.jsdos"
201+ cp latest-redirect/index.html "staging/demo/build/latest/index.html"
202+
203+ - name : Deploy demo to gh-pages
195204 uses : peaceiris/actions-gh-pages@v4
196205 with :
197206 github_token : ${{ secrets.GITHUB_TOKEN }}
198- publish_dir : ./
199- destination_dir : demo/build/${{ steps.sha.outputs.short }}
200- exclude_assets : " WOLF3D,latest-redirect"
207+ publish_dir : ./staging/demo/build
208+ destination_dir : demo/build
201209 keep_files : true
202210
203- - name : Deploy latest redirect to gh-pages
204- uses : peaceiris/actions-gh-pages@v4
211+ - name : Smoke test deployed demo
212+ run : |
213+ SHORT_SHA="${{ steps.sha.outputs.short }}"
214+ for url in \
215+ "https://luke-b.github.io/DosTest/demo/build/${SHORT_SHA}/" \
216+ "https://luke-b.github.io/DosTest/demo/build/latest/"
217+ do
218+ echo "Checking ${url}"
219+ curl --fail --silent --show-error --location \
220+ --retry 3 --retry-delay 5 --retry-all-errors \
221+ "${url}" > /dev/null
222+ done
223+
224+ - name : Checkout gh-pages for pruning
225+ uses : actions/checkout@v4
205226 with :
206- github_token : ${{ secrets.GITHUB_TOKEN }}
207- publish_dir : ./latest-redirect
208- destination_dir : demo/build/latest
209- keep_files : true
227+ ref : gh-pages
228+ path : gh-pages-prune
229+ token : ${{ secrets.GITHUB_TOKEN }}
230+
231+ - name : Prune old gh-pages builds
232+ run : |
233+ set -euo pipefail
234+ cd gh-pages-prune
235+
236+ build_root="demo/build"
237+ mkdir -p "${build_root}"
238+ retain_count="${RETAINED_GH_PAGES_BUILDS}"
239+
240+ case "${retain_count}" in
241+ ''|*[!0-9]*|0)
242+ echo "RETAINED_GH_PAGES_BUILDS must be set to a positive integer." >&2
243+ exit 1
244+ ;;
245+ esac
246+
247+ build_timestamps_file="$(mktemp)"
248+ while read -r dir_path; do
249+ dir="$(basename "${dir_path}")"
250+ [ "${dir}" = "latest" ] && continue
251+ if [ ! -d "${build_root}/${dir}" ]; then
252+ echo "Build directory ${build_root}/${dir} does not exist." >&2
253+ exit 1
254+ fi
255+ if ! ts="$(git log -1 --format=%ct -- "${build_root}/${dir}" 2>&1)"; then
256+ echo "Failed to determine timestamp for ${build_root}/${dir}: ${ts}" >&2
257+ exit 1
258+ fi
259+ printf '%s %s\n' "${ts}" "${dir}" >> "${build_timestamps_file}"
260+ done < <(find "${build_root}" -mindepth 1 -maxdepth 1 -type d)
261+
262+ mapfile -t sha_build_dirs < <(sort -rn "${build_timestamps_file}" | awk '{print $2}' | grep -v '^$')
263+
264+ if [ "${#sha_build_dirs[@]}" -le "${retain_count}" ]; then
265+ echo "No old builds to prune."
266+ exit 0
267+ fi
268+
269+ for dir in "${sha_build_dirs[@]:${retain_count}}"; do
270+ echo "Removing old build ${dir}"
271+ rm -rf "${build_root}/${dir}"
272+ done
273+
274+ git config user.name "github-actions[bot]"
275+ git config user.email "github-actions[bot]@users.noreply.github.com"
276+ git add --all "${build_root}"
277+ if ! git diff --cached --quiet; then
278+ git commit -m "chore: prune old demo builds [skip ci]"
279+ git push
280+ fi
210281
211282 update-readme :
212283 runs-on : ubuntu-latest
0 commit comments