name: Cleanup Old Caches
on:
workflow_dispatch:
workflow_run:
workflows:
- Cross Platform Builds
- Deploy Frontend
- Publish
types:
- completed
permissions:
actions: write
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup caches not accessed in last 1 hour
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
// Get current timestamp
const currentTime = Math.floor(Date.now() / 1000);
const oneHourInSeconds = 60 * 60; // 1 hour in seconds
try {
// List all caches for the repository
const cachesResponse = await github.rest.actions.getActionsCacheList({
owner,
repo,
per_page: 100 // Get up to 100 caches per request
});
let deletedCount = 0;
let totalCount = cachesResponse.data.actions_caches.length;
console.log(`Found ${totalCount} total caches`);
for (const cache of cachesResponse.data.actions_caches) {
// Calculate age of cache in seconds (since last access)
const cacheAccessTime = Math.floor(new Date(cache.last_accessed_at).getTime() / 1000);
const ageInSeconds = currentTime - cacheAccessTime;
// Delete cache if it hasn't been accessed in the last 1 hour
if (ageInSeconds > oneHourInSeconds) {
console.log(`Deleting cache with ID ${cache.id}, key: ${cache.key}, last accessed: ${cache.last_accessed_at}`);
await github.rest.actions.deleteActionsCacheById({
owner,
repo,
cache_id: cache.id
});
deletedCount++;
}
}
console.log(`Successfully deleted ${deletedCount} caches out of ${totalCount} total caches.`);
core.setOutput('deleted-count', deletedCount);
core.setOutput('total-count', totalCount);
} catch (error) {
console.error('Error during cache cleanup:', error);
core.setFailed(error.message);
}