Cleanup old workflow runs #108
This file contains hidden or 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
| name: Cleanup old workflow runs | |
| on: | |
| schedule: | |
| - cron: '0 3 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| if: github.event.repository.fork == false | |
| permissions: | |
| actions: write | |
| steps: | |
| - uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const KEEP = 0; | |
| const workflowNames = ["VirusTotal Scan", "Cleanup old workflow runs"]; | |
| // Get workflow ID | |
| const workflows = await github.rest.actions.listRepoWorkflows({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| for (const name of workflowNames) { | |
| const wf = workflows.data.workflows.find(w => w.name === name); | |
| if (!wf) { | |
| core.info(`Workflow "${name}" not found, skipping`); | |
| continue; | |
| } | |
| // List runs | |
| const runs = await github.rest.actions.listWorkflowRuns({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| workflow_id: wf.id, | |
| per_page: 100 | |
| }); | |
| const toDelete = runs.data.workflow_runs.slice(KEEP); | |
| core.info(`Deleting ${toDelete.length} old runs`); | |
| for (const run of toDelete) { | |
| try { | |
| await github.rest.actions.deleteWorkflowRun({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| run_id: run.id | |
| }); | |
| } catch (e) { | |
| core.info(`Skip run ${run.id}: ${e.message}`); | |
| } | |
| } | |
| } |