amalgolopoly/.forgejo/workflows/cleanup-preview.yaml
Lukas Schaefer dd22a44eec
All checks were successful
Deploy / deploy (pull_request) Successful in 7s
Cleanup preview / cleanup-preview (pull_request) Successful in 2s
Deploy / deploy (push) Successful in 6s
Cleanup after pr closes
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
2026-08-02 19:55:17 -04:00

96 lines
3.1 KiB
YAML

name: Cleanup preview
on:
pull_request:
types: [closed]
concurrency:
group: ci-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: write
jobs:
cleanup-preview:
runs-on: selfhosted
steps:
- name: Remove preview from dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
if [ -z "${GITHUB_TOKEN}" ]; then
echo "GITHUB_TOKEN is required to push the dist branch" >&2
exit 1
fi
server="${GITHUB_SERVER_URL:?}"
server="${server#https://}"
server="${server#http://}"
remote="https://oauth2:${GITHUB_TOKEN}@${server}/${GITHUB_REPOSITORY}.git"
target_dir="pr-${PR_NUMBER:?}"
max_attempts=5
attempt=0
while [ "$attempt" -lt "$max_attempts" ]; do
attempt=$((attempt + 1))
echo "Cleanup attempt ${attempt}/${max_attempts}"
expected="$(git ls-remote "$remote" refs/heads/dist | awk '{print $1}')"
if [ -z "${expected}" ]; then
echo "No dist branch; nothing to clean up"
exit 0
fi
workdir="$(mktemp -d)"
git clone --depth 1 --branch dist "$remote" "$workdir"
if [ ! -e "${workdir}/${target_dir}" ]; then
echo "No preview at ${target_dir}; nothing to clean up"
rm -rf "$workdir"
exit 0
fi
rm -rf "${workdir}/${target_dir}"
rm -rf "${workdir}/.git"
git -C "$workdir" init -b dist
git -C "$workdir" config user.name "forgejo-actions[bot]"
git -C "$workdir" config user.email "forgejo-actions[bot]@noreply.localhost"
git -C "$workdir" add -A
git -C "$workdir" commit --allow-empty -m "Remove preview ${target_dir}"
set +e
git -C "$workdir" push --force-with-lease="refs/heads/dist:${expected}" "$remote" HEAD:dist
push_status=$?
set -e
rm -rf "$workdir"
if [ "$push_status" -eq 0 ]; then
echo "Removed ${target_dir} from dist"
exit 0
fi
echo "Push rejected (likely concurrent deploy); retrying..." >&2
sleep 1
done
echo "Failed to clean up preview after ${max_attempts} attempts" >&2
exit 1
- name: Ping update server
env:
UPDATE_SECRET: ${{ secrets.UPDATE_SECRET }}
run: |
set -euo pipefail
if [ -z "${UPDATE_SECRET}" ]; then
echo "UPDATE_SECRET secret is required" >&2
exit 1
fi
git_repo="${GITHUB_SERVER_URL:?}/${GITHUB_REPOSITORY:?}.git"
curl -fsS -X POST https://update.lschaefer.xyz/update-project \
--data-urlencode "secret=${UPDATE_SECRET}" \
--data-urlencode "git_repo=${git_repo}" \
--data-urlencode "output_directory=amalgolopoly" \
--data-urlencode "branch=dist"