Skip to content

Housekeeping

Housekeeping #4

Workflow file for this run

name: Housekeeping
on:
schedule:
- cron: '0 3 * * 0'
workflow_dispatch:
permissions:
actions: write
contents: read
env:
WORKFLOW_RUNS_RETENTION_DAYS: 90
jobs:
cleanup-workflow-runs:
name: Cleanup Old Workflow Runs
runs-on: ubuntu-latest
steps:
- name: Delete old workflow runs
uses: Mattraks/delete-workflow-runs@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ github.repository }}
retain_days: ${{ env.WORKFLOW_RUNS_RETENTION_DAYS }}
keep_minimum_runs: 5
delete_workflow_pattern: '*'
delete_run_by_conclusion_pattern: |
cancelled
skipped
failure
cleanup-caches:
name: Cleanup Orphaned Caches
runs-on: ubuntu-latest
steps:
- name: Cleanup caches
uses: actions/github-script@v8
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
let caches = [];
let page = 1;
while (true) {
const response = await github.rest.actions.getActionsCacheList({
owner, repo, per_page: 100, page: page
});
caches = caches.concat(response.data.actions_caches);
if (response.data.actions_caches.length < 100) break;
page++;
}
let deletedCount = 0;
for (const cache of caches) {
if (cache.ref.startsWith('refs/pull/')) {
const prNumber = cache.ref.match(/refs\/pull\/(\d+)/)?.[1];
if (prNumber) {
try {
const pr = await github.rest.pulls.get({owner, repo, pull_number: parseInt(prNumber)});
if (pr.data.state === 'closed') {
await github.rest.actions.deleteActionsCacheById({owner, repo, cache_id: cache.id});
deletedCount++;
}
} catch (e) {
await github.rest.actions.deleteActionsCacheById({owner, repo, cache_id: cache.id});
deletedCount++;
}
}
}
}
console.log(`Deleted ${deletedCount} orphaned caches`);