From 2b0951d7693f3a00c7e6895f5232165a88d81d98 Mon Sep 17 00:00:00 2001 From: jmfinelli <61553861+jmfinelli@users.noreply.github.com> Date: Tue, 30 Jun 2026 10:06:31 +0100 Subject: [PATCH 1/4] Create cleanup_cron_job.yml --- .github/workflows/cleanup_cron_job.yml | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 .github/workflows/cleanup_cron_job.yml diff --git a/.github/workflows/cleanup_cron_job.yml b/.github/workflows/cleanup_cron_job.yml new file mode 100644 index 000000000..629144e73 --- /dev/null +++ b/.github/workflows/cleanup_cron_job.yml @@ -0,0 +1,58 @@ +name: Cleanup Successful Workflow Runs + +on: + schedule: + - cron: "0 7 * * 0" + +jobs: + cleanup: + runs-on: ubuntu-latest + steps: + - name: Delete successful workflow runs + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + REPO="$GITHUB_REPOSITORY" + echo "Target repository: $REPO" + + RUNS=$(gh run list \ + --repo "$REPO" \ + --limit 500 \ + --json databaseId,event,headBranch,conclusion \ + -q '.[] | select(.conclusion=="success") | {id:.databaseId, event:.event, branch:.headBranch}') + + echo "$RUNS" | jq -c '.' | while read -r run; do + RUN_ID=$(echo "$run" | jq -r '.id') + EVENT=$(echo "$run" | jq -r '.event') + BRANCH=$(echo "$run" | jq -r '.branch') + + DELETE_RUN=true + if [[ "$EVENT" == "pull_request" && -n "$BRANCH" ]]; then + PR_NUMBER=$(gh pr list \ + --repo "$REPO" \ + --head "$BRANCH" \ + --state all \ + --json number \ + -q '.[0].number' 2>/dev/null || true) + + if [[ -n "$PR_NUMBER" ]]; then + STATE=$(gh pr view "$PR_NUMBER" \ + --repo "$REPO" \ + --json state \ + -q '.state' 2>/dev/null || echo "UNKNOWN") + + # Normalize state to lowercase for safe comparison + STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') + + if [[ "$STATE" == "open" ]]; then + DELETE_RUN=false + echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" + fi + fi + fi + + if [[ "$DELETE_RUN" == true ]]; then + echo "Deleting successful run ID: $RUN_ID" + gh run delete "$RUN_ID" --repo "$REPO" + fi + done From 6158efdf24d68e9e5a888f69ec51381400e298b6 Mon Sep 17 00:00:00 2001 From: Manuel Finelli Date: Wed, 1 Jul 2026 10:38:45 +0100 Subject: [PATCH 2/4] Addressed reviews --- .github/workflows/cleanup_cron_job.yml | 107 ++++++++++++++++--------- 1 file changed, 71 insertions(+), 36 deletions(-) diff --git a/.github/workflows/cleanup_cron_job.yml b/.github/workflows/cleanup_cron_job.yml index 629144e73..de1e5403b 100644 --- a/.github/workflows/cleanup_cron_job.yml +++ b/.github/workflows/cleanup_cron_job.yml @@ -1,58 +1,93 @@ -name: Cleanup Successful Workflow Runs +name: Cleanup Successful and Cancelled Workflow Runs on: schedule: - cron: "0 7 * * 0" +permissions: + actions: write + contents: read + +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + jobs: cleanup: + name: Cleanup successful and cancelled runs runs-on: ubuntu-latest steps: - - name: Delete successful workflow runs + - name: Delete successful and cancelled workflow runs env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | + set -euo pipefail REPO="$GITHUB_REPOSITORY" + RETENTION_DAYS=14 + CUTOFF_DATE=$(date -u -d "$RETENTION_DAYS days ago" +%Y-%m-%dT%H:%M:%SZ) echo "Target repository: $REPO" + echo "Deleting successful and cancelled runs older than $RETENTION_DAYS days (before $CUTOFF_DATE)" - RUNS=$(gh run list \ - --repo "$REPO" \ - --limit 500 \ - --json databaseId,event,headBranch,conclusion \ - -q '.[] | select(.conclusion=="success") | {id:.databaseId, event:.event, branch:.headBranch}') - - echo "$RUNS" | jq -c '.' | while read -r run; do - RUN_ID=$(echo "$run" | jq -r '.id') - EVENT=$(echo "$run" | jq -r '.event') - BRANCH=$(echo "$run" | jq -r '.branch') - - DELETE_RUN=true - if [[ "$EVENT" == "pull_request" && -n "$BRANCH" ]]; then - PR_NUMBER=$(gh pr list \ - --repo "$REPO" \ - --head "$BRANCH" \ - --state all \ - --json number \ - -q '.[0].number' 2>/dev/null || true) - - if [[ -n "$PR_NUMBER" ]]; then - STATE=$(gh pr view "$PR_NUMBER" \ + PAGE=1 + while true; do + RUNS=$(gh run list \ + --repo "$REPO" \ + --limit 100 \ + --page "$PAGE" \ + --json databaseId,event,headBranch,conclusion,createdAt \ + -q '[.[] | select(.conclusion=="success" or .conclusion=="cancelled") | {id:.databaseId, event:.event, branch:.headBranch, createdAt:.createdAt}]') + + if [[ "$(echo "$RUNS" | jq 'length')" -eq 0 ]]; then + break + fi + + echo "$RUNS" | jq -c '.[]' | while read -r run; do + RUN_ID=$(echo "$run" | jq -r '.id') + EVENT=$(echo "$run" | jq -r '.event') + BRANCH=$(echo "$run" | jq -r '.branch') + CREATED_AT=$(echo "$run" | jq -r '.createdAt') + + if [[ "$CREATED_AT" > "$CUTOFF_DATE" ]]; then + echo "Skipping run $RUN_ID (created $CREATED_AT, within retention period)" + continue + fi + + DELETE_RUN=true + if [[ "$EVENT" == "pull_request" && -n "$BRANCH" ]]; then + PR_NUMBER=$(gh pr list \ --repo "$REPO" \ - --json state \ - -q '.state' 2>/dev/null || echo "UNKNOWN") + --head "$BRANCH" \ + --state all \ + --json number \ + -q '.[0].number') || { + echo "Failed to list PRs for branch $BRANCH; skipping run $RUN_ID" >&2 + continue + } + + if [[ -n "$PR_NUMBER" ]]; then + STATE=$(gh pr view "$PR_NUMBER" \ + --repo "$REPO" \ + --json state \ + -q '.state') || { + echo "Failed to view PR $PR_NUMBER; skipping run $RUN_ID" >&2 + continue + } - # Normalize state to lowercase for safe comparison - STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') + STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') - if [[ "$STATE" == "open" ]]; then - DELETE_RUN=false - echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" + if [[ "$STATE" == "open" ]]; then + DELETE_RUN=false + echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" + fi fi fi - fi - if [[ "$DELETE_RUN" == true ]]; then - echo "Deleting successful run ID: $RUN_ID" - gh run delete "$RUN_ID" --repo "$REPO" - fi + if [[ "$DELETE_RUN" == true ]]; then + echo "Deleting run ID: $RUN_ID" + gh run delete "$RUN_ID" --repo "$REPO" + sleep 0.5 + fi + done + + PAGE=$((PAGE + 1)) done From e233e6ae8ce0f593d963be91160e34993bc00efe Mon Sep 17 00:00:00 2001 From: Manuel Finelli Date: Wed, 1 Jul 2026 12:12:58 +0100 Subject: [PATCH 3/4] Addressed coderabbit's second review --- .github/workflows/cleanup_cron_job.yml | 83 +++++++++----------------- 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/.github/workflows/cleanup_cron_job.yml b/.github/workflows/cleanup_cron_job.yml index de1e5403b..6e1bfdef7 100644 --- a/.github/workflows/cleanup_cron_job.yml +++ b/.github/workflows/cleanup_cron_job.yml @@ -7,6 +7,7 @@ on: permissions: actions: write contents: read + pull-requests: read concurrency: group: ${{ github.workflow }} @@ -28,66 +29,40 @@ jobs: echo "Target repository: $REPO" echo "Deleting successful and cancelled runs older than $RETENTION_DAYS days (before $CUTOFF_DATE)" - PAGE=1 - while true; do - RUNS=$(gh run list \ - --repo "$REPO" \ - --limit 100 \ - --page "$PAGE" \ - --json databaseId,event,headBranch,conclusion,createdAt \ - -q '[.[] | select(.conclusion=="success" or .conclusion=="cancelled") | {id:.databaseId, event:.event, branch:.headBranch, createdAt:.createdAt}]') + gh api --paginate "repos/$REPO/actions/runs?status=completed&per_page=100" \ + --jq '.workflow_runs[] | select((.conclusion == "success") or (.conclusion == "cancelled")) | {id: .id, event: .event, created_at: .created_at, prs: [.pull_requests[]?.number]}' | + jq -c --arg cutoff "$CUTOFF_DATE" 'select(.created_at < $cutoff)' | + while read -r run; do + RUN_ID=$(echo "$run" | jq -r '.id') + EVENT=$(echo "$run" | jq -r '.event') - if [[ "$(echo "$RUNS" | jq 'length')" -eq 0 ]]; then - break - fi - - echo "$RUNS" | jq -c '.[]' | while read -r run; do - RUN_ID=$(echo "$run" | jq -r '.id') - EVENT=$(echo "$run" | jq -r '.event') - BRANCH=$(echo "$run" | jq -r '.branch') - CREATED_AT=$(echo "$run" | jq -r '.createdAt') + DELETE_RUN=true + if [[ "$EVENT" == "pull_request" ]]; then + PR_NUMBERS=$(echo "$run" | jq -r '.prs[]') - if [[ "$CREATED_AT" > "$CUTOFF_DATE" ]]; then - echo "Skipping run $RUN_ID (created $CREATED_AT, within retention period)" - continue - fi - - DELETE_RUN=true - if [[ "$EVENT" == "pull_request" && -n "$BRANCH" ]]; then - PR_NUMBER=$(gh pr list \ + for PR_NUMBER in $PR_NUMBERS; do + STATE=$(gh pr view "$PR_NUMBER" \ --repo "$REPO" \ - --head "$BRANCH" \ - --state all \ - --json number \ - -q '.[0].number') || { - echo "Failed to list PRs for branch $BRANCH; skipping run $RUN_ID" >&2 - continue + --json state \ + -q '.state') || { + echo "Failed to view PR #$PR_NUMBER; skipping run $RUN_ID" >&2 + DELETE_RUN=false + break } - if [[ -n "$PR_NUMBER" ]]; then - STATE=$(gh pr view "$PR_NUMBER" \ - --repo "$REPO" \ - --json state \ - -q '.state') || { - echo "Failed to view PR $PR_NUMBER; skipping run $RUN_ID" >&2 - continue - } - - STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') + STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') - if [[ "$STATE" == "open" ]]; then - DELETE_RUN=false - echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" - fi + if [[ "$STATE" == "open" ]]; then + DELETE_RUN=false + echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" + break fi - fi - - if [[ "$DELETE_RUN" == true ]]; then - echo "Deleting run ID: $RUN_ID" - gh run delete "$RUN_ID" --repo "$REPO" - sleep 0.5 - fi - done + done + fi - PAGE=$((PAGE + 1)) + if [[ "$DELETE_RUN" == true ]]; then + echo "Deleting run ID: $RUN_ID" + gh run delete "$RUN_ID" --repo "$REPO" || echo "Failed to delete run $RUN_ID" >&2 + sleep 0.5 + fi done From cce04336aeb466fabf013c19d87fe28f7a0efa73 Mon Sep 17 00:00:00 2001 From: Manuel Finelli Date: Wed, 1 Jul 2026 13:13:42 +0100 Subject: [PATCH 4/4] Addressed coderabbit's third review --- .github/workflows/cleanup_cron_job.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cleanup_cron_job.yml b/.github/workflows/cleanup_cron_job.yml index 6e1bfdef7..b703b3f5b 100644 --- a/.github/workflows/cleanup_cron_job.yml +++ b/.github/workflows/cleanup_cron_job.yml @@ -29,15 +29,15 @@ jobs: echo "Target repository: $REPO" echo "Deleting successful and cancelled runs older than $RETENTION_DAYS days (before $CUTOFF_DATE)" - gh api --paginate "repos/$REPO/actions/runs?status=completed&per_page=100" \ - --jq '.workflow_runs[] | select((.conclusion == "success") or (.conclusion == "cancelled")) | {id: .id, event: .event, created_at: .created_at, prs: [.pull_requests[]?.number]}' | + gh api --paginate "repos/$REPO/actions/runs?per_page=100" \ + --jq '.workflow_runs[] | select(.status == "completed") | select((.conclusion == "success") or (.conclusion == "cancelled")) | {id: .id, created_at: .created_at, prs: [.pull_requests[]?.number]}' | jq -c --arg cutoff "$CUTOFF_DATE" 'select(.created_at < $cutoff)' | while read -r run; do RUN_ID=$(echo "$run" | jq -r '.id') - EVENT=$(echo "$run" | jq -r '.event') + PR_COUNT=$(echo "$run" | jq '.prs | length') DELETE_RUN=true - if [[ "$EVENT" == "pull_request" ]]; then + if [[ "$PR_COUNT" -gt 0 ]]; then PR_NUMBERS=$(echo "$run" | jq -r '.prs[]') for PR_NUMBER in $PR_NUMBERS; do @@ -62,7 +62,7 @@ jobs: if [[ "$DELETE_RUN" == true ]]; then echo "Deleting run ID: $RUN_ID" - gh run delete "$RUN_ID" --repo "$REPO" || echo "Failed to delete run $RUN_ID" >&2 + gh run delete "$RUN_ID" --repo "$REPO" sleep 0.5 fi done