-
Notifications
You must be signed in to change notification settings - Fork 2
514 lines (443 loc) · 21.4 KB
/
sync-release-to-gitcode.yml
File metadata and controls
514 lines (443 loc) · 21.4 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
name: Sync Release to GitCode
on:
release:
types: [created, published, prereleased]
workflow_dispatch:
inputs:
release_name:
description: 'Release name (optional, defaults to tag name)'
required: false
type: string
release_body:
description: 'Release description/body (optional, fetched from GitHub if not provided)'
required: false
type: string
prerelease:
description: 'Is this a prerelease?'
required: false
type: boolean
default: false
draft:
description: 'Is this a draft release?'
required: false
type: boolean
default: false
test_mode:
description: 'Test mode (dry run - no actual sync to GitCode)'
required: false
type: boolean
default: false
env:
GITCODE_API_BASE: https://gitcode.com/api/v5
GITCODE_OWNER: ${{ vars.GITCODE_OWNER || 'mkdir700' }}
GITCODE_REPO: EchoPlayer
jobs:
sync-to-gitcode:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Get release information
id: release
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# Manual trigger - use selected tag/branch from workflow UI
echo "🔧 Manual trigger detected"
# Get the selected ref (can be a tag or branch)
SELECTED_REF="${{ github.ref_name }}"
REF_TYPE="${{ github.ref_type }}"
echo "Selected ref: $SELECTED_REF"
echo "Ref type: $REF_TYPE"
# If user selected a tag directly from the Tags tab, use it
if [ "$REF_TYPE" = "tag" ]; then
TAG_NAME="$SELECTED_REF"
echo "✅ Using selected tag: $TAG_NAME"
else
# If it's a branch, try to extract tag from branch name
echo "Selected ref is a branch, trying to extract tag from branch name"
if [[ "$SELECTED_REF" =~ (v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?) ]]; then
TAG_NAME="${BASH_REMATCH[1]}"
echo "✅ Extracted tag from branch name: $TAG_NAME"
else
echo "❌ Could not extract tag from branch name: $SELECTED_REF"
echo "Please either:"
echo " 1. Select a tag from the 'Tags' tab in the workflow UI, or"
echo " 2. Use a branch name containing a version tag (e.g., release/v1.0.0)"
exit 1
fi
fi
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
echo "release_name=${{ github.event.inputs.release_name || '$TAG_NAME' }}" >> $GITHUB_OUTPUT
# Handle release body - fetch from GitHub if not provided
if [ -z "${{ github.event.inputs.release_body }}" ]; then
echo "📄 No release description provided, fetching from GitHub API..."
release_response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG_NAME")
if [ "$(echo "$release_response" | jq -r '.message // empty')" = "Not Found" ]; then
echo "⚠️ Release not found for tag: $TAG_NAME, using default description"
github_release_body="Release created via manual trigger for tag $TAG_NAME"
else
github_release_body=$(echo "$release_response" | jq -r '.body // empty')
if [ -z "$github_release_body" ] || [ "$github_release_body" = "null" ]; then
echo "⚠️ No description found in GitHub release, using default description"
github_release_body="Release created via manual trigger for tag $TAG_NAME"
else
echo "✅ Successfully fetched release description from GitHub"
fi
fi
echo "release_body<<EOF" >> $GITHUB_OUTPUT
echo "$github_release_body" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "📝 Using provided release description"
echo "release_body<<EOF" >> $GITHUB_OUTPUT
echo "${{ github.event.inputs.release_body }}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
echo "prerelease=${{ github.event.inputs.prerelease }}" >> $GITHUB_OUTPUT
echo "draft=${{ github.event.inputs.draft }}" >> $GITHUB_OUTPUT
echo "test_mode=${{ github.event.inputs.test_mode }}" >> $GITHUB_OUTPUT
else
# Automatic trigger - use release event data
echo "🚀 Release event detected, using release data"
echo "tag_name=${{ github.event.release.tag_name }}" >> $GITHUB_OUTPUT
echo "release_name=${{ github.event.release.name }}" >> $GITHUB_OUTPUT
echo "release_body<<EOF" >> $GITHUB_OUTPUT
echo "${{ github.event.release.body }}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "prerelease=${{ github.event.release.prerelease }}" >> $GITHUB_OUTPUT
echo "draft=${{ github.event.release.draft }}" >> $GITHUB_OUTPUT
echo "test_mode=false" >> $GITHUB_OUTPUT
fi
- name: Sync repository to GitCode
if: steps.release.outputs.test_mode != 'true'
run: |
echo "🔄 Syncing repository to GitCode using HTTPS..."
# Configure git with token authentication
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
# Construct GitCode repository URL with token authentication
GITCODE_REPO_URL="https://oauth2:${{ secrets.GITCODE_ACCESS_TOKEN }}@gitcode.com/$GITCODE_OWNER/$GITCODE_REPO.git"
echo "Repository: $GITCODE_OWNER/$GITCODE_REPO"
# Add GitCode remote (remove if exists)
if git remote | grep -q "gitcode"; then
echo "Removing existing gitcode remote"
git remote remove gitcode
fi
echo "Adding GitCode remote with HTTPS authentication"
git remote add gitcode "$GITCODE_REPO_URL"
echo "📤 Force pushing branches to GitCode..."
# Show available branches
echo "Available branches:"
git branch -a | grep -E "(main|dev|alpha|beta)" || echo "Target branches not found"
# Force push main branches to GitCode
for branch in main dev alpha beta; do
if git show-ref --verify --quiet refs/heads/$branch || git show-ref --verify --quiet refs/remotes/origin/$branch; then
echo "Pushing branch: $branch"
if git show-ref --verify --quiet refs/heads/$branch; then
git push --force gitcode $branch:$branch || {
echo "❌ Failed to push local branch $branch"
exit 1
}
else
git push --force gitcode origin/$branch:$branch || {
echo "❌ Failed to push remote branch $branch"
exit 1
}
fi
echo "✅ Successfully pushed branch: $branch"
else
echo "⚠️ Branch $branch not found, skipping"
fi
done
echo "🏷️ Pushing all tags to GitCode..."
echo "Available tags (last 10):"
git tag | tail -10 || echo "No tags found"
git push --force gitcode --tags || {
echo "❌ Failed to push tags"
exit 1
}
echo "✅ Repository sync completed successfully"
- name: Test mode - Skip repository sync
if: steps.release.outputs.test_mode == 'true'
run: |
echo "🧪 Test mode enabled - skipping repository sync to GitCode"
echo "Would sync the following branches: main, dev, alpha, beta"
echo "Would force push all tags to GitCode"
echo "This would ensure tag ${{ steps.release.outputs.tag_name }} exists before creating release"
- name: Download release assets
id: download-assets
run: |
mkdir -p ./release-assets
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
# Manual trigger - fetch release data from GitHub API
echo "📦 Fetching release assets for tag: ${{ steps.release.outputs.tag_name }}"
release_response=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ steps.release.outputs.tag_name }}")
if [ "$(echo "$release_response" | jq -r '.message // empty')" = "Not Found" ]; then
echo "⚠️ Release not found for tag: ${{ steps.release.outputs.tag_name }}"
assets_json='[]'
else
assets_json=$(echo "$release_response" | jq '.assets')
fi
else
# Automatic trigger - use event data
assets_json='${{ toJson(github.event.release.assets) }}'
fi
echo "Assets to download:"
echo "$assets_json" | jq -r '.[] | "\(.name) - \(.browser_download_url)"'
asset_files=""
if [ "$(echo "$assets_json" | jq 'length')" -gt 0 ]; then
for asset in $(echo "$assets_json" | jq -r '.[] | @base64'); do
name=$(echo "$asset" | base64 --decode | jq -r '.name')
url=$(echo "$asset" | base64 --decode | jq -r '.browser_download_url')
echo "Downloading $name from $url"
curl -L -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-o "./release-assets/$name" "$url"
if [ -n "$asset_files" ]; then
asset_files="$asset_files,"
fi
asset_files="$asset_files./release-assets/$name"
done
fi
echo "asset_files=$asset_files" >> $GITHUB_OUTPUT
echo "has_assets=$([ -n "$asset_files" ] && echo "true" || echo "false")" >> $GITHUB_OUTPUT
- name: Check if release exists on GitCode
id: check-release
run: |
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "🧪 Test mode enabled - skipping GitCode API check"
echo "exists=false" >> $GITHUB_OUTPUT
echo "Test mode: Simulating release does not exist on GitCode"
else
# First check if tag exists using GitCode tags API
echo "Checking if tag exists..."
tags_response=$(curl -s -w "%{http_code}" \
-H "Accept: application/json" \
"$GITCODE_API_BASE/repos/$GITCODE_OWNER/$GITCODE_REPO/tags?access_token=${{ secrets.GITCODE_ACCESS_TOKEN }}")
tags_http_code="${tags_response: -3}"
tags_response_body="${tags_response%???}"
echo "Tags API HTTP Code: $tags_http_code"
tag_exists=false
if [ "$tags_http_code" = "200" ] || [ "$tags_http_code" = "201" ]; then
echo "Available tags (first 20):"
echo "$tags_response_body" | jq -r '.[] | .name' 2>/dev/null | head -20 || echo "Failed to parse tags"
# Check if our target tag exists
if echo "$tags_response_body" | jq -e --arg tag "${{ steps.release.outputs.tag_name }}" '.[] | select(.name == $tag)' > /dev/null 2>&1; then
tag_exists=true
echo "✅ Tag ${{ steps.release.outputs.tag_name }} exists on GitCode"
else
echo "❌ Tag ${{ steps.release.outputs.tag_name }} does not exist on GitCode"
fi
else
echo "❌ Failed to fetch tags from GitCode (HTTP $tags_http_code): $tags_response_body"
fi
# Then check if release exists (only if tag exists)
if [ "$tag_exists" = "true" ]; then
echo "Checking if release exists..."
response=$(curl -s -w "%{http_code}" \
-H "Accept: application/json" \
"$GITCODE_API_BASE/repos/$GITCODE_OWNER/$GITCODE_REPO/releases/tags/${{ steps.release.outputs.tag_name }}?access_token=${{ secrets.GITCODE_ACCESS_TOKEN }}")
else
echo "⚠️ Skipping release check since tag does not exist"
response="404Not Found"
fi
http_code="${response: -3}"
response_body="${response%???}"
echo "HTTP Code: $http_code"
echo "Response: $response_body"
if [ "$http_code" = "200" ]; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Release already exists on GitCode"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Release does not exist on GitCode"
fi
fi
- name: Create release on GitCode
if: steps.check-release.outputs.exists == 'false'
id: create-release
env:
TAG_NAME: ${{ steps.release.outputs.tag_name }}
RELEASE_NAME: ${{ steps.release.outputs.release_name }}
RELEASE_BODY: ${{ steps.release.outputs.release_body }}
PRERELEASE: ${{ steps.release.outputs.prerelease }}
DRAFT: ${{ steps.release.outputs.draft }}
TEST_MODE: ${{ steps.release.outputs.test_mode }}
ACCESS_TOKEN: ${{ secrets.GITCODE_ACCESS_TOKEN }}
run: |
payload=$(jq -n \
--arg tag_name "$TAG_NAME" \
--arg name "$RELEASE_NAME" \
--arg body "$RELEASE_BODY" \
--argjson prerelease "$PRERELEASE" \
--argjson draft "$DRAFT" \
'{
tag_name: $tag_name,
name: $name,
body: $body,
prerelease: $prerelease,
draft: $draft
}')
echo "Creating release with payload:"
echo "$payload" | jq .
if [ "$TEST_MODE" = "true" ]; then
echo "🧪 Test mode enabled - skipping release creation on GitCode"
echo "✅ Test mode: Would create release successfully on GitCode"
echo "created=true" >> $GITHUB_OUTPUT
else
response=$(curl -s -w "%{http_code}" \
-X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d "$payload" \
"$GITCODE_API_BASE/repos/$GITCODE_OWNER/$GITCODE_REPO/releases?access_token=$ACCESS_TOKEN")
http_code="${response: -3}"
response_body="${response%???}"
echo "Create Release Response Code: $http_code"
echo "Create Release Response: $response_body"
if [ "$http_code" = "201" ] || [ "$http_code" = "200" ]; then
echo "✅ Release created successfully on GitCode (HTTP $http_code)"
echo "created=true" >> $GITHUB_OUTPUT
else
echo "❌ Failed to create release on GitCode (HTTP $http_code)"
echo "Response: $response_body"
echo "created=false" >> $GITHUB_OUTPUT
exit 1
fi
fi
- name: Update existing release on GitCode
if: steps.check-release.outputs.exists == 'true'
id: update-release
env:
TAG_NAME: ${{ steps.release.outputs.tag_name }}
RELEASE_NAME: ${{ steps.release.outputs.release_name }}
RELEASE_BODY: ${{ steps.release.outputs.release_body }}
PRERELEASE: ${{ steps.release.outputs.prerelease }}
DRAFT: ${{ steps.release.outputs.draft }}
TEST_MODE: ${{ steps.release.outputs.test_mode }}
ACCESS_TOKEN: ${{ secrets.GITCODE_ACCESS_TOKEN }}
run: |
payload=$(jq -n \
--arg name "$RELEASE_NAME" \
--arg body "$RELEASE_BODY" \
--argjson prerelease "$PRERELEASE" \
--argjson draft "$DRAFT" \
'{
name: $name,
body: $body,
prerelease: $prerelease,
draft: $draft
}')
echo "Updating release with payload:"
echo "$payload" | jq .
if [ "$TEST_MODE" = "true" ]; then
echo "🧪 Test mode enabled - skipping release update on GitCode"
echo "✅ Test mode: Would update release successfully on GitCode"
echo "updated=true" >> $GITHUB_OUTPUT
else
response=$(curl -s -w "%{http_code}" \
-X PATCH \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d "$payload" \
"$GITCODE_API_BASE/repos/$GITCODE_OWNER/$GITCODE_REPO/releases/$TAG_NAME?access_token=$ACCESS_TOKEN")
http_code="${response: -3}"
response_body="${response%???}"
echo "Update Release Response Code: $http_code"
echo "Update Release Response: $response_body"
if [ "$http_code" = "200" ]; then
echo "✅ Release updated successfully on GitCode"
echo "updated=true" >> $GITHUB_OUTPUT
else
echo "❌ Failed to update release on GitCode"
echo "updated=false" >> $GITHUB_OUTPUT
exit 1
fi
fi
- name: Upload assets to GitCode release
if: steps.download-assets.outputs.has_assets == 'true'
run: |
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "🧪 Test mode enabled - skipping asset upload to GitCode"
echo "Would upload the following assets:"
IFS=',' read -ra ASSET_FILES <<< "${{ steps.download-assets.outputs.asset_files }}"
for asset_file in "${ASSET_FILES[@]}"; do
if [ -f "$asset_file" ]; then
echo " - $(basename "$asset_file")"
fi
done
echo "✅ Test mode: Would upload all assets successfully to GitCode"
else
echo "📦 Uploading assets to GitCode release using JavaScript uploader..."
# Make upload script executable
chmod +x ./scripts/upload-assets.js
# Convert comma-separated asset files to array for JavaScript uploader
IFS=',' read -ra ASSET_FILES <<< "${{ steps.download-assets.outputs.asset_files }}"
# Upload assets using the JavaScript uploader
node ./scripts/upload-assets.js \
--token "${{ secrets.GITCODE_ACCESS_TOKEN }}" \
--owner "$GITCODE_OWNER" \
--repo "$GITCODE_REPO" \
--tag "${{ steps.release.outputs.tag_name }}" \
--concurrency 3 \
--retry 3 \
"${ASSET_FILES[@]}"
upload_exit_code=$?
if [ $upload_exit_code -eq 0 ]; then
echo "✅ All assets uploaded successfully to GitCode"
else
echo "❌ Asset upload failed with exit code: $upload_exit_code"
exit 1
fi
fi
- name: Summary
run: |
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "## 🧪 Test Mode - Release Sync Summary" >> $GITHUB_STEP_SUMMARY
else
echo "## 🚀 Release Sync Summary" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Trigger:** ${{ github.event_name == 'workflow_dispatch' && '🔧 Manual' || '🚀 Automatic' }}" >> $GITHUB_STEP_SUMMARY
echo "**Release:** ${{ steps.release.outputs.tag_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Name:** ${{ steps.release.outputs.release_name }}" >> $GITHUB_STEP_SUMMARY
echo "**GitCode Repository:** $GITCODE_OWNER/$GITCODE_REPO" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "**Mode:** 🧪 Test Mode (Dry Run)" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.check-release.outputs.exists }}" = "true" ]; then
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "**Action:** Would update existing release ✅" >> $GITHUB_STEP_SUMMARY
else
echo "**Action:** Updated existing release ✅" >> $GITHUB_STEP_SUMMARY
fi
else
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "**Action:** Would create new release ✅" >> $GITHUB_STEP_SUMMARY
else
echo "**Action:** Created new release ✅" >> $GITHUB_STEP_SUMMARY
fi
fi
if [ "${{ steps.download-assets.outputs.has_assets }}" = "true" ]; then
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "**Assets:** Would upload to GitCode ✅" >> $GITHUB_STEP_SUMMARY
else
echo "**Assets:** Uploaded to GitCode ✅" >> $GITHUB_STEP_SUMMARY
fi
else
echo "**Assets:** No assets to upload" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.release.outputs.test_mode }}" = "true" ]; then
echo "Test completed successfully! 🧪 No actual changes were made to GitCode." >> $GITHUB_STEP_SUMMARY
else
echo "Release has been successfully synced to GitCode! 🎉" >> $GITHUB_STEP_SUMMARY
fi