Skip to content

Commit ec5bdbd

Browse files
committed
feat(ci): add manual GitCode release sync
1 parent 9f7f67c commit ec5bdbd

1 file changed

Lines changed: 169 additions & 5 deletions

File tree

.github/workflows/sync-gitcode.yml

Lines changed: 169 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ on:
44
push:
55
branches:
66
- main
7-
8-
concurrency:
9-
group: sync-gitcode-main
10-
cancel-in-progress: true
7+
workflow_dispatch:
8+
inputs:
9+
release_tag:
10+
description: GitHub release tag to sync to GitCode
11+
required: true
12+
type: string
1113

1214
jobs:
1315
sync:
14-
if: github.repository == 'LiteLDev/LeviLauncher'
16+
if: github.event_name == 'push' && github.repository == 'LiteLDev/LeviLauncher'
1517
runs-on: ubuntu-latest
18+
concurrency:
19+
group: sync-gitcode-main
20+
cancel-in-progress: true
1621
permissions:
1722
contents: read
1823

@@ -51,3 +56,162 @@ jobs:
5156
run: |
5257
git remote add gitcode "$GITCODE_PUSH_URL"
5358
git push gitcode refs/heads/main:refs/heads/main
59+
60+
sync-release:
61+
if: github.event_name == 'workflow_dispatch' && github.repository == 'LiteLDev/LeviLauncher'
62+
runs-on: ubuntu-latest
63+
concurrency:
64+
group: sync-gitcode-release-${{ inputs.release_tag }}
65+
cancel-in-progress: false
66+
permissions:
67+
contents: read
68+
69+
steps:
70+
- uses: actions/checkout@v6
71+
with:
72+
fetch-depth: 0
73+
persist-credentials: false
74+
75+
- name: Validate release sync settings
76+
env:
77+
GITCODE_API_TOKEN: ${{ secrets.GITCODE_API_TOKEN }}
78+
GITCODE_PUSH_URL: ${{ secrets.GITCODE_PUSH_URL }}
79+
RELEASE_TAG: ${{ inputs.release_tag }}
80+
run: |
81+
set -euo pipefail
82+
83+
if [ -z "$RELEASE_TAG" ]; then
84+
echo "::error::Missing required workflow input release_tag."
85+
exit 1
86+
fi
87+
88+
if [ -z "$GITCODE_PUSH_URL" ]; then
89+
echo "::error::Missing required secret GITCODE_PUSH_URL."
90+
exit 1
91+
fi
92+
93+
if [ -z "$GITCODE_API_TOKEN" ]; then
94+
echo "::error::Missing required secret GITCODE_API_TOKEN."
95+
exit 1
96+
fi
97+
98+
- name: Resolve GitCode repository
99+
id: gitcode_repo
100+
env:
101+
GITCODE_PUSH_URL: ${{ secrets.GITCODE_PUSH_URL }}
102+
run: |
103+
set -euo pipefail
104+
105+
remote_without_scheme="${GITCODE_PUSH_URL#*://}"
106+
remote_without_auth="${remote_without_scheme#*@}"
107+
repo_path="${remote_without_auth#*/}"
108+
repo_path="${repo_path%.git}"
109+
110+
gitcode_owner="${repo_path%%/*}"
111+
gitcode_repo="${repo_path#*/}"
112+
113+
if [ -z "$gitcode_owner" ] || [ -z "$gitcode_repo" ] || [ "$gitcode_owner" = "$gitcode_repo" ]; then
114+
echo "::error::Unable to parse GitCode owner/repo from GITCODE_PUSH_URL."
115+
exit 1
116+
fi
117+
118+
echo "owner=$gitcode_owner" >> "$GITHUB_OUTPUT"
119+
echo "repo=$gitcode_repo" >> "$GITHUB_OUTPUT"
120+
echo "repository=$gitcode_owner/$gitcode_repo" >> "$GITHUB_OUTPUT"
121+
122+
- name: Fetch tags
123+
run: |
124+
git fetch --force --tags origin
125+
126+
- name: Push release tag to GitCode
127+
env:
128+
GITCODE_PUSH_URL: ${{ secrets.GITCODE_PUSH_URL }}
129+
RELEASE_TAG: ${{ inputs.release_tag }}
130+
run: |
131+
set -euo pipefail
132+
133+
if ! git rev-parse "refs/tags/$RELEASE_TAG" >/dev/null 2>&1; then
134+
echo "::error::Tag $RELEASE_TAG does not exist in the GitHub repository."
135+
exit 1
136+
fi
137+
138+
git remote add gitcode "$GITCODE_PUSH_URL"
139+
git push gitcode "refs/tags/$RELEASE_TAG:refs/tags/$RELEASE_TAG"
140+
141+
- name: Export GitHub release metadata
142+
env:
143+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144+
RELEASE_TAG: ${{ inputs.release_tag }}
145+
run: |
146+
set -euo pipefail
147+
148+
gh api "repos/${{ github.repository }}/releases/tags/${RELEASE_TAG}" > github-release.json
149+
150+
release_name="$(jq -r 'if (.name // "") == "" then .tag_name else .name end' github-release.json)"
151+
release_url="$(jq -r '.html_url' github-release.json)"
152+
release_body="$(jq -r '.body // ""' github-release.json)"
153+
target_commitish="$(jq -r 'if (.target_commitish // "") == "" then "main" else .target_commitish end' github-release.json)"
154+
prerelease="$(jq -r '.prerelease // false' github-release.json)"
155+
asset_links="$(jq -r '[.assets[]? | "- [" + .name + "](" + .browser_download_url + ")"] | join("\n")' github-release.json)"
156+
157+
if [ -n "$asset_links" ]; then
158+
sync_body="$(printf "%s\n\n---\n\nSynced from GitHub release: %s\n\nGitHub assets:\n%s\n" "$release_body" "$release_url" "$asset_links")"
159+
else
160+
sync_body="$(printf "%s\n\n---\n\nSynced from GitHub release: %s\n" "$release_body" "$release_url")"
161+
fi
162+
163+
jq -n \
164+
--arg tag "$RELEASE_TAG" \
165+
--arg name "$release_name" \
166+
--arg body "$sync_body" \
167+
--arg target_commitish "$target_commitish" \
168+
--argjson prerelease "$prerelease" \
169+
'{
170+
tag_name: $tag,
171+
name: $name,
172+
body: $body,
173+
target_commitish: $target_commitish,
174+
prerelease: $prerelease
175+
}' > gitcode-release.json
176+
177+
- name: Create or update GitCode release
178+
env:
179+
GITCODE_API_TOKEN: ${{ secrets.GITCODE_API_TOKEN }}
180+
GITCODE_OWNER: ${{ steps.gitcode_repo.outputs.owner }}
181+
GITCODE_REPO: ${{ steps.gitcode_repo.outputs.repo }}
182+
RELEASE_TAG: ${{ inputs.release_tag }}
183+
run: |
184+
set -euo pipefail
185+
186+
release_api_root="https://api.gitcode.com/api/v5/repos/${GITCODE_OWNER}/${GITCODE_REPO}/releases"
187+
release_api_url="${release_api_root}/${RELEASE_TAG}"
188+
189+
status_code="$(curl -sS -o gitcode-release-current.json -w "%{http_code}" \
190+
-H "PRIVATE-TOKEN: ${GITCODE_API_TOKEN}" \
191+
"$release_api_url")"
192+
193+
if [ "$status_code" = "200" ]; then
194+
echo "Updating GitCode release for ${RELEASE_TAG}"
195+
curl --fail-with-body -sS \
196+
-X PATCH \
197+
-H "PRIVATE-TOKEN: ${GITCODE_API_TOKEN}" \
198+
-H "Content-Type: application/json" \
199+
--data @gitcode-release.json \
200+
"$release_api_url" > gitcode-release-result.json
201+
elif [ "$status_code" = "404" ]; then
202+
echo "Creating GitCode release for ${RELEASE_TAG}"
203+
curl --fail-with-body -sS \
204+
-X POST \
205+
-H "PRIVATE-TOKEN: ${GITCODE_API_TOKEN}" \
206+
-H "Content-Type: application/json" \
207+
--data @gitcode-release.json \
208+
"$release_api_root" > gitcode-release-result.json
209+
else
210+
echo "::error::Unexpected response while checking GitCode release: HTTP ${status_code}"
211+
cat gitcode-release-current.json
212+
exit 1
213+
fi
214+
215+
- name: Print synced release summary
216+
run: |
217+
jq '{ tag_name, name, prerelease, html_url, url }' gitcode-release-result.json || cat gitcode-release-result.json

0 commit comments

Comments
 (0)