Repository navigation
Create cleanup_cron_job.yml #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| name: Cleanup Successful and Cancelled Workflow Runs | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: "0 7 * * 0" | ||
|
|
||
| permissions: | ||
| actions: write | ||
| contents: read | ||
| pull-requests: read | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| name: Cleanup successful and cancelled runs | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| - 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)" | ||
|
|
||
| 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)' | | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| while read -r run; do | ||
| RUN_ID=$(echo "$run" | jq -r '.id') | ||
| EVENT=$(echo "$run" | jq -r '.event') | ||
|
|
||
| DELETE_RUN=true | ||
| if [[ "$EVENT" == "pull_request" ]]; then | ||
| PR_NUMBERS=$(echo "$run" | jq -r '.prs[]') | ||
|
|
||
| for PR_NUMBER in $PR_NUMBERS; do | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the workflow around the referenced lines.
file=".github/workflows/cleanup_cron_job.yml"
wc -l "$file"
sed -n '1,220p' "$file"
# Find where EVENT and prs are populated/used in this workflow.
rg -n '\bEVENT\b|\.prs\b|pull_request|pull_request_target|gh pr view|DELETE_RUN' "$file"Repository: jbosstm/lra Length of output: 3116 🌐 Web query:
💡 Result: In the GitHub Actions REST API, the Citations:
Use PR association instead of gating on 🤖 Prompt for AI Agents |
||
| STATE=$(gh pr view "$PR_NUMBER" \ | ||
| --repo "$REPO" \ | ||
| --json state \ | ||
| -q '.state') || { | ||
| echo "Failed to view PR #$PR_NUMBER; skipping run $RUN_ID" >&2 | ||
| DELETE_RUN=false | ||
| break | ||
| } | ||
|
|
||
| STATE=$(echo "$STATE" | tr '[:upper:]' '[:lower:]') | ||
|
|
||
| if [[ "$STATE" == "open" ]]; then | ||
| DELETE_RUN=false | ||
| echo "Skipping run $RUN_ID (PR #$PR_NUMBER is still open)" | ||
| break | ||
| fi | ||
| done | ||
| fi | ||
|
|
||
| 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 | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
Uh oh!
There was an error while loading. Please reload this page.