Files
gandalf/version.py
jaredandClaude Opus 5.5 48daed3682 CI: one pipeline that gates the deploy on every check and waits for prod to run it
gandalf#55, part 1 (no server changes needed).

- .gitea/workflows/ci.yml replaces lint.yml, test.yml and security.yml.
  Jobs: Lint (Python) with the dash check, Lint (JS), Test, Security,
  Scan (gitleaks over the full history, actionlint, shellcheck), Deploy
  and Notify. Deploy needs every check, so failing tests or a bandit or
  gitleaks finding no longer reach production.
- Pinned: actions/checkout to a full SHA, job images to digests (Python
  3.13 on Debian 13 like CT 157, Node 22 for eslint), Python tools in the
  new requirements-dev.txt, scanner downloads by SHA-256. No setup-python
  (the runner's shared tool cache broke Pulse twice).
- Builds run on pull requests and on pushes to main only, plus a weekly
  run on main; concurrency queues deploys and never cancels one.
- Deploy signs {"ref", "sha", "ts"} (unique per request), then waits up to
  5 minutes until /health reports that commit for both processes, and only
  then tags deploy-<date>-<run>.
- /health reports `sha` (version.py reads .git at startup, no subprocess)
  and `monitor_sha` (the monitor writes it every cycle).

Part 2 (server side): the pinned deploy script that refuses stale or
replayed requests, deploys exactly that sha and rolls back.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-28 23:43:21 -05:00

40 lines
1.4 KiB
Python

"""The git commit this process is running, read once at startup (gandalf#55).
/health reports it so CI can wait until production runs the commit it tested. Read straight
from .git (no subprocess): a detached HEAD holds the sha itself; a branch HEAD points at a
loose ref or a line in packed-refs. Anything unreadable gives '' rather than an error.
"""
import os
import re
_SHA = re.compile(r'^[0-9a-f]{40}$')
def running_sha(root: str = '') -> str:
"""The 40-character commit sha checked out in *root* (default: this file's folder), or ''."""
git = os.path.join(root or os.path.dirname(os.path.abspath(__file__)), '.git')
try:
with open(os.path.join(git, 'HEAD')) as f:
head = f.read().strip()
if _SHA.match(head):
return head
if not head.startswith('ref: '):
return ''
ref = head[5:].strip()
loose = os.path.join(git, *ref.split('/'))
if os.path.isfile(loose):
with open(loose) as f:
sha = f.read().strip()
return sha if _SHA.match(sha) else ''
with open(os.path.join(git, 'packed-refs')) as f:
for line in f:
parts = line.split()
if len(parts) == 2 and parts[1] == ref and _SHA.match(parts[0]):
return parts[0]
except OSError:
pass
return ''
SHA = running_sha()