Skip to content

Commit

Permalink
Add cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
milespetrov committed Aug 30, 2024
1 parent 55e8770 commit 9eedf3b
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 155 deletions.
105 changes: 0 additions & 105 deletions .github/workflows/build-demo.yml

This file was deleted.

101 changes: 101 additions & 0 deletions .github/workflows/demo-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Workflow for building and deploying a demo site to GitHub Pages
name: Deploy static demo files to Pages

on: push

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: write
pages: write
id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these deployments to complete.
concurrency:
group: 'pages'
cancel-in-progress: false

jobs:
build:
name: Build the project
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '22.5.1'
cache: 'npm'

- name: Build or delete the demo
run: |
BRANCH_OR_TAG_NAME=${GITHUB_REF#refs/heads/}
BRANCH_OR_TAG_NAME=${BRANCH_OR_TAG_NAME#refs/tags/}
git config --global user.email "miles.petrov@ec.gc.ca"
git config --global user.name "Miles Petrov"
git fetch --all
# Check if demo-page branch exists and create it if necessary
if git ls-remote --heads origin demo-page | grep -q "refs/heads/demo-page"; then
echo "demo-page exists."
git checkout demo-page
else
echo "demo-page does not exist. Creating an orphan branch."
git checkout --orphan demo-page
git rm -rf . || true
rm -r * || true
git commit --allow-empty -m "Initial commit on orphan demo-page"
fi
# Checkout to current branch or tag and create a temporary branch
git checkout $BRANCH_OR_TAG_NAME
git checkout -b temp-$BRANCH_OR_TAG_NAME
# Create a subfolder and checkout demo-page's content
mkdir demo-folder
git --work-tree=demo-folder checkout demo-page -- .
# Modify and commit changes
npm ci
npm run build
mv dist demo-folder/$BRANCH_OR_TAG_NAME
rm -rf node_modules
git add demo-folder
git commit -m "Modified contents of demo-page within demo-folder"
# Apply the changes to demo-page
git checkout demo-page
git checkout temp-$BRANCH_OR_TAG_NAME -- demo-folder
mv demo-folder/* .
rmdir demo-folder
git add .
git commit -m "Applied changes from demo-folder to demo-page"
# Squash the commit history into a single commit
git reset --soft $(git rev-list --max-parents=0 HEAD)
git commit -m "Applied changes from demo-folder to demo-page"
git push -f origin demo-page
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: '.'
retention-days: 1

- name: Setup Pages
uses: actions/configure-pages@v5

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
91 changes: 91 additions & 0 deletions .github/workflows/demo-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: Clean Folders and Deploy

on:
schedule:
- cron: "0 0 * * *" # Runs every day at midnight
workflow_dispatch: # Allows manual trigger from the GitHub website

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: write
pages: write
id-token: write

jobs:
clean_folders:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Checkout the repo
uses: actions/checkout@v4
with:
ref: demo-page # Checkout the `demo-page` branch

- name: Set up Git
run: |
git config --global user.name "GitHub Action"
git config --global user.email "action@github.com"
- name: Fetch remote branches and tags from origin
run: git fetch origin --prune

- name: Run cleanup script
run: |
# Get all remote branch names from origin
remote_branches=$(git branch -r | grep -v '\->' | grep "origin/" | sed "s# *origin/##g")
# Get all remote tag names from origin
remote_tags=$(git ls-remote --tags origin | awk -F/ '{print $NF}' | sed 's/\^{}//')
# Combine branches and tags into a single list
remote_refs=$(echo -e "$remote_branches\n$remote_tags")
# Get all folder names in the current directory
local_folders=$(ls -d */ | sed 's#/##')
# Loop through each folder in the current directory
for folder in $local_folders; do
# Check if the folder name exists in the list of remote refs
if ! echo "$remote_refs" | grep -qw "$folder"; then
# If the folder name doesn't exist in the remote refs, delete the folder
echo "Deleting folder: $folder"
rm -rf "$folder"
fi
done
- name: Commit and push changes
id: commit-changes
run: |
git add .
if ! git diff --cached --exit-code > /dev/null; then
git commit -m "Automated folder cleanup"
# Squash the commit history into a single commit
git reset --soft $(git rev-list --max-parents=0 HEAD)
git commit -m "Automated folder cleanup"
git push -f origin demo-page
echo "changes=true" >> $GITHUB_ENV
else
echo "No changes to commit"
echo "changes=false" >> $GITHUB_ENV
# Deploy to GitHub Pages if changes were made
- name: Upload artifact
if: env.changes == 'true'
uses: actions/upload-pages-artifact@v3
with:
path: '.'
retention-days: 1

- name: Setup Pages
if: env.changes == 'true'
uses: actions/configure-pages@v5

- name: Deploy to GitHub Pages
if: env.changes == 'true'
id: deployment
uses: actions/deploy-pages@v4
50 changes: 0 additions & 50 deletions .github/workflows/pages-cleanup.yml

This file was deleted.

0 comments on commit 9eedf3b

Please sign in to comment.