-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (115 loc) · 4.58 KB
/
sync-deploy.yml
File metadata and controls
141 lines (115 loc) · 4.58 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
name: Sync Main to Deploy
on:
push:
branches:
- main
jobs:
sync:
runs-on: ubuntu-latest
permissions:
contents: write
actions: read
steps:
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN || github.token }}
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install PyYAML
- name: Process markdown files
run: python .github/scripts/process_markdown.py
- name: Configure Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Backup processed files
run: |
# Create backup of processed files before switching branches
# Copy all files and directories, including hidden ones like .gitbook
# But exclude .git and .github directories
mkdir -p /tmp/processed_src
find . -maxdepth 1 -not -name '.' -not -name '.git' -not -name '.github' -exec cp -r {} /tmp/processed_src/ \;
echo "Backed up processed files"
- name: Stash changes before branch switch
run: |
# Stash any changes made by the processing script to avoid branch switch conflicts
if ! git diff --quiet || ! git diff --cached --quiet; then
git stash push -m "Temporary stash of processed files for deploy sync"
echo "Stashed changes before branch switch"
else
echo "No changes to stash"
fi
- name: Switch to deploy branch
run: |
# Fetch the latest deploy branch
git fetch origin deploy || echo "Deploy branch doesn't exist yet, will be created"
# Switch to deploy branch (preserve existing content)
if git show-ref --verify --quiet refs/remotes/origin/deploy; then
# Deploy branch exists remotely, check it out
git checkout deploy
echo "Switched to existing deploy branch"
else
# Deploy branch doesn't exist, create it as orphan
git checkout --orphan deploy
git rm -rf . 2>/dev/null || true
echo "Created new orphan deploy branch"
fi
- name: Sync processed files to deploy branch
run: |
# Only update the src folder, preserve everything else on deploy branch
# Remove existing src folder if it exists
if [ -d "src" ]; then
rm -rf src
echo "Removed existing src folder"
fi
# Create new src directory and copy processed files
mkdir -p src
cp -r /tmp/processed_src/* src/ 2>/dev/null || true
# Also copy hidden files like .gitbook to src
cp -r /tmp/processed_src/.* src/ 2>/dev/null || true
echo "Updated src folder with processed files from main branch"
# Show what files are being synced
echo "Files in src directory:"
ls -la src/ || echo "No src directory found"
# Add and commit only src folder changes
git add src
# Check if there are changes to commit
echo "Checking for changes to commit..."
git status --porcelain
if ! git diff --staged --quiet; then
git commit -m "Sync src from main: $(git log main -1 --format='%h %s')"
# Push changes
git push origin deploy
# Trigger repository dispatch event to start GitHub Pages deployment
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/dispatches \
-d '{"event_type":"deploy-pages"}'
else
echo "No changes to commit"
fi
# Clean up backup
rm -rf /tmp/processed_src
- name: Revert changes on main branch
run: |
# Switch back to main branch
git checkout main
# Drop the stash to revert changes (we don't need them anymore)
if git stash list | grep -q "Temporary stash of processed files for deploy sync"; then
git stash drop
echo "Dropped stash - changes reverted"
else
echo "No stash to drop"
fi
# Ensure we're in clean state
git reset --hard HEAD
echo "Reverted processing changes on main branch"