forked from ramp4-pcar4/ramp4-pcar4
-
Notifications
You must be signed in to change notification settings - Fork 0
93 lines (76 loc) · 3.52 KB
/
demo-cleanup.yml
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
name: Clean up demo folders when they no longer exist
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
- name: Set up Git
run: |
git config --global user.name "GitHub Action"
git config --global user.email "[email protected]"
- 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
# Let the next steps know that changes were made and deployment is needed
echo "changes=true" >> $GITHUB_ENV
else
echo "No changes to commit"
# Let the next steps know that no changes were made and deployment will be skipped
echo "changes=false" >> $GITHUB_ENV
fi
- 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