PR Preview Cleanup #781
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 }} |