Skip to content

Commit

Permalink
chore: add cleanup gh pages (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmerget authored Feb 17, 2025
1 parent b5f8053 commit e981d5b
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: Cleans all preview pages for gh-pages

on:
schedule:
- cron: "0 0 * * *"

jobs:
clean:
name: Clean
runs-on: ubuntu-24.04 # Use Ubuntu 24.04 explicitly
steps:
- name: ⏬ Checkout repo
uses: actions/checkout@v4

- name: 📥 Get gh-pages tar
run: wget -q https://github.com/db-ux-design-system/theme-builder/tarball/gh-pages

- name: ➕ Create dist dir
run: mkdir dist

- name: 📦 Unpack Tar
run: tar -zxf gh-pages -C dist --strip-components 1

- name: 🗑️ Clean all preview pages
id: cleanup
uses: actions/github-script@v7
with:
result-encoding: json
script: |
const { default: cleanUpPages } = await import('${{ github.workspace }}/scripts/cleanup-gh-pages.js');
return await cleanUpPages({github, context});
- name: 🥅 Deploy to GH-Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./dist
59 changes: 59 additions & 0 deletions scripts/cleanup-gh-pages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Fetches all branches and deletes all review-branches in github pages
*/
import FS from 'node:fs';

const TAG = 'cleanup-gh-pages:';

const removeOldFromPath = (isTag, data) => {
const path = `public/${isTag ? 'version' : 'review'}`;
if (
FS.existsSync(path) &&
data?.filter((branch) => branch.name).length > 0
) {
const dirsToDelete = FS.readdirSync(path)
.filter((file) => !data.find((branch) => branch.name === file))
// Let's not clean up specific folders
.filter((file) => !['main', 'latest'].includes(file));
if (dirsToDelete?.length > 0) {
console.log(
TAG,
`Start removing ${isTag ? 'tags' : 'branches'} from gh-pages`
);
console.log(TAG, dirsToDelete);
for (const dir of dirsToDelete) {
FS.rmSync(`${path}/${dir}`, {
recursive: true,
force: true
});
console.log(TAG, `deleted ${isTag ? 'tag' : 'branch'} ${dir}`);
}

return true;
}

console.log(TAG, `All ${isTag ? 'tags' : 'branches'} are up to date`);
}

return false;
};

const cleanUpPages = async ({ github, context }) => {
const { repo, owner } = context.repo;
const branches = await github.rest.repos.listBranches({
owner,
repo
});
const tags = await github.rest.repos.listTags({
owner,
repo
});

return {
deploy:
removeOldFromPath(false, branches.data) ||
removeOldFromPath(true, tags.data)
};
};

export default cleanUpPages;

0 comments on commit e981d5b

Please sign in to comment.