Files
tgit/app/runner/deployer.py
T
ElebobaandClaude Opus 5.5 95a72437ec
CI / ruff · mypy · pytest (pull_request) Failing after 3m40s
feat(pipeline): уведомления CI/CD — PipelineWatcher, CI загрузок на Gitea Actions
- app/pipeline: протокол CIProvider, клиент Gitea Actions (прогон по head_sha, упавший
  шаг, лог джоба, ретраи), PipelineWatcher — опрос только при незавершённых push,
  сообщение о push обновляется CI ⏳ → ✅/❌ → CD 🚀/⛔, автору — отдельные уведомления
- деплой сервисов — только после зелёного CI; старый push не откатывает сервис,
  который развернёт более новый; после рестарта бот продолжает с того же места
- CI_PROVIDER=none — прежнее поведение (деплой сразу после push)
- шаблон tgit-uploads: .gitea/workflows/check.yml; .gitea/ защищён от правок из бота
- миграция 0002: старые push помечены skipped, чтобы не слать им «таймаут CI»
- ADR-016, ARCHITECTURE §2.5/§5.2, README, DEPLOY, .env.example

Refs #11

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 00:24:08 +03:00

76 lines
2.9 KiB
Python

"""After a push passes CI: the services it touched are (re)deployed through tgit-runner (PipelineWatcher, #11).
A service = `services/<name>/`. Gone from the commit — removed on the host. A refused deploy keeps the old version
running (the runner switches only to a healthy one) and raises ⛔ for the whole team.
"""
import asyncio
import logging
from collections.abc import Awaitable, Callable, Iterable
from dataclasses import dataclass
from app.alerts.detectors import Condition
from app.alerts.service import AlertService
from app.db.models import AlertKind
from app.repo.git import GitError, UploadsRepo
from app.runner.client import SERVICE, RunnerClient, RunnerResult
log = logging.getLogger(__name__)
def changed_services(paths: Iterable[str]) -> list[str]:
found = set()
for path in paths:
parts = path.split("/")
if len(parts) >= 3 and parts[0] == "services" and SERVICE.match(parts[1]):
found.add(parts[1])
return sorted(found)
@dataclass(frozen=True)
class Outcome:
service: str
result: RunnerResult
removed: bool = False
Report = Callable[[Outcome], Awaitable[None]]
class Deployer:
def __init__(self, repo: UploadsRepo, runner: RunnerClient, alerts: AlertService | None = None) -> None:
self._repo = repo
self._runner = runner
self._alerts = alerts
self._lock = asyncio.Lock() # one deploy at a time: builds are heavy for the shared host
async def deploy(self, sha: str, services: list[str], report: Report) -> None:
async with self._lock:
for service in services:
try:
outcome = await self._one(sha, service)
except Exception: # one broken service must not stop the others
log.exception("deploy %s @ %s", service, sha[:7])
outcome = Outcome(service, RunnerResult(False, -1, "internal error", ""))
if not outcome.result.ok and self._alerts is not None:
await self._alerts.event(
Condition(
AlertKind.DEPLOY_REFUSED,
f"tgit-svc-{service}",
{"detail": outcome.result.summary, "sha": sha[:7], "log": outcome.result.tail},
)
)
try:
await report(outcome)
except Exception:
log.exception("deploy report for %s", service)
async def _one(self, sha: str, service: str) -> Outcome:
tree = f"{sha}:services/{service}"
try:
await self._repo.git("cat-file", "-e", tree)
except GitError:
return Outcome(service, await self._runner.run("remove", service), removed=True)
archive = await self._repo.git("archive", "--format=tar", tree)
return Outcome(service, await self._runner.deploy(service, sha, archive))