From e2e45f216983522462ab2d2955028f246eb83ac7 Mon Sep 17 00:00:00 2001 From: Lam Nguyen Date: Tue, 7 Oct 2025 10:43:33 -0700 Subject: [PATCH] feat: Add workflow to prune stale branches --- .github/workflows/stale-branches.yml | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/stale-branches.yml diff --git a/.github/workflows/stale-branches.yml b/.github/workflows/stale-branches.yml new file mode 100644 index 000000000..cfbe0c295 --- /dev/null +++ b/.github/workflows/stale-branches.yml @@ -0,0 +1,41 @@ +name: Prune stale branches +on: + workflow_dispatch: + schedule: + - cron: '30 1 * * *' # run every day at 01:30 UTC + +jobs: + stale-branches: + runs-on: ubuntu-latest + env: + ORG: nginx + REPO: documentation + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - name: Prune all stale branches + run: | + HAS_MORE="true" + + while [ "$HAS_MORE" == "true" ]; do + RESPONSE=$(curl -L -s \ + -X GET \ + -H "Accept: application/json" \ + -s "https://github.com/${{env.ORG}}/${{env.REPO}}/branches/stale") + + HAS_MORE=$(echo "$RESPONSE" | jq -r '.payload.has_more') + BRANCHES=$(echo "$RESPONSE" | jq -r '.payload.branches[].name') + + for BRANCH in $BRANCHES; do + echo "Deleting branch $BRANCH..." + DELETE_RESPONSE=$(curl -L -s \ + -X DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "Authorization: Bearer ${{env.GITHUB_TOKEN}}" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/${{env.ORG}}/${{env.REPO}}/git/refs/heads/$BRANCH) + echo "Delete response for branch $BRANCH: $DELETE_RESPONSE" + done + done +