Skip to content

Commit

Permalink
Merge pull request #321 from inbo/create_delete_old_artifacts
Browse files Browse the repository at this point in the history
Create delete_old_artifacts.yaml
  • Loading branch information
SanderDevisscher authored Jan 13, 2025
2 parents 094ab4b + f6624a9 commit 9856c97
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 9856c97

Please sign in to comment.