-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
90 lines (77 loc) · 3.48 KB
/
action.yml
File metadata and controls
90 lines (77 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: 'Cache Cleanup Action'
description: 'Removes unused GitHub Actions caches based on PR status and age'
author: 'Frank Finnøy Burmo'
branding:
icon: 'trash-2'
color: 'green'
inputs:
main-branch-retention-days:
description: 'Number of days before main branch caches are deleted'
required: false
default: '7'
github-token:
description: 'GitHub token with permission to delete caches'
required: false
default: ${{ github.token }}
runs:
using: 'composite'
steps:
- name: Delete unused caches
uses: actions/github-script@v8
with:
github-token: ${{ inputs.github-token }}
script: |
const mainBranchRetentionDays = parseInt('${{ inputs.main-branch-retention-days }}');
const caches = await github.rest.actions.getActionsCacheList({
owner: context.repo.owner,
repo: context.repo.repo
});
console.log(`Total number of caches: ${caches.data.total_count}`);
// Get all open pull requests
const pulls = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open'
});
console.log(`Number of open pull requests: ${pulls.data.length}`);
// Create a list of all active PR branch references
const activeBranches = pulls.data.map(pr => pr.head.ref);
console.log('Active PR branches:', activeBranches);
let deletedCount = 0;
// Process all caches
for (const cache of caches.data.actions_caches) {
// Check if the cache is from a PR branch
if (cache.ref && cache.ref.includes('refs/pull/')) {
// Extract PR number from the ref (format: refs/pull/X/merge or refs/pull/X/head)
const prMatch = cache.ref.match(/refs\/pull\/(\d+)/);
if (prMatch) {
const prNumber = parseInt(prMatch[1]);
// Check if this PR is still open
const isPrActive = pulls.data.some(pr => pr.number === prNumber);
if (!isPrActive) {
console.log(`Deleting cache for closed PR #${prNumber}: ${cache.key} (${cache.ref})`);
await github.rest.actions.deleteActionsCacheById({
owner: context.repo.owner,
repo: context.repo.repo,
cache_id: cache.id
});
deletedCount++;
}
}
} else if (cache.ref && cache.ref.includes('refs/heads/main')) {
// Check if the cache is from main branch and hasn't been used for X days (configurable)
const retentionDaysAgo = new Date();
retentionDaysAgo.setDate(retentionDaysAgo.getDate() - mainBranchRetentionDays);
const lastUsed = new Date(cache.last_accessed_at);
if (lastUsed < retentionDaysAgo) {
console.log(`Deleting old cache from main branch: ${cache.key} (${cache.ref})`);
await github.rest.actions.deleteActionsCacheById({
owner: context.repo.owner,
repo: context.repo.repo,
cache_id: cache.id
});
deletedCount++;
}
}
}
console.log(`Cache cleanup completed! Deleted ${deletedCount} caches.`);