Skip to content

Cleanup Old Versions #37

Cleanup Old Versions

Cleanup Old Versions #37

name: Cleanup Old Versions
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Run version cleanup script
run: |
node -e "
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const versionsDir = 'Versions';
if (!fs.existsSync(versionsDir)) {
console.log(`Directory ${versionsDir} does not exist. Exiting.`);
process.exit(0);
}
const files = fs.readdirSync(versionsDir);
// Filter only version files (originv*.*.*.html)
const versionFiles = files.filter(file =>
file.match(/^originv\d+\.\d+\.\d+\.html$/)
);
if (versionFiles.length <= 5) {
console.log(`Only ${versionFiles.length} version files found. Keeping all versions.`);
process.exit(0);
}
// Parse version numbers for proper sorting
const parsedVersions = versionFiles.map(file => {
const versionMatch = file.match(/^originv(\d+)\.(\d+)\.(\d+)\.html$/);
if (versionMatch) {
const [fullMatch, major, minor, patch] = versionMatch;
return {
filename: file,
path: path.join(versionsDir, file),
version: [parseInt(major), parseInt(minor), parseInt(patch)]
};
}
return null;
}).filter(v => v !== null);
// Sort versions in descending order (newest first)
parsedVersions.sort((a, b) => {
// Compare major version
if (a.version[0] !== b.version[0]) {
return b.version[0] - a.version[0];
}
// Compare minor version
if (a.version[1] !== b.version[1]) {
return b.version[1] - a.version[1];
}
// Compare patch version
return b.version[2] - a.version[2];
});
// Keep only the 5 most recent versions
const keepVersions = parsedVersions.slice(0, 5);
const removeVersions = parsedVersions.slice(5);
// Display the versions we're keeping
console.log('Keeping the following versions:');
keepVersions.forEach(v => {
console.log(`- ${v.filename}`);
});
// Remove old versions
if (removeVersions.length > 0) {
console.log('\\nRemoving the following versions:');
removeVersions.forEach(version => {
console.log(`- ${version.filename}`);
try {
fs.unlinkSync(version.path);
} catch (error) {
console.error(`Error removing ${version.path}: ${error.message}`);
}
});
}
console.log(`\\nCleanup complete. Removed ${removeVersions.length} old versions, kept ${keepVersions.length} versions.`);
"
- name: Commit changes if any files were removed
run: |
git config --global user.name "GitHub Action"
git config --global user.email "action@github.com"
if [[ -n $(git status -s) ]]; then
git add -A
git commit -m "Automated cleanup of old versions - keeping 5 most recent"
git push
else
echo "No changes to commit"
fi