Skip to content

Cleanup Old Artifacts #3

Cleanup Old Artifacts

Cleanup Old Artifacts #3

name: Cleanup Old Artifacts
on:
schedule:
- cron: '0 2 * * 0'
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup old artifacts
uses: actions/github-script@v7
with:
script: |
const artifacts = await github.rest.actions.listArtifactsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
});
const sortedArtifacts = artifacts.data.artifacts.sort((a, b) =>
new Date(b.created_at) - new Date(a.created_at)
);
const toDelete = sortedArtifacts.slice(5);
for (const artifact of toDelete) {
console.log(`Deleting artifact: ${artifact.name} (${artifact.created_at})`);
await github.rest.actions.deleteArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
});
}
console.log(`Cleaned up ${toDelete.length} old artifacts`);