Skip to content

Commit

Permalink
Create delete_old_artifacts.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderDevisscher committed Jan 13, 2025
1 parent 094ab4b commit f6624a9
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/delete_old_artifacts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: 'Delete old artifacts'
on:
schedule:
- cron: '0 0 * * *' # Run daily at midnight
workflow_dispatch:

jobs:
delete-artifacts:
runs-on: ubuntu-latest
steps:
- name: Delete old artifacts
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo } = context.repo;
const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
const artifacts = await github.paginate(github.rest.actions.listArtifactsForRepo, {
owner,
repo,
per_page: 100
});
const artifactMap = new Map();
for (const artifact of artifacts) {
const createdAt = new Date(artifact.created_at);
if (createdAt > sevenDaysAgo) {
// Keep all artifacts less than 7 days old
continue;
}
if (!artifactMap.has(artifact.name) || createdAt > new Date(artifactMap.get(artifact.name).created_at)) {
artifactMap.set(artifact.name, artifact);
}
}
for (const artifact of artifacts) {
const createdAt = new Date(artifact.created_at);
if (createdAt > sevenDaysAgo) {
// Keep all artifacts less than 7 days old
continue;
}
if (artifact.id !== artifactMap.get(artifact.name)?.id) {
console.log(`Deleting artifact ${artifact.name} with ID ${artifact.id}`);
await github.rest.actions.deleteArtifact({
owner,
repo,
artifact_id: artifact.id
});
}
}

0 comments on commit f6624a9

Please sign in to comment.