-
Notifications
You must be signed in to change notification settings - Fork 0
267 lines (234 loc) Β· 11.4 KB
/
cleanup-container-images.yml
File metadata and controls
267 lines (234 loc) Β· 11.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
name: Cleanup Container Images
# WARNING: This workflow deletes container images permanently!
# Always test with dry_run=true first to preview what will be deleted.
# The 'latest' tag and the N most recent versions are always preserved.
"on":
schedule:
# Run monthly on the 1st at 02:00 UTC
- cron: "0 2 1 * *"
workflow_dispatch:
inputs:
retention_days:
description: "Number of days to keep images (default: 30)"
required: false
default: "30"
type: string
dry_run:
description: "Perform a dry run without actually deleting images"
required: false
default: true
type: boolean
keep_latest_count:
description: "Number of latest versions to always keep (default: 5)"
required: false
default: "5"
type: string
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
permissions:
contents: read
packages: write
jobs:
cleanup:
name: Clean Up Old Container Images
runs-on: ubuntu-latest
steps:
- name: Validate inputs
run: |
# Set defaults for scheduled runs
RETENTION_DAYS="${{ github.event.inputs.retention_days || '30' }}"
DRY_RUN="${{ github.event.inputs.dry_run || 'true' }}"
KEEP_LATEST_COUNT="${{ github.event.inputs.keep_latest_count || '5' }}"
# Validate inputs
if ! [[ "$RETENTION_DAYS" =~ ^[0-9]+$ ]] || [ "$RETENTION_DAYS" -lt 1 ]; then
echo "β Error: retention_days must be a positive integer"
exit 1
fi
if ! [[ "$KEEP_LATEST_COUNT" =~ ^[0-9]+$ ]] || [ "$KEEP_LATEST_COUNT" -lt 1 ]; then
echo "β Error: keep_latest_count must be a positive integer"
exit 1
fi
# Export validated values
echo "RETENTION_DAYS=$RETENTION_DAYS" >> $GITHUB_ENV
echo "DRY_RUN=$DRY_RUN" >> $GITHUB_ENV
echo "KEEP_LATEST_COUNT=$KEEP_LATEST_COUNT" >> $GITHUB_ENV
echo "β
Configuration validated:"
echo " - Retention period: $RETENTION_DAYS days"
echo " - Dry run mode: $DRY_RUN"
echo " - Keep latest count: $KEEP_LATEST_COUNT"
- name: Cleanup old container images
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const retentionDays = parseInt(process.env.RETENTION_DAYS);
const dryRun = process.env.DRY_RUN === 'true';
const keepLatestCount = parseInt(process.env.KEEP_LATEST_COUNT);
const [owner, repo] = process.env.IMAGE_NAME.split('/');
console.log(`π Starting cleanup for ${owner}/${repo}`);
console.log(`π
Retention period: ${retentionDays} days`);
console.log(`π·οΈ Keep latest count: ${keepLatestCount}`);
console.log(`π§ͺ Dry run mode: ${dryRun ? 'ENABLED' : 'DISABLED'}`);
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - retentionDays);
console.log(`π Cutoff date: ${cutoffDate.toISOString()}`);
try {
// Detect whether the owner is an organization or a user. The Packages API has
// separate org vs user endpoints; using the wrong one can make deletions appear
// to succeed in logs while not affecting the actual package namespace.
let isOrg = false;
try {
await github.rest.orgs.get({ org: owner });
isOrg = true;
console.log(`π Owner ${owner} is an organization`);
} catch (err) {
// If org lookup fails with 404, assume it's a user account.
if (err.status === 404) {
console.log(`π Owner ${owner} is a user account`);
isOrg = false;
} else {
throw err;
}
}
console.log('π¦ Fetching packages...');
// Use pagination to ensure we see all package versions
let packages = [];
if (isOrg) {
packages = await github.paginate(github.rest.packages.getAllPackageVersionsForOrg, {
package_type: 'container',
package_name: repo,
org: owner,
state: 'active',
per_page: 100
});
} else {
packages = await github.paginate(github.rest.packages.getAllPackageVersionsForPackageOwnedByUser, {
package_type: 'container',
package_name: repo,
username: owner,
state: 'active',
per_page: 100
});
}
console.log(`π Found ${packages.length} package versions`);
if (packages.length === 0) {
console.log('βΉοΈ No packages found to clean up');
return;
}
// Sort packages by creation date (newest first)
packages.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
console.log('\nπ Package Analysis:');
packages.slice(0, 10).forEach((pkg, index) => {
const createdAt = new Date(pkg.created_at);
const age = Math.floor((Date.now() - createdAt.getTime()) / (1000 * 60 * 60 * 24));
const tags = pkg.metadata?.container?.tags || [];
console.log(` ${index + 1}. ID: ${pkg.id}, Age: ${age}d, Tags: [${tags.join(', ')}], Created: ${createdAt.toISOString()}`);
});
if (packages.length > 10) {
console.log(` ... and ${packages.length - 10} more`);
}
let deletedCount = 0;
let keptCount = 0;
const errors = [];
// Always keep the latest N versions regardless of age
const latestToKeep = packages.slice(0, keepLatestCount);
console.log(`\nπ Always keeping latest ${keepLatestCount} versions:`);
latestToKeep.forEach((pkg, index) => {
const tags = pkg.metadata?.container?.tags || [];
console.log(` ${index + 1}. ID: ${pkg.id}, Tags: [${tags.join(', ')}]`);
});
// Process remaining packages
const packagesToConsider = packages.slice(keepLatestCount);
console.log(`\nπ Evaluating ${packagesToConsider.length} packages for cleanup...`);
for (const pkg of packagesToConsider) {
const createdAt = new Date(pkg.created_at);
const isOld = createdAt < cutoffDate;
const tags = pkg.metadata?.container?.tags || [];
const hasLatestTag = tags.includes('latest');
const age = Math.floor((Date.now() - createdAt.getTime()) / (1000 * 60 * 60 * 24));
// Never delete 'latest' tag
if (hasLatestTag) {
console.log(`π KEEP: ID ${pkg.id} (has 'latest' tag), Age: ${age}d, Tags: [${tags.join(', ')}]`);
keptCount++;
continue;
}
// Delete if older than retention period
if (isOld) {
console.log(`ποΈ DELETE: ID ${pkg.id}, Age: ${age}d, Tags: [${tags.join(', ')}]`);
if (!dryRun) {
try {
if (isOrg) {
await github.rest.packages.deletePackageVersionForOrg({
package_type: 'container',
package_name: repo,
org: owner,
package_version_id: pkg.id
});
} else {
await github.rest.packages.deletePackageVersionForUser({
package_type: 'container',
package_name: repo,
username: owner,
package_version_id: pkg.id
});
}
console.log(` β
Successfully deleted package version ${pkg.id}`);
} catch (error) {
const errorMsg = `Failed to delete package version ${pkg.id}: ${error.message}`;
console.log(` β ${errorMsg}`);
errors.push(errorMsg);
// If we hit a permissions error, surface it immediately
if (error.status === 403) {
console.log(' π Permission error deleting package version - check token scope and org settings');
}
continue;
}
}
deletedCount++;
} else {
console.log(`π KEEP: ID ${pkg.id} (within retention period), Age: ${age}d, Tags: [${tags.join(', ')}]`);
keptCount++;
}
}
// Summary
console.log('\nπ Cleanup Summary:');
console.log(` π Kept packages: ${keptCount + keepLatestCount}`);
console.log(` ποΈ ${dryRun ? 'Would delete' : 'Deleted'} packages: ${deletedCount}`);
console.log(` π¦ Total packages processed: ${packages.length}`);
if (errors.length > 0) {
console.log(`\nβ Errors encountered (${errors.length}):`);
errors.forEach(error => console.log(` - ${error}`));
}
if (dryRun) {
console.log('\nπ§ͺ This was a DRY RUN - no packages were actually deleted');
console.log('π‘ To perform actual deletion, set dry_run to false');
} else if (deletedCount > 0) {
console.log('\nβ
Cleanup completed successfully');
} else {
console.log('\nβΉοΈ No packages needed cleanup');
}
} catch (error) {
console.error('β Failed to cleanup packages:', error.message);
// Check if it's a permissions issue
if (error.status === 403) {
console.error('π This might be a permissions issue. Ensure the workflow has "packages: write" permission.');
} else if (error.status === 404) {
console.error('π¦ Package not found. This might be normal if no container images exist yet.');
}
throw error;
}
- name: Cleanup summary
run: |
echo "π Container image cleanup workflow completed"
echo ""
echo "βΉοΈ Configuration used:"
echo " - Retention period: $RETENTION_DAYS days"
echo " - Keep latest count: $KEEP_LATEST_COUNT"
echo " - Dry run mode: $DRY_RUN"
echo ""
echo "π‘ Tips:"
echo " - Run with dry_run=true first to preview changes"
echo " - Adjust retention_days based on your needs"
echo " - The 'latest' tag is always preserved"
echo " - The newest $KEEP_LATEST_COUNT versions are always kept"