-
Notifications
You must be signed in to change notification settings - Fork 0
287 lines (244 loc) · 10.8 KB
/
sync.yml
File metadata and controls
287 lines (244 loc) · 10.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
name: Sync Child Repo Changes
on:
workflow_dispatch: {} # Manual trigger
# Triggered by child repo when main branch is updated
jobs:
sync-child-repo:
runs-on: ubuntu-latest
concurrency:
group: sync-child-repo
cancel-in-progress: false
env:
PARENT_REPO: ${{ github.repository }}
PARENT_BRANCH: main # Base branch for new sync branch
CHILD_REPO: ${{ vars.CHILD_REPO || 'devmaster-x/ex_child' }} # Child repo reference
CHILD_BRANCH: main # Child repo main branch
PARENT_MODULE_PATH: ./app # Target path in parent repo
steps:
- name: Checkout parent repo (main branch)
uses: actions/checkout@v4
with:
ref: ${{ env.PARENT_BRANCH }}
fetch-depth: 0
token: ${{ secrets.CHILD_REPO_TOKEN }} # Use PAT for authentication
- name: Configure Git Authentication
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Configure git to use the PAT for authentication
git remote set-url origin https://x-access-token:${{ secrets.CHILD_REPO_TOKEN }}@github.com/${{ github.repository }}.git
echo "Updated git remote URL with PAT authentication"
- name: Update main branch
run: |
# Ensure we have the latest changes from main branch
git fetch origin
git reset --hard origin/${{ env.PARENT_BRANCH }}
echo "Updated to latest ${{ env.PARENT_BRANCH }} branch"
- name: Set timestamp
run: |
TIMESTAMP=$(date '+%Y-%m-%d-%H-%M-%S')
echo "TIMESTAMP=$TIMESTAMP" >> $GITHUB_ENV
echo "Set timestamp: $TIMESTAMP"
- name: Create sync branch
run: |
# Create a more unique branch name with timestamp and run ID
BRANCH_NAME="sync-child-repo-${{ github.run_id }}-${{ env.TIMESTAMP }}"
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
# Check if branch already exists locally and delete it
if git branch --list "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
echo "Local branch $BRANCH_NAME already exists, deleting it..."
git branch -D "$BRANCH_NAME"
fi
# Create the new branch
git checkout -b "$BRANCH_NAME"
echo "Created branch: $BRANCH_NAME"
- name: Checkout child repo
uses: actions/checkout@v4
with:
repository: ${{ env.CHILD_REPO }}
ref: ${{ env.CHILD_BRANCH }}
path: child-repo
token: ${{ secrets.CHILD_REPO_TOKEN }} # PAT with access to child repo
fetch-depth: 0
submodules: false # Explicitly disable submodules
- name: Sync child repo -> parent repo
run: |
echo "=== STARTING SYNC PROCESS ==="
echo "Current working directory: $(pwd)"
echo "Child repo path: child-repo"
echo "Target path: ${{ env.PARENT_MODULE_PATH }}"
echo ""
echo "=== CHILD REPO CONTENTS ==="
echo "Root contents:"
ls -la child-repo/
echo ""
echo "App directory contents:"
ls -la child-repo/app/
echo ""
echo "=== TARGET DIRECTORY STATUS ==="
echo "Target directory before sync:"
ls -la "${{ env.PARENT_MODULE_PATH }}/" || echo "Target directory doesn't exist yet"
echo ""
echo "=== PERFORMING SYNC ==="
# Create target directory if it doesn't exist
mkdir -p "${{ env.PARENT_MODULE_PATH }}"
# Remove all existing content in target directory
echo "Removing existing content..."
rm -rf "${{ env.PARENT_MODULE_PATH }}"/*
# Copy ALL files from child repo app directory to target
echo "Copying files from child-repo/app to ${{ env.PARENT_MODULE_PATH }}..."
# Use cp with verbose output for reliable copying
cp -rv child-repo/app/* "${{ env.PARENT_MODULE_PATH }}/"
echo ""
echo "=== VERIFICATION ==="
echo "Target directory contents after sync:"
ls -la "${{ env.PARENT_MODULE_PATH }}/"
echo ""
echo "=== FILE COUNT COMPARISON ==="
echo "Files in child repo app: $(find child-repo/app -type f | wc -l)"
echo "Files in target directory: $(find "${{ env.PARENT_MODULE_PATH }}" -type f | wc -l)"
echo ""
echo "=== SYNC COMPLETE ==="
- name: Clean up unwanted files
run: |
echo "=== CLEANING UP UNWANTED FILES ==="
# Remove .git directory if it exists
if [ -d "${{ env.PARENT_MODULE_PATH }}/.git" ]; then
echo "Removing .git directory..."
rm -rf "${{ env.PARENT_MODULE_PATH }}/.git"
fi
# Remove any other unnecessary files
for file in .github .gitignore .gitattributes; do
if [ -e "${{ env.PARENT_MODULE_PATH }}/$file" ]; then
echo "Removing $file..."
rm -rf "${{ env.PARENT_MODULE_PATH }}/$file"
fi
done
echo "Cleanup complete!"
- name: Verify sync results
run: |
echo "=== VERIFYING SYNC RESULTS ==="
echo "Expected files: app.js, homepage.js, readme.md, style.css"
echo ""
# Check each expected file
expected_files=("app.js" "homepage.js" "readme.md" "style.css")
missing_files=()
for file in "${expected_files[@]}"; do
if [ -f "${{ env.PARENT_MODULE_PATH }}/$file" ]; then
echo "✅ $file found"
# Get file size (works on both Linux and macOS)
if command -v stat >/dev/null 2>&1; then
if stat --version >/dev/null 2>&1; then
# Linux stat
size=$(stat -c%s "${{ env.PARENT_MODULE_PATH }}/$file" 2>/dev/null || echo "unknown")
else
# macOS stat
size=$(stat -f%z "${{ env.PARENT_MODULE_PATH }}/$file" 2>/dev/null || echo "unknown")
fi
else
size="unknown"
fi
echo " Size: $size bytes"
else
echo "❌ $file missing"
missing_files+=("$file")
fi
done
echo ""
echo "=== FINAL VERIFICATION ==="
if [ ${#missing_files[@]} -eq 0 ]; then
echo "🎉 All expected files are present!"
else
echo "⚠️ Missing files: ${missing_files[*]}"
echo "Current target directory contents:"
ls -la "${{ env.PARENT_MODULE_PATH }}/"
exit 1
fi
- name: Debug git status
run: |
echo "=== GIT STATUS DEBUG ==="
echo "Current git status:"
git status
echo ""
echo "Staged files:"
git diff --cached --name-only || echo "No staged files"
echo ""
echo "Modified files:"
git diff --name-only || echo "No modified files"
echo ""
echo "Untracked files:"
git ls-files --others --exclude-standard || echo "No untracked files"
- name: Check for changes
id: check-changes
run: |
if [[ -n "$(git status --porcelain)" ]]; then
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Changes detected, will create PR"
else
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No changes detected"
fi
- name: Commit changes
if: steps.check-changes.outputs.has_changes == 'true'
run: |
git add "${{ env.PARENT_MODULE_PATH }}"
git commit -m "Sync child repo from ${{ env.CHILD_REPO }} @ ${{ github.sha }}
- Source: ${{ env.CHILD_REPO }}@${{ env.CHILD_BRANCH }}
- Target: ${{ env.PARENT_MODULE_PATH }}
- Timestamp: ${{ env.TIMESTAMP }}
- Triggered by: ${{ github.event_name }}"
- name: Clean up existing remote branch
if: steps.check-changes.outputs.has_changes == 'true'
run: |
# Check if remote branch exists and delete it if it does
if git ls-remote --heads origin "${{ env.BRANCH_NAME }}" | grep -q "${{ env.BRANCH_NAME }}"; then
echo "Remote branch ${{ env.BRANCH_NAME }} already exists, deleting it first..."
git push origin --delete "${{ env.BRANCH_NAME }}" || true
echo "Remote branch deleted successfully"
else
echo "No existing remote branch found"
fi
- name: Push sync branch
if: steps.check-changes.outputs.has_changes == 'true'
run: |
# Push the new branch
git push origin "${{ env.BRANCH_NAME }}"
echo "Pushed branch: ${{ env.BRANCH_NAME }}"
- name: Create Pull Request
if: steps.check-changes.outputs.has_changes == 'true'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.CHILD_REPO_TOKEN }}
commit-message: "Sync child repo from ${{ env.CHILD_REPO }} @ ${{ github.sha }}"
branch: "${{ env.BRANCH_NAME }}"
base: ${{ env.PARENT_BRANCH }}
title: "Sync child repo from ${{ env.CHILD_REPO }} - ${{ env.TIMESTAMP }}"
body: |
## Automated Sync of Child Repository
This PR syncs the latest changes from the **child repository** to the **parent repository**.
### Details
- **Source**: `${{ env.CHILD_REPO }}@${{ env.CHILD_BRANCH }}`
- **Target**: `${{ env.PARENT_MODULE_PATH }}`
- **Sync Time**: `${{ env.TIMESTAMP }}`
- **Trigger**: `${{ github.event_name }}`
### What Changed
- Updated `${{ env.PARENT_MODULE_PATH }}/` directory with latest child repo content
- All files from child repo have been synchronized
### Review Notes
- This is an automated sync - please review the changes
- The sync preserves the structure and content from the source repository
- Any conflicts should be resolved manually if they occur
---
*This PR was automatically generated by GitHub Actions*
labels: |
automated
sync
child-repo
assignees: |
${{ github.repository_owner }}
- name: Cleanup on no changes
if: steps.check-changes.outputs.has_changes == 'false'
run: |
echo "No changes to sync. Skipping PR creation."
# Delete the branch if no changes
git push origin --delete "${{ env.BRANCH_NAME }}" || true