refactor(windows): publish Velopack updates through Gitea Releases
This commit is contained in:
1 parent
826745b407
commit
1ef8edc8bc
17 files changed
+519
-87
No files matched your search
@@ -1,24 +1,20 @@
|
||||
"""Promote packages from a successful main CI for the exact release commit."""
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import uuid
|
||||
import zipfile
|
||||
|
||||
from release_artifacts import app_versions, validate_artifact
|
||||
|
||||
SERVER = 'https://git.orangraph.com'
|
||||
REPO = os.environ['GITEA_REPOSITORY']
|
||||
TOKEN = os.environ['GITEA_TOKEN']
|
||||
BASE = f'{SERVER}/api/v1/repos/{REPO}'
|
||||
|
||||
|
||||
def request(url, data=None, method=None, content_type='application/json'):
|
||||
headers = {'Authorization': f'token {TOKEN}'}
|
||||
headers = {'Authorization': 'token ' + os.environ['GITEA_TOKEN']}
|
||||
if data is not None:
|
||||
headers['Content-Type'] = content_type
|
||||
if isinstance(data, dict):
|
||||
@@ -37,57 +33,56 @@ def download(asset):
|
||||
|
||||
|
||||
def main():
|
||||
repo = os.environ['GITEA_REPOSITORY']
|
||||
base = f'{SERVER}/api/v1/repos/{repo}'
|
||||
sha = os.environ['GITEA_SHA']
|
||||
tag = os.environ['RELEASE_TAG']
|
||||
version = re.search(r'^version: (\S+)', Path('pubspec.yaml').read_text(), re.M)[1].split('+')[0]
|
||||
version, windows_version = app_versions('pubspec.yaml')
|
||||
if tag != f'v{version}':
|
||||
raise ValueError('Release tag must match pubspec version')
|
||||
runs = request(f'{BASE}/actions/runs?limit=100')['workflow_runs']
|
||||
runs = request(f'{base}/actions/runs?limit=100')['workflow_runs']
|
||||
matches = [run for run in runs if run['head_sha'] == sha and run['path'].endswith('@refs/heads/main') and run['conclusion'] == 'success' and Path(run['path'].split('@')[0]).name == 'ci.yml']
|
||||
if not matches:
|
||||
raise ValueError('No successful main CI for this exact commit; run CI before releasing')
|
||||
run = max(matches, key=lambda item: item['id'])
|
||||
artifacts = request(f"{BASE}/actions/runs/{run['id']}/artifacts")['artifacts']
|
||||
artifacts = request(f"{base}/actions/runs/{run['id']}/artifacts")['artifacts']
|
||||
found = [item for item in artifacts if item['name'] == 'cutdex-release-packages' and not item.get('expired', False)]
|
||||
if len(found) != 1:
|
||||
raise ValueError('Expected one validated application artifact')
|
||||
data = request(f"{BASE}/actions/artifacts/{found[0]['id']}/zip")
|
||||
data = request(f"{base}/actions/artifacts/{found[0]['id']}/zip")
|
||||
with zipfile.ZipFile(io.BytesIO(data)) as archive:
|
||||
contents = {item.filename: archive.read(item) for item in archive.infolist() if not item.is_dir()}
|
||||
manifest = json.loads(contents['release-manifest.json'])
|
||||
if manifest['version'] != version:
|
||||
raise ValueError('Artifact version does not match release')
|
||||
expected = {f'cutdex-{version}-android.apk', f'cutdex-{version}-windows-x64.zip'}
|
||||
if set(manifest['files']) != expected:
|
||||
raise ValueError('Release requires both Android and Windows packages')
|
||||
for name, digest in manifest['files'].items():
|
||||
if hashlib.sha256(contents[name]).hexdigest() != digest:
|
||||
raise ValueError(f'Package checksum mismatch: {name}')
|
||||
releases = request(f'{BASE}/releases?limit=100')
|
||||
entries = [item for item in archive.infolist() if not item.is_dir()]
|
||||
if len({item.filename for item in entries}) != len(entries):
|
||||
raise ValueError('Duplicate artifact filenames')
|
||||
contents = {item.filename: archive.read(item) for item in entries}
|
||||
validate_artifact(contents, version, windows_version)
|
||||
releases = request(f'{base}/releases?limit=100')
|
||||
existing = [release for release in releases if release['tag_name'] == tag]
|
||||
if existing:
|
||||
release = existing[0]
|
||||
if not release['draft']:
|
||||
raise ValueError('Release already published; choose a new version')
|
||||
else:
|
||||
release = request(f'{BASE}/releases', {'tag_name': tag, 'target_commitish': sha, 'name': f'Cutdex {version}', 'draft': True, 'body': f'Windows x64 portable ZIP and Android ARM64/x86_64 APK. Android currently uses the project debug signing key. Processing workers and models are distributed separately. Verified CI run: {run["id"]}.'}, 'POST')
|
||||
assets = request(f"{BASE}/releases/{release['id']}/assets")
|
||||
release = request(f'{base}/releases', {'tag_name': tag, 'target_commitish': sha, 'name': f'Cutdex {version}', 'draft': True, 'prerelease': False, 'body': f'Windows x64 Velopack installer EXE, Portable ZIP, full update package and channel index; Android ARM64/x86_64 APK. Windows update version: {windows_version}. Android currently uses the project debug signing key. Processing workers and models are distributed separately. Verified CI run: {run["id"]}.'}, 'POST')
|
||||
assets = request(f"{base}/releases/{release['id']}/assets")
|
||||
for name, body in contents.items():
|
||||
if '/' in name or '\\' in name:
|
||||
raise ValueError('Unexpected artifact path')
|
||||
prior = [asset for asset in assets if asset['name'] == name]
|
||||
if prior:
|
||||
downloaded = download(prior[0])
|
||||
if downloaded != body:
|
||||
raise ValueError(f'Refusing to replace Release asset: {name}')
|
||||
continue
|
||||
boundary = uuid.uuid4().hex
|
||||
payload = (f'--{boundary}\r\nContent-Disposition: form-data; name="attachment"; filename="{name}"\r\nContent-Type: application/octet-stream\r\n\r\n'.encode() + body + f'\r\n--{boundary}--\r\n'.encode())
|
||||
asset = request(f"{BASE}/releases/{release['id']}/assets?name={urllib.parse.quote(name)}", payload, 'POST', f'multipart/form-data; boundary={boundary}')
|
||||
if download(asset) != body:
|
||||
raise ValueError(f'Remote checksum verification failed: {name}')
|
||||
request(f"{BASE}/releases/{release['id']}", {'draft': False}, 'PATCH')
|
||||
print(f'{SERVER}/{REPO}/releases/tag/{tag}')
|
||||
request(f"{base}/releases/{release['id']}/assets?name={urllib.parse.quote(name)}", payload, 'POST', f'multipart/form-data; boundary={boundary}')
|
||||
# Refuse publishing a resumed draft containing unrelated/stale attachments.
|
||||
final_assets = request(f"{base}/releases/{release['id']}/assets")
|
||||
if len(final_assets) != len(contents) or {asset['name'] for asset in final_assets} != set(contents):
|
||||
raise ValueError('Draft Release attachments differ from the validated artifact')
|
||||
for asset in final_assets:
|
||||
if download(asset) != contents[asset['name']]:
|
||||
raise ValueError('Final Release attachment verification failed: ' + asset['name'])
|
||||
request(f"{base}/releases/{release['id']}", {'draft': False, 'prerelease': False}, 'PATCH')
|
||||
print(f'{SERVER}/{repo}/releases/tag/{tag}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in new issue
Block a user