-
Notifications
You must be signed in to change notification settings - Fork 0
103 lines (85 loc) · 3.09 KB
/
sync.yml
File metadata and controls
103 lines (85 loc) · 3.09 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
name: Sync repository
# Trigger on every push (all branches)
on:
push:
branches:
- "**"
env:
TARGET_OWNER: ${{ secrets.TARGET_OWNER }}
TARGET_REPO: ${{ secrets.TARGET_REPO }}
MIRROR_ALL: "false"
permissions:
contents: read
jobs:
sync:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
- name: Determine current branch
id: vars
run: |
set -euo pipefail
if [[ "${GITHUB_REF}" =~ ^refs/heads/ ]]; then
BRANCH="${GITHUB_REF#refs/heads/}"
else
BRANCH="${GITHUB_HEAD_REF:-main}"
fi
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
- name: Find working remote URL for personal repo
id: find-url
env:
TOKEN: ${{ secrets.PERSONAL_REPO_TOKEN }}
run: |
set -euo pipefail
TARGET="${{ env.TARGET_OWNER }}/${{ env.TARGET_REPO }}.git"
FORMATS=(
"https://${{ env.TARGET_OWNER }}:${TOKEN}@github.com/${TARGET}"
"https://x-access-token:${TOKEN}@github.com/${TARGET}"
)
for url in "${FORMATS[@]}"; do
if git ls-remote "$url" >/dev/null 2>&1; then
echo "remote_url=$url" >> "$GITHUB_OUTPUT"
exit 0
fi
done
echo "ERROR: token cannot access https://github.com/${{ env.TARGET_OWNER }}/${{ env.TARGET_REPO }}" >&2
exit 1
- name: Configure git
run: |
git config user.name "Learnverse Bot"
git config user.email "learnverse-bot@users.noreply.github.com"
- name: Push branch to personal repo (excluding .github)
env:
REMOTE_URL: ${{ steps.find-url.outputs.remote_url }}
BRANCH: ${{ steps.vars.outputs.branch }}
run: |
set -euo pipefail
git remote add personal "$REMOTE_URL"
# Create a temporary branch from HEAD
TMP_BRANCH="sync-tmp-${BRANCH}-no-github"
git checkout -b "$TMP_BRANCH"
# Remove .github from index only (do not delete from source repo)
git rm -r --cached .github || true
# Commit only if there are staged changes
if git diff --cached --quiet; then
echo "No .github files to remove from index; still pushing HEAD (no change)."
else
git commit --amend --no-edit
fi
# Force-push the sanitized branch to the target branch on personal repo
echo "Pushing sanitized branch ($TMP_BRANCH → ${BRANCH})..."
git push personal "$TMP_BRANCH:refs/heads/${BRANCH}" --force
# Also push tags (non-fatal)
git push personal --tags --force || true
- name: Cleanup temporary branch
if: always()
run: |
ORIGINAL_BRANCH="${{ steps.vars.outputs.branch }}"
git checkout -f "$ORIGINAL_BRANCH" || true
git branch -D "sync-tmp-${ORIGINAL_BRANCH}-no-github" || true
- name: Done
run: echo "Sync finished (excluded .github)."