-
Notifications
You must be signed in to change notification settings - Fork 0
316 lines (279 loc) · 12 KB
/
Copy pathsync.yml
File metadata and controls
316 lines (279 loc) · 12 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# 这个文件应该放在外部仓库的 .github/workflows/ 目录下
name: Reusable Sync Workflow
on:
workflow_call:
inputs:
target_branch:
description: '目标分支名称'
required: true
type: string
repository_name:
description: '仓库名称 (格式: owner/repo)'
required: true
type: string
enable_gitee:
description: '是否同步到 Gitee'
required: false
type: boolean
default: true
enable_cnb:
description: '是否同步到 CNB'
required: false
type: boolean
default: true
gitee_private:
description: '自动创建 Gitee 仓库时是否创建为私有仓库'
required: false
type: boolean
default: false
cnb_visibility:
description: '自动创建 CNB 仓库时使用的可见性'
required: false
type: string
default: public
jobs:
sync-to-gitee:
if: ${{ inputs.enable_gitee }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # 获取所有提交历史
- name: Set up Git user
run: |
git config --global user.email "${{ vars.GIT_USER_EMAIL }}"
git config --global user.name "${{ vars.GIT_USER_NAME }}"
- name: Fetch repo info from GitHub
id: info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
REPO_NAME="${{ inputs.repository_name }}"
REPO_ONLY="${REPO_NAME#*/}"
echo "repo_name=${REPO_ONLY}" >> $GITHUB_OUTPUT
gh repo view "${REPO_NAME}" --json description,homepageUrl,repositoryTopics > /tmp/gh_repo.json
DESC=$(jq -r '.description // ""' /tmp/gh_repo.json)
echo "description=${DESC}" >> $GITHUB_OUTPUT
SITE_URL=$(jq -r '.homepageUrl // ""' /tmp/gh_repo.json)
echo "site_url=${SITE_URL}" >> $GITHUB_OUTPUT
TOPICS_JSON=$(jq -c '[(.repositoryTopics // [])[] | if type == "object" then .name else . end]' /tmp/gh_repo.json)
echo "topics_json=${TOPICS_JSON}" >> $GITHUB_OUTPUT
- name: Init repo
id: gitee-init
env:
GITEE_TOKEN: ${{ secrets.GITEE_GITHUB_ACTION_SYNC_TOKEN }}
REPO: ${{ steps.info.outputs.repo_name }}
GH_DESC: ${{ steps.info.outputs.description }}
GH_SITE: ${{ steps.info.outputs.site_url }}
GITEE_PRIVATE: ${{ inputs.gitee_private }}
run: |
if [ -z "$GITEE_TOKEN" ]; then
echo "GITEE_GITHUB_ACTION_SYNC_TOKEN not configured, skipping"
echo "created=false" >> $GITHUB_OUTPUT
echo "needs_update=false" >> $GITHUB_OUTPUT
exit 0
fi
RESPONSE=$(curl -s -w "\n%{http_code}" \
"https://gitee.com/api/v5/repos/GameFrameX/${REPO}?access_token=${GITEE_TOKEN}")
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" != "200" ]; then
echo "Creating Gitee repo: GameFrameX/${REPO}"
curl -s -X POST \
"https://gitee.com/api/v5/orgs/GameFrameX/repos" \
-H "Content-Type: application/json" \
-d "{
\"access_token\": \"${GITEE_TOKEN}\",
\"name\": \"${REPO}\",
\"private\": ${GITEE_PRIVATE},
\"auto_init\": false
}" > /dev/null 2>&1 || true
echo "created=true" >> $GITHUB_OUTPUT
echo "needs_update=false" >> $GITHUB_OUTPUT
else
echo "Gitee repo GameFrameX/${REPO} already exists"
echo "created=false" >> $GITHUB_OUTPUT
CURRENT_DESC=$(echo "$BODY" | jq -r '.description // ""')
CURRENT_HOME=$(echo "$BODY" | jq -r '.homepage // ""')
NEEDS_UPDATE="false"
echo " GitHub -> description: '${GH_DESC}', homepage: '${GH_SITE}'"
echo " Gitee -> description: '${CURRENT_DESC}', homepage: '${CURRENT_HOME}'"
if [ "$CURRENT_DESC" != "$GH_DESC" ] || [ "$CURRENT_HOME" != "$GH_SITE" ]; then
NEEDS_UPDATE="true"
echo " Info mismatch, needs update"
else
echo " Info matches, skipping setup"
fi
echo "needs_update=${NEEDS_UPDATE}" >> $GITHUB_OUTPUT
fi
- name: Setup repo
if: steps.gitee-init.outputs.created == 'true' || steps.gitee-init.outputs.needs_update == 'true'
env:
GITEE_GITHUB_ACTION_SYNC_TOKEN: ${{ secrets.GITEE_GITHUB_ACTION_SYNC_TOKEN }}
REPO: ${{ steps.info.outputs.repo_name }}
DESC: ${{ steps.info.outputs.description }}
SITE_URL: ${{ steps.info.outputs.site_url }}
run: |
echo "Setting up repo info..."
RESULT=$(curl -s -w "\n%{http_code}" -X PATCH \
"https://gitee.com/api/v5/repos/GameFrameX/${REPO}" \
-H "Content-Type: application/json" \
-d "{
\"access_token\": \"${GITEE_GITHUB_ACTION_SYNC_TOKEN}\",
\"name\": \"${REPO}\",
\"description\": \"${DESC}\",
\"homepage\": \"${SITE_URL}\"
}")
RESULT_CODE=$(echo "$RESULT" | tail -1)
if [ "$RESULT_CODE" != "200" ]; then
echo "Warning: Gitee setup failed (HTTP $RESULT_CODE)"
echo "$RESULT" | sed '$d'
else
echo "Gitee repo info updated successfully"
fi
- name: Set SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.GITEE_ID_RSA }}" >> ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
eval $(ssh-agent -s) && ssh-add ~/.ssh/id_rsa
# 信任域名
ssh-keyscan -H ${{ vars.GITEE_DOMAIN_URL }} >> ~/.ssh/known_hosts
- name: Check current branch
run: echo "当前分支:${{ inputs.target_branch }} ${{ inputs.repository_name }}"
- name: Echo git url
run: echo "git@${{ vars.GITEE_DOMAIN_URL }}:${{ inputs.repository_name }}.git"
- name: Add remote url
run: git remote add mirror "git@${{ vars.GITEE_DOMAIN_URL }}:${{ inputs.repository_name }}.git"
- name: Fetch
run: git fetch --prune mirror --tags --verbose
- name: Pull and push
run: |
if [ "${{ inputs.target_branch }}" ]; then
git checkout ${{ inputs.target_branch }}
git push -f mirror ${{ inputs.target_branch }}
git push -f mirror --tags --verbose
fi
sync-to-cnb:
if: ${{ inputs.enable_cnb }}
runs-on: ubuntu-latest
env:
CNB_SYNC_TOKEN_PRESENT: ${{ secrets.CNB_SYNC_TOKEN != '' }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # 获取所有提交历史
- name: Fetch repo info from GitHub
id: info
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
REPO_NAME="${{ inputs.repository_name }}"
REPO_ONLY="${REPO_NAME#*/}"
echo "repo_name=${REPO_ONLY}" >> $GITHUB_OUTPUT
gh repo view "${REPO_NAME}" --json description,homepageUrl,repositoryTopics > /tmp/gh_repo.json
DESC=$(jq -r '.description // ""' /tmp/gh_repo.json)
echo "description=${DESC}" >> $GITHUB_OUTPUT
SITE_URL=$(jq -r '.homepageUrl // ""' /tmp/gh_repo.json)
echo "site_url=${SITE_URL}" >> $GITHUB_OUTPUT
TOPICS_JSON=$(jq -c '[(.repositoryTopics // [])[] | if type == "object" then .name else . end]' /tmp/gh_repo.json)
echo "topics_json=${TOPICS_JSON}" >> $GITHUB_OUTPUT
- name: Init repo
id: cnb-init
env:
CNB_TOKEN: ${{ secrets.CNB_SYNC_TOKEN }}
REPO: ${{ steps.info.outputs.repo_name }}
GH_DESC: ${{ steps.info.outputs.description }}
GH_SITE: ${{ steps.info.outputs.site_url }}
GH_TOPICS: ${{ steps.info.outputs.topics_json }}
CNB_VISIBILITY: ${{ inputs.cnb_visibility }}
run: |
if [ -z "$CNB_TOKEN" ]; then
echo "CNB_SYNC_TOKEN not configured, skipping"
echo "created=false" >> $GITHUB_OUTPUT
echo "needs_update=false" >> $GITHUB_OUTPUT
exit 0
fi
RESPONSE=$(curl -s -w "\n%{http_code}" \
-H "Authorization: Bearer ${CNB_TOKEN}" \
"https://api.cnb.cool/gameframex/${REPO}")
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" != "200" ]; then
echo "Creating CNB repo: gameframex/${REPO}"
curl -s -X POST \
"https://api.cnb.cool/gameframex/-/repos" \
-H "Accept: application/vnd.cnb.api+json" \
-H "Authorization: Bearer ${CNB_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${REPO}\",
\"visibility\": \"${CNB_VISIBILITY}\"
}" > /dev/null 2>&1 || true
echo "created=true" >> $GITHUB_OUTPUT
echo "needs_update=false" >> $GITHUB_OUTPUT
else
echo "CNB repo gameframex/${REPO} already exists"
echo "created=false" >> $GITHUB_OUTPUT
# 比较当前信息与 GitHub 是否一致
CURRENT_DESC=$(echo "$BODY" | jq -r '.description // ""')
CURRENT_SITE=$(echo "$BODY" | jq -r '.site // ""')
CURRENT_TOPICS=$(echo "$BODY" | jq -c '
if (.topics | type) == "array" then
[.topics[] | tostring]
elif (.topics | type) == "string" then
(.topics | if . == "" then [] else split(",") | map(gsub("^\\s+|\\s+$"; "")) | map(select(length > 0)) end)
else
[]
end | sort
')
SORTED_GH_TOPICS=$(echo "$GH_TOPICS" | jq -c '[.[] | select(length <= 12)][:12] | sort')
NEEDS_UPDATE="false"
if [ "$CURRENT_DESC" != "$GH_DESC" ] || [ "$CURRENT_SITE" != "$GH_SITE" ] || [ "$CURRENT_TOPICS" != "$SORTED_GH_TOPICS" ]; then
NEEDS_UPDATE="true"
echo " description: '${CURRENT_DESC}' -> '${GH_DESC}'"
echo " site: '${CURRENT_SITE}' -> '${GH_SITE}'"
echo " topics: ${CURRENT_TOPICS} -> ${SORTED_GH_TOPICS}"
fi
echo "needs_update=${NEEDS_UPDATE}" >> $GITHUB_OUTPUT
fi
- name: Setup repo
if: steps.cnb-init.outputs.created == 'true' || steps.cnb-init.outputs.needs_update == 'true'
env:
CNB_SYNC_TOKEN: ${{ secrets.CNB_SYNC_TOKEN }}
REPO: ${{ steps.info.outputs.repo_name }}
DESC: ${{ steps.info.outputs.description }}
SITE_URL: ${{ steps.info.outputs.site_url }}
TOPICS: ${{ steps.info.outputs.topics_json }}
run: |
echo "Setting up repo info..."
TRIMMED_TOPICS=$(echo "$TOPICS" | jq -c '[.[] | select(length <= 12)][:12]')
RESULT=$(curl -s -w "\n%{http_code}" -X PATCH \
"https://api.cnb.cool/gameframex/${REPO}" \
-H "Authorization: Bearer ${CNB_SYNC_TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"description\": \"${DESC}\",
\"site\": \"${SITE_URL}\",
\"topics\": ${TRIMMED_TOPICS}
}")
RESULT_CODE=$(echo "$RESULT" | tail -1)
if [ "$RESULT_CODE" != "200" ]; then
echo "Warning: CNB setup failed (HTTP $RESULT_CODE)"
echo "$RESULT" | sed '$d'
else
echo "CNB repo info updated successfully"
fi
- name: Sync to CNB Repository
if: env.CNB_SYNC_TOKEN_PRESENT == 'true'
uses: docker://tencentcom/git-sync
env:
PLUGIN_TARGET_URL: "https://cnb.cool/${{ inputs.repository_name }}.git"
PLUGIN_AUTH_TYPE: "https"
PLUGIN_USERNAME: "cnb"
PLUGIN_PASSWORD: ${{ secrets.CNB_SYNC_TOKEN }}
PLUGIN_FORCE: "true"
PLUGIN_SYNC_MODE: "rebase"
PLUGIN_PUSH_TAGS: "true"