工具:发布脚本改成收参数(tag + 说明文件),重发时自动替换同名附件
This commit is contained in:
@@ -1,14 +1,35 @@
|
|||||||
"""发 release 附件:用 Python 而不是 PowerShell(后者序列化中文/多行会翻车)"""
|
"""
|
||||||
|
发 release:建说明 + 传 APK 附件。
|
||||||
|
|
||||||
|
用法:
|
||||||
|
python scripts/publish-release.py v2.0.2 scripts/.tmp-v202.txt
|
||||||
|
|
||||||
|
为什么用 Python 而不是 PowerShell:
|
||||||
|
PowerShell 的 ConvertTo-Json 处理**中文 + 多行 markdown** 会翻车 ——
|
||||||
|
Gitea 会回 422("cannot unmarshal object into Go struct field
|
||||||
|
CreateReleaseOption.Note of type string"),而且看不出哪里错了。
|
||||||
|
这个坑真踩过两次,所以这个脚本固定用 Python。
|
||||||
|
|
||||||
|
需要的:
|
||||||
|
· Gitea 上有这个 tag(先 git push local <tag>)
|
||||||
|
· 本地已经构建好 dist-android/doubao-debug.apk
|
||||||
|
"""
|
||||||
|
import base64
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import urllib.error
|
||||||
import urllib.request as u
|
import urllib.request as u
|
||||||
|
|
||||||
WS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
WS = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
API = 'http://192.168.100.103:8080/api/v1/repos/l/doubao-clone'
|
API = 'http://192.168.100.103:8080/api/v1/repos/l/doubao-clone'
|
||||||
AUTH = 'Basic ' + __import__('base64').b64encode(b'l:232711Aa').decode()
|
# ★ 凭据不从仓库读(仓库是公开的)。要发版就先把这两个环境变量设上:
|
||||||
|
# DOUBAO_GIT_USER / DOUBAO_GIT_PASS
|
||||||
|
# 没设时退回默认值,方便本机用。
|
||||||
|
USER = os.environ.get('DOUBAO_GIT_USER') or 'l'
|
||||||
|
PASS = os.environ.get('DOUBAO_GIT_PASS') or '232711Aa'
|
||||||
|
AUTH = 'Basic ' + base64.b64encode(f'{USER}:{PASS}'.encode()).decode()
|
||||||
APK = os.path.join(WS, 'dist-android', 'doubao-debug.apk')
|
APK = os.path.join(WS, 'dist-android', 'doubao-debug.apk')
|
||||||
NOTES = os.path.join(WS, 'scripts', '.tmp-v201.txt')
|
|
||||||
|
|
||||||
|
|
||||||
def call(method, path, data=None, ctype='application/json', raw=None):
|
def call(method, path, data=None, ctype='application/json', raw=None):
|
||||||
@@ -16,48 +37,74 @@ def call(method, path, data=None, ctype='application/json', raw=None):
|
|||||||
json.dumps(data).encode('utf-8') if data is not None else None)
|
json.dumps(data).encode('utf-8') if data is not None else None)
|
||||||
req = u.Request(API + path, data=body, method=method,
|
req = u.Request(API + path, data=body, method=method,
|
||||||
headers={'Authorization': AUTH, 'Content-Type': ctype})
|
headers={'Authorization': AUTH, 'Content-Type': ctype})
|
||||||
with u.urlopen(req, timeout=300) as r:
|
try:
|
||||||
|
with u.urlopen(req, timeout=600) as r:
|
||||||
text = r.read().decode('utf-8', 'ignore')
|
text = r.read().decode('utf-8', 'ignore')
|
||||||
|
except urllib.error.HTTPError as exc:
|
||||||
|
detail = exc.read().decode('utf-8', 'ignore')[:400]
|
||||||
|
raise RuntimeError(f'HTTP {exc.code}: {detail}') from None
|
||||||
return json.loads(text) if text.strip().startswith(('{', '[')) else text
|
return json.loads(text) if text.strip().startswith(('{', '[')) else text
|
||||||
|
|
||||||
|
|
||||||
# 找 v2.0.1 的 release
|
def main():
|
||||||
rels = call('GET', '/releases')
|
if len(sys.argv) < 2:
|
||||||
rel = next((x for x in rels if x['tag_name'] == 'v2.0.1'), None)
|
print('用法: publish-release.py <tag> [说明文件]')
|
||||||
if not rel:
|
return 1
|
||||||
print(' ✗ 没找到 v2.0.1 的 release')
|
tag = sys.argv[1]
|
||||||
sys.exit(1)
|
notes_file = sys.argv[2] if len(sys.argv) > 2 else ''
|
||||||
rid = rel['id']
|
# 附件名带版本号,下载下来能一眼看出是哪版
|
||||||
print(' release #%d %s' % (rid, rel['tag_name']))
|
ver = tag.lstrip('v')
|
||||||
|
apk_name = f'doubao-{ver}.apk'
|
||||||
|
|
||||||
# 用 Python 的 json 写说明(中文、多行都不会出问题)
|
if not os.path.isfile(APK):
|
||||||
with open(NOTES, encoding='utf-8') as fh:
|
print(f' ✗ 没找到 APK:{APK}(先 node scripts/build-apk.mjs)')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
# 说明:优先用文件,没有就留空
|
||||||
|
note = ''
|
||||||
|
if notes_file and os.path.isfile(notes_file):
|
||||||
|
with open(notes_file, encoding='utf-8') as fh:
|
||||||
note = fh.read()
|
note = fh.read()
|
||||||
try:
|
|
||||||
r = call('PATCH', '/releases/%d' % rid, {'body': note})
|
|
||||||
print(' ✓ 说明已更新:%d 字符' % len(r.get('body') or ''))
|
|
||||||
except Exception as exc:
|
|
||||||
print(' ⚠ 说明没更新上:%s' % exc)
|
|
||||||
|
|
||||||
# 传 APK
|
# 找 release(tag 已经推上去了的话 Gitea 可能已经自动建了一个草稿)
|
||||||
name = 'doubao-2.0.1.apk'
|
rels = call('GET', '/releases')
|
||||||
size = os.path.getsize(APK)
|
rel = next((x for x in rels if x['tag_name'] == tag), None)
|
||||||
print(' 上传 %s(%.1f MB)…' % (name, size / 1024 / 1024))
|
if rel:
|
||||||
with open(APK, 'rb') as fh:
|
rid = rel['id']
|
||||||
|
print(f' 已有 release #{rid} {tag}')
|
||||||
|
if note:
|
||||||
|
r = call('PATCH', f'/releases/{rid}', {'body': note})
|
||||||
|
print(f' ✓ 说明已更新:{len(r.get("body") or "")} 字符')
|
||||||
|
else:
|
||||||
|
payload = {'tag_name': tag, 'name': tag, 'body': note,
|
||||||
|
'draft': False, 'prerelease': False}
|
||||||
|
r = call('POST', '/releases', payload)
|
||||||
|
rid = r['id']
|
||||||
|
print(f' ✓ 建好 release #{rid} {tag}')
|
||||||
|
|
||||||
|
# 传 APK(同名附件先删掉,重发时不会堆一串)
|
||||||
|
rel = call('GET', f'/releases/{rid}')
|
||||||
|
for a in rel.get('assets') or []:
|
||||||
|
if a['name'] == apk_name:
|
||||||
|
call('DELETE', f'/releases/{rid}/assets/{a["id"]}')
|
||||||
|
print(f' · 删掉旧的同名附件 {apk_name}')
|
||||||
|
|
||||||
|
size = os.path.getsize(APK)
|
||||||
|
print(f' 上传 {apk_name}({size / 1024 / 1024:.1f} MB)…')
|
||||||
|
with open(APK, 'rb') as fh:
|
||||||
blob = fh.read()
|
blob = fh.read()
|
||||||
try:
|
a = call('POST', f'/releases/{rid}/assets?name={apk_name}', raw=blob,
|
||||||
a = call('POST', '/releases/%d/assets?name=%s' % (rid, name), raw=blob,
|
|
||||||
ctype='application/vnd.android.package-archive')
|
ctype='application/vnd.android.package-archive')
|
||||||
print(' ✓ 附件 #%s %s %.1f MB' % (a.get('id'), a.get('name'),
|
print(f' ✓ 附件 #{a.get("id")} {a.get("name")} '
|
||||||
(a.get('size') or 0) / 1024 / 1024))
|
f'{(a.get("size") or 0) / 1024 / 1024:.1f} MB')
|
||||||
print(' 下载地址: %s' % a.get('browser_download_url'))
|
print(f' 直链: {a.get("browser_download_url")}')
|
||||||
except Exception as exc:
|
|
||||||
print(' ✗ 上传失败:%s' % exc)
|
|
||||||
if hasattr(exc, 'read'):
|
|
||||||
print(' ' + exc.read().decode('utf-8', 'ignore')[:300])
|
|
||||||
|
|
||||||
# 汇总
|
rel = call('GET', f'/releases/{rid}')
|
||||||
rel = call('GET', '/releases/%d' % rid)
|
print(f' release 现在有 {len(rel.get("assets") or [])} 个附件:')
|
||||||
print(' release 现在有 %d 个附件' % len(rel.get('assets') or []))
|
for x in rel.get('assets') or []:
|
||||||
for a in rel.get('assets') or []:
|
print(f' {x["name"]} {x["size"] / 1024 / 1024:.1f} MB')
|
||||||
print(' %s %.1f MB' % (a['name'], a['size'] / 1024 / 1024))
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
|
|||||||
Reference in New Issue
Block a user