Compare commits

..
1 Commits
Author SHA1 Message Date
linkong e6aa8c539b fix: verify Gitea release assets before publishing
ci / frontend (push) Waiting to run
ci / backend (push) Waiting to run
2026-11-22 14:23:20 +08:00
2 changed files with 220 additions and 100 deletions

No files matched your search

+23 -1
View File
@@ -176,7 +176,28 @@ Push to remote:
```
- If remote publication was not requested or remote access is restricted, report the commit and tag as complete and output the push commands for manual execution.
### Step 9 — Completion summary
### Step 9 — Publish and verify the Gitea Release
When remote publication is requested, verify the remote tag points to the
release commit. Check the tag's Gitea Actions run and its release job. A queued
run without an online runner does not publish anything.
If the job cannot start or finish, use the packaged client artifacts for the
exact tag. Reuse verified artifacts when available, or build with Bun from the
tag in a clean checkout. Use an asset directory that contains only this release.
```bash
python3 scripts/release_publish.py --tag vx.x.x --assets-dir client/release --dry-run
python3 scripts/release_publish.py --tag vx.x.x --assets-dir client/release --use-git-credential
```
Use `GITEA_TOKEN` instead of `--use-git-credential` when publishing with a
configured token. The publish script uploads to a draft and checks the
attachment names and sizes before publishing. Verify the published Release URL
and expected attachments through Gitea before reporting success. If Gitea is
unreachable, report the tag and package state and why publication is incomplete.
### Step 10 — Completion summary
```
✓ Version bumped: 0.76.18 → 0.76.19
@@ -187,6 +208,7 @@ Push to remote:
✓ commit: release: bump version to 0.76.19
✓ tag: v0.76.19
✓ Pushed to origin/dev (or reported pending manual push)
✓ Gitea Release and attachments verified (when publication was requested)
```
## Notes
+197 -99
View File
@@ -4,6 +4,7 @@
from __future__ import annotations
import argparse
import base64
import hashlib
import json
import os
@@ -18,6 +19,14 @@ from pathlib import Path
ASSET_SUFFIXES = (".zip", ".appimage", ".deb", ".exe", ".dmg")
class RejectRedirects(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl) -> None:
return None
API_OPENER = urllib.request.build_opener(RejectRedirects())
def run_git_origin_url() -> str:
try:
output = subprocess.check_output(
@@ -36,9 +45,10 @@ def parse_server_and_repo(remote_url: str) -> tuple[str, str]:
if not url:
return "", ""
https_match = re.match(r"^(https?://[^/]+)/(.+?)(?:\.git)?/?$", url)
if https_match:
return https_match.group(1).rstrip("/"), https_match.group(2).strip("/")
parsed = urllib.parse.urlsplit(url)
if parsed.scheme in ("https", "http") and parsed.hostname:
server = f"{parsed.scheme}://{parsed.netloc.rsplit('@', 1)[-1]}"
return server, parsed.path.strip("/").removesuffix(".git")
ssh_match = re.match(r"^(?:ssh://)?[^@]+@([^:/]+)[:/](.+?)(?:\.git)?/?$", url)
if ssh_match:
@@ -49,6 +59,34 @@ def parse_server_and_repo(remote_url: str) -> tuple[str, str]:
return "", ""
def git_credential_authorization(origin: str, server: str, repo: str) -> str:
"""Reuse a saved credential only for the matching HTTPS origin."""
if (
urllib.parse.urlsplit(origin).scheme != "https"
or (server, repo) != parse_server_and_repo(origin)
):
raise RuntimeError("--use-git-credential requires the target to match HTTPS origin")
try:
result = subprocess.run(
["git", "-c", "credential.interactive=false", "credential", "fill"],
input=f"url={origin}\n\n",
env={**os.environ, "GIT_TERMINAL_PROMPT": "0", "GCM_INTERACTIVE": "Never"},
text=True,
capture_output=True,
timeout=15,
check=True,
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as exc:
raise RuntimeError("No saved Git credential available for HTTPS origin") from exc
data = dict(line.split("=", 1) for line in result.stdout.splitlines() if "=" in line)
username = data.get("username")
password = data.get("password")
if not username or not password:
raise RuntimeError("Saved Git credential is incomplete")
encoded = base64.b64encode(f"{username}:{password}".encode()).decode()
return f"Basic {encoded}"
def extract_release_notes(changelog_path: Path, tag: str) -> str:
"""Extract the matching version section from docs/CHANGELOG.md."""
if not changelog_path.is_file():
@@ -128,11 +166,11 @@ def collect_release_assets(assets_dir: Path) -> list[tuple[Path, str]]:
def api_request(
method: str,
url: str,
token: str,
authorization: str,
payload: dict | None = None,
) -> tuple[int, dict | list | str | None]:
headers = {
"Authorization": f"token {token}",
"Authorization": authorization,
"Accept": "application/json",
}
data = None
@@ -142,7 +180,7 @@ def api_request(
request = urllib.request.Request(url, data=data, headers=headers, method=method)
try:
with urllib.request.urlopen(request, timeout=30) as response:
with API_OPENER.open(request, timeout=30) as response:
raw = response.read().decode("utf-8", errors="replace")
return response.status, json.loads(raw) if raw.strip() else None
except urllib.error.HTTPError as exc:
@@ -156,24 +194,34 @@ def api_request(
def upload_asset_with_curl(
upload_url: str,
token: str,
authorization: str,
file_path: Path,
) -> None:
"""Stream multipart file upload using curl to avoid buffering ~200MB assets in memory."""
proc = subprocess.run(
[
"curl",
"--disable",
"-sS",
"--connect-timeout",
"15",
"--speed-time",
"60",
"--speed-limit",
"1024",
"-w",
"\n%{http_code}",
"-X",
"POST",
"-H",
f"Authorization: token {token}",
"-F",
f"attachment=@{file_path}",
"--header",
"@-",
"--header",
"Content-Type: application/octet-stream",
"--data-binary",
f"@{file_path}",
upload_url,
],
input=f"Authorization: {authorization}\n",
text=True,
capture_output=True,
)
@@ -184,7 +232,6 @@ def upload_asset_with_curl(
lines = proc.stdout.splitlines()
status_str = lines[-1].strip() if lines else ""
body = "\n".join(lines[:-1]).strip()
try:
status_code = int(status_str)
except ValueError as exc:
@@ -195,10 +242,8 @@ def upload_asset_with_curl(
f"Upload rejected with HTTP 413 Payload Too Large for {file_path.name}. "
"Check Gitea app.ini [attachment] MAX_SIZE and Nginx/Ingress client_max_body_size (>= 300M)."
)
if status_code >= 400:
raise RuntimeError(
f"Upload failed for {file_path.name} (HTTP {status_code}): {body}"
)
if status_code != 201:
raise RuntimeError(f"Upload failed for {file_path.name} (HTTP {status_code})")
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
@@ -224,6 +269,11 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
default="",
help="Repository slug owner/name (defaults to GITHUB_REPOSITORY or origin remote)",
)
parser.add_argument(
"--use-git-credential",
action="store_true",
help="Use the saved Git credential for the matching HTTPS origin",
)
parser.add_argument(
"--dry-run",
action="store_true",
@@ -232,9 +282,125 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
return parser.parse_args(argv)
def find_draft(api_base: str, tag: str, authorization: str) -> dict | None:
page = 1
while True:
status, data = api_request(
"GET", f"{api_base}/releases?draft=true&limit=50&page={page}", authorization
)
if status != 200 or not isinstance(data, list):
raise RuntimeError(f"Could not list release drafts (HTTP {status})")
if not data:
return None
for release in data:
if release.get("tag_name") == tag and release.get("draft") is True:
return release
page += 1
def get_release(api_base: str, tag: str, notes: str, authorization: str) -> dict:
tag_url = f"{api_base}/releases/tags/{urllib.parse.quote(tag, safe='')}"
status, release = api_request("GET", tag_url, authorization)
if status == 200 and isinstance(release, dict) and release.get("id"):
return release
if status != 404:
raise RuntimeError(f"Could not read release {tag} (HTTP {status})")
draft = find_draft(api_base, tag, authorization)
if draft:
return draft
status, release = api_request(
"POST",
f"{api_base}/releases",
authorization,
{"tag_name": tag, "name": tag, "body": notes, "draft": True, "prerelease": False},
)
if (
status != 201
or not isinstance(release, dict)
or release.get("draft") is not True
or not release.get("id")
):
raise RuntimeError(f"Could not create draft release {tag} (HTTP {status})")
print(f"created draft release {tag} (id={release['id']})", flush=True)
return release
def list_assets(release_url: str, authorization: str) -> dict[str, dict]:
status, data = api_request("GET", f"{release_url}/assets", authorization)
if status != 200 or not isinstance(data, list):
raise RuntimeError(f"Could not list release assets (HTTP {status})")
if any(not isinstance(item, dict) or not isinstance(item.get("name"), str) for item in data):
raise RuntimeError("Gitea returned invalid release assets")
assets = {item["name"]: item for item in data}
if len(assets) != len(data):
raise RuntimeError("Gitea returned duplicate release asset names")
return assets
def verify_existing_asset(
asset: dict, local_path: Path, sha: str, server: str, authorization: str
) -> None:
"""Reuse an attachment only if its contents match the local artifact."""
name = local_path.name
if asset.get("size") != local_path.stat().st_size:
raise RuntimeError(f"Existing asset has a different size: {name}")
download = asset.get("browser_download_url")
if not isinstance(download, str) or not download:
raise RuntimeError(f"Missing download URL for {name}")
url = urllib.parse.urljoin(server + "/", download)
target = urllib.parse.urlsplit(url)
trusted = urllib.parse.urlsplit(server)
if (target.scheme, target.netloc) != (trusted.scheme, trusted.netloc):
raise RuntimeError(f"Unexpected download host for {name}")
request = urllib.request.Request(url, headers={"Authorization": authorization})
digest = hashlib.sha256()
try:
with API_OPENER.open(request, timeout=30) as response:
while chunk := response.read(1024 * 1024):
digest.update(chunk)
except urllib.error.URLError as exc:
raise RuntimeError(f"Could not verify existing asset: {name}") from exc
if digest.hexdigest() != sha:
raise RuntimeError(f"Existing asset has a different SHA-256: {name}")
def publish_release(
server: str, repo: str, tag: str, notes: str, assets: list[tuple[Path, str]], authorization: str
) -> None:
api_base = f"{server}/api/v1/repos/{repo}"
release = get_release(api_base, tag, notes, authorization)
release_url = f"{api_base}/releases/{release['id']}"
existing = list_assets(release_url, authorization)
for path, sha in assets:
if path.name in existing:
verify_existing_asset(existing[path.name], path, sha, server, authorization)
print(f"verified existing {path.name}", flush=True)
for path, _sha in assets:
if path.name in existing:
continue
name = urllib.parse.quote(path.name, safe="")
print(f"uploading {path.name}...", flush=True)
upload_asset_with_curl(f"{release_url}/assets?name={name}", authorization, path)
uploaded = list_assets(release_url, authorization)
for path, _sha in assets:
if uploaded.get(path.name, {}).get("size") != path.stat().st_size:
raise RuntimeError(f"Missing or incomplete attachment: {path.name}")
status, published = api_request(
"PATCH", release_url, authorization, {"name": tag, "body": notes, "draft": False}
)
if status != 200 or not isinstance(published, dict) or published.get("draft") is not False:
raise RuntimeError(f"Could not publish release {tag} (HTTP {status})")
print(f"published release {tag} with {len(assets)} asset(s)", flush=True)
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
derived_server, derived_repo = parse_server_and_repo(run_git_origin_url())
origin = run_git_origin_url()
derived_server, derived_repo = parse_server_and_repo(origin)
server = (args.server or os.environ.get("GITHUB_SERVER_URL") or derived_server).rstrip("/")
repo = (args.repo or os.environ.get("GITHUB_REPOSITORY") or derived_repo).strip("/")
@@ -246,6 +412,9 @@ def main(argv: list[str] | None = None) -> int:
notes = extract_release_notes(Path(args.changelog), args.tag)
assets_dir = Path(args.assets_dir)
assets = collect_release_assets(assets_dir)
if not assets:
print(f"error: no distributable assets found in {assets_dir}", file=sys.stderr)
return 1
if args.dry_run:
print(f"server: {server}")
@@ -258,89 +427,18 @@ def main(argv: list[str] | None = None) -> int:
print(f" - {path.name} ({size_mb:.2f} MiB) sha256={sha}")
return 0
token = os.environ.get("GITEA_TOKEN") or os.environ.get("GITHUB_TOKEN") or ""
if not token:
print(
"error: GITEA_TOKEN or GITHUB_TOKEN must be set to publish a Gitea Release",
file=sys.stderr,
)
try:
if args.use_git_credential:
authorization = git_credential_authorization(origin, server, repo)
else:
token = os.environ.get("GITEA_TOKEN") or os.environ.get("GITHUB_TOKEN")
if not token:
raise RuntimeError("Set GITEA_TOKEN or use the saved HTTPS Git credential")
authorization = f"token {token}"
publish_release(server, repo, args.tag, notes, assets, authorization)
except (RuntimeError, urllib.error.URLError, TimeoutError) as exc:
print(f"error: {exc}", file=sys.stderr)
return 1
api_base = f"{server}/api/v1/repos/{repo}"
encoded_tag = urllib.parse.quote(args.tag, safe="")
status, existing = api_request("GET", f"{api_base}/releases/tags/{encoded_tag}", token)
if status == 404:
create_status, release_obj = api_request(
"POST",
f"{api_base}/releases",
token,
payload={
"tag_name": args.tag,
"name": args.tag,
"body": notes,
"draft": False,
"prerelease": False,
},
)
if create_status >= 400 or not isinstance(release_obj, dict):
print(
f"error: failed to create Gitea Release {args.tag} (HTTP {create_status}): {release_obj}",
file=sys.stderr,
)
return 1
print(f"created release {args.tag} (id={release_obj.get('id')})")
elif status == 200 and isinstance(existing, dict):
release_id = existing["id"]
patch_status, release_obj = api_request(
"PATCH",
f"{api_base}/releases/{release_id}",
token,
payload={"name": args.tag, "body": notes},
)
if patch_status >= 400 or not isinstance(release_obj, dict):
print(
f"error: failed to update Gitea Release {args.tag} (HTTP {patch_status}): {release_obj}",
file=sys.stderr,
)
return 1
print(f"updated release {args.tag} (id={release_obj.get('id')})")
else:
print(
f"error: failed querying release {args.tag} (HTTP {status}): {existing}",
file=sys.stderr,
)
return 1
release_id = release_obj["id"]
list_status, asset_list = api_request("GET", f"{api_base}/releases/{release_id}/assets", token)
existing_assets: dict[str, int] = {}
if list_status == 200 and isinstance(asset_list, list):
for item in asset_list:
if isinstance(item, dict) and "name" in item and "id" in item:
existing_assets[str(item["name"])] = int(item["id"])
for asset_path, _sha in assets:
existing_id = existing_assets.get(asset_path.name)
if existing_id is not None:
del_status, del_body = api_request(
"DELETE",
f"{api_base}/releases/{release_id}/assets/{existing_id}",
token,
)
if del_status >= 400:
print(
f"error: failed removing old asset {asset_path.name} (HTTP {del_status}): {del_body}",
file=sys.stderr,
)
return 1
encoded_name = urllib.parse.quote(asset_path.name, safe="")
upload_url = f"{api_base}/releases/{release_id}/assets?name={encoded_name}"
print(f"uploading {asset_path.name}...")
upload_asset_with_curl(upload_url, token, asset_path)
print(f"published release {args.tag} with {len(assets)} asset(s)")
return 0