-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from inbo/create_delete_old_artifacts
Create delete_old_artifacts.yaml
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 | ||
}); | ||
} | ||
} |