contiai / scripts /deploy.py
aliabozahra315's picture
Deploy c831dbe
f8e4cd5
Raw History Blame Contribute Delete
7.77 kB
"""Deploy everything committed on `main`, in one go.
python scripts/deploy.py # from the repository root
python scripts/deploy.py --dry-run # every check, nothing pushed
What it does, in order, stopping at the first thing that is not right:
1. the working tree is clean and `main` is checked out - only what is
committed is deployed;
2. `scripts/preflight.py` passes (ruff, the API tests, the web checks, no
leaked key);
3. no run is in progress on the live database (a deploy restarts the worker);
4. `main` is pushed to GitHub - the web app deploys itself from there;
5. the API: `hf-deploy` takes every commit since the one it last deployed
(its `apps/api/app/BUILD.txt`), records the new one in BUILD.txt, and is
pushed to the Space;
6. it waits for the Space's `/health` to report the new build.
The Space's remote URL carries a token; it is never printed.
"""
from __future__ import annotations
import argparse
import json
import re
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
HEALTH = "https://aliroya-contiai.hf.space/health"
SECRETS_FILE = ROOT.parent / "متغيرات-النشر.txt"
BUILD = "apps/api/app/BUILD.txt"
def sh(*cmd: str, check: bool = True, quiet: bool = False) -> str:
done = subprocess.run(list(cmd), cwd=ROOT, capture_output=True, text=True,
encoding="utf-8", errors="replace", shell=(sys.platform == "win32"))
out = (done.stdout or "") + (done.stderr or "")
# a remote URL may carry a token: never let one reach the screen
out = re.sub(r"https://[^\s@]+@", "https://***@", out)
if check and done.returncode != 0:
print(out)
raise SystemExit(f"✗ {' '.join(cmd[:3])} failed")
if not quiet and out.strip():
print(out.strip())
return out
def step(n: int, text: str) -> None:
print(f"\n── {n}. {text}")
def running_runs() -> int | None:
"""How many runs are queued or running on the live database; None when
it cannot be asked (no deployment variables on this machine)."""
if not SECRETS_FILE.exists():
return None
m = re.search(r"^DATABASE_URL=(\S+)", SECRETS_FILE.read_text(encoding="utf-8"), re.M)
if not m:
return None
url = m.group(1).replace("postgresql+asyncpg://", "postgresql://")
try:
import asyncio
import asyncpg
except ImportError:
return None
async def ask() -> int:
con = await asyncpg.connect(url, statement_cache_size=0)
try:
return int(await con.fetchval(
"select count(*) from runs where status::text in ('running','queued')"))
finally:
await con.close()
try:
return asyncio.run(ask())
except Exception as exc: # noqa: BLE001
print(f" (could not ask the database: {type(exc).__name__})")
return None
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--dry-run", action="store_true", help="check everything, push nothing")
ap.add_argument("--skip-checks", action="store_true", help="skip preflight (not advised)")
ap.add_argument("--force", action="store_true", help="deploy even with runs in progress")
args = ap.parse_args()
step(1, "the tree is clean and on main")
branch = sh("git", "rev-parse", "--abbrev-ref", "HEAD", quiet=True).strip()
if branch != "main":
raise SystemExit(f"✗ on «{branch}», not main")
dirty = [line for line in sh("git", "status", "--porcelain", quiet=True).splitlines()
if not line.startswith("??")]
if dirty:
raise SystemExit("✗ uncommitted changes:\n" + "\n".join(dirty[:20]))
head = sh("git", "rev-parse", "--short", "main", quiet=True).strip()
deployed = sh("git", "show", f"hf-deploy:{BUILD}", quiet=True).strip()
pending = sh("git", "log", "--oneline", f"{deployed}..main", quiet=True).strip()
print(f" main {head}; live build {deployed}")
if not pending:
print(" nothing new to deploy")
return 0
print(" to deploy:\n " + pending.replace("\n", "\n "))
if not args.skip_checks:
step(2, "preflight (what CI checks)")
code = subprocess.run([sys.executable, str(ROOT / "scripts" / "preflight.py")],
cwd=ROOT).returncode
if code != 0:
raise SystemExit("✗ preflight failed — nothing was pushed")
step(3, "no run in progress")
busy = running_runs()
if busy is None:
print(" (not checked: no deployment variables here)")
elif busy and not args.force:
raise SystemExit(f"✗ {busy} run(s) in progress — wait, or pass --force")
else:
print(f" {busy} in progress")
if args.dry_run:
print("\n✓ dry run: every check passed; nothing pushed")
return 0
step(4, "push main (the web app deploys from it)")
sh("git", "push", "origin", "main")
step(5, "the API: hf-deploy ← main, BUILD.txt, push to the Space")
sh("git", "checkout", "hf-deploy", quiet=True)
try:
# One commit at a time: a range stops at its first conflict and the
# commits after it were silently left behind (2026-10-20, a web file
# the Space never carried).
commits = sh("git", "rev-list", "--reverse", f"{deployed}..main",
quiet=True).split()
for commit in commits:
out = sh("git", "cherry-pick", "-n", commit, check=False)
conflicts = [line[3:] for line in sh("git", "status", "--porcelain", quiet=True)
.splitlines() if line[:2] in ("UU", "AA", "DU", "UD")]
for path in [c for c in conflicts if c.startswith("apps/web/")]:
# the Space does not build the web app: its own copy stays,
# and a web file it never carried stays out
if sh("git", "cat-file", "-e", f"HEAD:{path}", check=False, quiet=True) == "":
sh("git", "checkout", "--ours", "--", path, quiet=True)
sh("git", "add", "--", path, quiet=True)
else:
sh("git", "rm", "-q", "--cached", "--", path, check=False, quiet=True)
(ROOT / path).unlink(missing_ok=True)
left = [c for c in conflicts if not c.startswith("apps/web/")]
if left:
print(out)
sh("git", "cherry-pick", "--abort", check=False, quiet=True)
sh("git", "reset", "--hard", "HEAD", quiet=True)
raise SystemExit("✗ conflicts outside apps/web: " + ", ".join(left))
sh("git", "cherry-pick", "--quit", check=False, quiet=True)
(ROOT / BUILD).write_text(head + "\n", encoding="utf-8")
sh("git", "add", "-A", "--", "apps", ".github", "Dockerfile", quiet=True)
sh("git", "commit", "-q", "-m", f"Deploy {head}", quiet=True)
sh("git", "push", "-q", "space", "hf-deploy:main")
finally:
sh("git", "checkout", "-q", "main", check=False, quiet=True)
step(6, f"waiting for the Space to report build {head}")
for _ in range(90): # up to 30 minutes
try:
with urllib.request.urlopen(HEALTH, timeout=10) as r:
build = json.loads(r.read().decode()).get("build", "")
if build == head:
print(f"\n✓ live: build {head}")
return 0
except Exception: # noqa: BLE001 - restarting
pass
time.sleep(20)
print(".", end="", flush=True)
raise SystemExit(f"\n✗ the Space did not report {head} in 30 minutes — check its logs")
if __name__ == "__main__":
sys.exit(main())