Skip to content

Commit 56324ec

Browse files
authored
Add Gitea sync workflow
This workflow synchronizes the GitHub repository with the Gitea repository on a schedule and allows manual triggering.
1 parent 2946fee commit 56324ec

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: Sync from Gitea (master→main, keep workflow)
2+
3+
on:
4+
schedule:
5+
# 2 times per day (UTC): 7:00, 11:00
6+
- cron: '0 7,11 * * *'
7+
workflow_dispatch: {}
8+
9+
permissions:
10+
contents: write # allow pushing with GITHUB_TOKEN
11+
12+
jobs:
13+
mirror:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Check out GitHub repo
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Fetch from Gitea
23+
env:
24+
GITEA_URL: ${{ secrets.GITEA_URL }}
25+
GITEA_USER: ${{ secrets.GITEA_USERNAME }}
26+
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
27+
run: |
28+
# Build authenticated Gitea URL: https://USER:TOKEN@...
29+
AUTH_URL="${GITEA_URL/https:\/\//https:\/\/$GITEA_USER:$GITEA_TOKEN@}"
30+
31+
git remote add gitea "$AUTH_URL"
32+
git fetch gitea --prune
33+
34+
- name: Update main from gitea/master, keep workflow, and force-push
35+
env:
36+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37+
GH_REPO: ${{ github.repository }}
38+
run: |
39+
# Configure identity for commits made by this workflow
40+
git config user.name "github-actions[bot]"
41+
git config user.email "github-actions[bot]@users.noreply.github.com"
42+
43+
# Authenticated push URL for GitHub
44+
git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${GH_REPO}.git"
45+
46+
WF_PATH=".github/workflows/sync-from-gitea.yml"
47+
48+
# If the workflow exists in the current checkout, save a copy
49+
if [ -f "$WF_PATH" ]; then
50+
mkdir -p /tmp/gh-workflows
51+
cp "$WF_PATH" /tmp/gh-workflows/
52+
fi
53+
54+
# Reset local 'main' to exactly match gitea/master
55+
if git show-ref --verify --quiet refs/remotes/gitea/master; then
56+
git checkout -B main gitea/master
57+
else
58+
echo "No gitea/master found, nothing to sync."
59+
exit 0
60+
fi
61+
62+
# Restore the workflow into the new HEAD and commit if needed
63+
if [ -f "/tmp/gh-workflows/sync-from-gitea.yml" ]; then
64+
mkdir -p .github/workflows
65+
cp /tmp/gh-workflows/sync-from-gitea.yml "$WF_PATH"
66+
git add "$WF_PATH"
67+
if ! git diff --cached --quiet; then
68+
git commit -m "Inject GitHub sync workflow"
69+
fi
70+
fi
71+
72+
# Force-push main so GitHub mirrors Gitea + workflow
73+
git push origin main --force

0 commit comments

Comments
 (0)