73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Upload artifact to Gitea Actions runtime, falling back to local save."""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
ARTIFACTS_DIR = os.path.join(os.path.dirname(HERE), "artifacts")
|
|
|
|
|
|
def _upload_via_runtime(artifact_name, payload, runtime_url, runtime_token):
|
|
"""Upload via Gitea Actions runtime API."""
|
|
url = f"{runtime_url}/_apis/artifact/build/{artifact_name}/{artifact_name}/upload"
|
|
headers = {
|
|
"Authorization": f"Bearer {runtime_token}",
|
|
"Content-Type": "application/octet-stream",
|
|
"Accept": "*/*",
|
|
}
|
|
req = urllib.request.Request(url, data=payload, headers=headers, method="PUT")
|
|
with urllib.request.urlopen(req) as resp:
|
|
return resp.status in (200, 201, 204)
|
|
|
|
|
|
def _save_locally(artifact_name, payload):
|
|
"""Fallback: save to local filesystem."""
|
|
os.makedirs(ARTIFACTS_DIR, exist_ok=True)
|
|
dest = os.path.join(ARTIFACTS_DIR, f"{artifact_name}.zip")
|
|
with open(dest, "wb") as f:
|
|
f.write(payload)
|
|
return dest
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print(f"Usage: {sys.argv[0]} <artifact_name> <file_path>")
|
|
sys.exit(1)
|
|
|
|
artifact_name = sys.argv[1]
|
|
file_path = sys.argv[2]
|
|
|
|
if not os.path.isfile(file_path):
|
|
print(f"[upload-artifact] ERROR: file not found: {file_path}")
|
|
sys.exit(1)
|
|
|
|
with open(file_path, "rb") as f:
|
|
payload = f.read()
|
|
|
|
runtime_url = os.environ.get("ACTIONS_RUNTIME_URL", "")
|
|
runtime_token = os.environ.get("ACTIONS_RUNTIME_TOKEN", "")
|
|
|
|
if runtime_url and runtime_token:
|
|
try:
|
|
ok = _upload_via_runtime(artifact_name, payload, runtime_url, runtime_token)
|
|
if ok:
|
|
print(f"[upload-artifact] Uploaded {file_path} ({len(payload)} bytes)")
|
|
return
|
|
except urllib.error.URLError as e:
|
|
print(f"[upload-artifact] Runtime upload failed (URLError): {e.reason}")
|
|
except Exception as e:
|
|
print(f"[upload-artifact] Runtime upload failed: {e}")
|
|
|
|
# Fallback
|
|
dest = _save_locally(artifact_name, payload)
|
|
print(f"[upload-artifact] Saved artifact to {dest} ({len(payload)} bytes)")
|
|
print("[upload-artifact] Done — 1 file(s) in '{}'.".format(artifact_name))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|