Skip to content

PR Preview Cleanup #781

PR Preview Cleanup

PR Preview Cleanup #781

Workflow file for this run

# Github action to cleanup a PR preview
# Runs on dispatch or weekly schedule and deletes closed PR previews that are older than 7 days
#
name: PR Preview Cleanup
on:
workflow_dispatch:
schedule:
- cron: "0 3 * * 1"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages
- name: Iterate and Check PRs
id: pr_check
run: |
# Get the list of PR folders inside 'pr/' (e.g., pr/123, pr/456)
pr_folders=($(find pr -mindepth 1 -maxdepth 1 -type d -printf "%f\n"))
closed_pr=()
for pr_folder in "${pr_folders[@]}"; do
# Fetch PR status from GitHub API
pr_status=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-X GET "https://api.github.com/repos/${{ github.repository }}/pulls/$pr_folder" | jq -r '.state')
echo "PR $pr_folder is $pr_status"
# Remove the folder if PR is closed
if [[ "$pr_status" != "open" ]]; then
rm -rf "pr/$pr_folder"
closed_pr+=("$pr_folder")
echo "Removed folder pr/$pr_folder for closed PR $pr_folder"
fi
done
# Store closed PRs in GITHUB_STATE for later use
echo "${closed_pr[*]}" >> "$GITHUB_STATE"
- name: Commit and push to gh-pages
uses: EndBug/[email protected]
with:
committer_email: [email protected]
committer_name: Fortran
message: "Sphinx build cleanup pr/${{ steps.pr_check.outputs.closed_pr }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}