fix(deploy): 使用 Gitea 单次人工确认发布
build / build (push) Successful in 1m46s

This commit is contained in:
姚章浩
2026-11-23 02:24:18 +08:00
parent 0b5de6ef7e
commit 07a0ee2644
4 changed files with 74 additions and 55 deletions
+55 -37
View File
@@ -2,8 +2,6 @@
"""Governance application CD. The installed host copy owns the release boundary."""
import argparse
import base64
import binascii
from contextlib import contextmanager
import copy
from datetime import datetime, timezone
@@ -40,7 +38,7 @@ SHA = re.compile('[0-9a-f]{40}')
HASH = re.compile('[0-9a-f]{64}')
IMAGE = re.compile('sha256:[0-9a-f]{64}')
UUID = re.compile('[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}')
NAMESPACE = 'knowledge-governance-release-v1'
GUARD_PREFIX = 'KG_CD_CONFIRM_V1 '
class CDError(RuntimeError):
@@ -108,13 +106,21 @@ def validate_request(request):
require(matches(SHA, request.get('sha')) and matches(UUID, request.get('task'))
and matches(HASH, request.get('bundle')), 'require full SHA, bundle SHA256 and task UUID')
require(matches(re.compile('[1-9][0-9]{0,14}'), request.get('run_id')), 'invalid Gitea run ID')
approval = request.get('approval')
require(isinstance(approval, str) and len(approval) <= 4096 and (action == 'writeback' or approval),
'missing or invalid release approval')
def approval_message(request):
return (f"{NAMESPACE}\n{REPO}\n{TARGET}\n{request['sha']}\n{request['bundle']}\n{request['task']}\n").encode()
def guard_proof(request, control_sha):
require(matches(SHA, control_sha), 'invalid workflow source SHA')
binding = {key: request[key] for key in ('run_id', 'action', 'confirm', 'sha', 'bundle', 'task')}
binding.update(repository=REPO, target=TARGET, workflow_sha=control_sha)
serialized = json.dumps(binding, sort_keys=True, separators=(',', ':')).encode()
return GUARD_PREFIX + hashlib.sha256(serialized).hexdigest()
def verify_guard_log(log, request, control_sha):
expected = guard_proof(request, control_sha)
pattern = re.compile(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z (' + GUARD_PREFIX + r'[0-9a-f]{64})')
proofs = [match.group(1) for line in log.decode().splitlines() if (match := pattern.fullmatch(line))]
require(proofs == [expected], 'Gitea guard dispatch proof missing or different')
def hash_file(path):
@@ -184,6 +190,7 @@ def release(host, request):
'writeback requires a verified deployment receipt')
require(all(existing[k] == request[k] for k in ('sha', 'task', 'bundle')), 'receipt binding mismatch')
manifest = verify_bundle(host.bundle_path(request), request, archives=False)
host.approve(request)
host.verify(manifest)
return writeback(host, request, existing)
@@ -370,28 +377,44 @@ class LiveHost:
return run(['git', '-C', str(self.git_path), *args], 'release source check', timeout=90)
def approve(self, request):
try:
signature = base64.b64decode(request['approval'], validate=True)
except (binascii.Error, ValueError) as exc:
raise CDError('approval signature is invalid') from exc
with tempfile.TemporaryDirectory(dir=DATA) as directory:
signature_file = Path(directory) / 'approval.sig'
signature_file.write_bytes(signature)
run(['ssh-keygen', '-Y', 'verify', '-f', str(DATA / 'allowed_signers'), '-I', 'release-owner',
'-n', NAMESPACE, '-s', str(signature_file)], 'release approval', input_data=approval_message(request))
def check_dispatch_run(self, request):
path = f'{FORGE}/api/v1/repos/{REPO}/actions/runs/{request["run_id"]}'
dispatch = json.loads(http(path))
require(str(dispatch.get('id')) == request['run_id'] and dispatch.get('event') == 'workflow_dispatch'
repo_url = f'{FORGE}/{REPO}'
run_id = request['run_id']
run_api = f'{FORGE}/api/v1/repos/{REPO}/actions/runs/{run_id}'
dispatch = json.loads(http(run_api))
control_sha = dispatch.get('head_sha')
trigger_actor = dispatch.get('trigger_actor') or dispatch.get('actor') or {}
require(str(dispatch.get('id')) == run_id and dispatch.get('event') == 'workflow_dispatch'
and dispatch.get('path') == 'deploy-production.yml@refs/heads/main'
and dispatch.get('head_branch') == 'main' and dispatch.get('status') in ('in_progress', 'running')
and (dispatch.get('actor') or {}).get('id') == 1,
'Gitea run is not an active deployment dispatched by the release owner')
and (dispatch.get('actor') or {}).get('id') == 1 and trigger_actor.get('id') == 1
and matches(SHA, control_sha), 'Gitea run is not an active deployment dispatched by the release owner')
# Repo secrets can be read by other workflows. Only a completed guard from
# the installed workflow and controller may authorize the exact inputs.
try:
self.git('merge-base', '--is-ancestor', control_sha, 'refs/remotes/origin/main')
except CDError:
self.fetch_source()
self.git('merge-base', '--is-ancestor', control_sha, 'refs/remotes/origin/main')
trusted = {'scripts/production_cd.py': Path(__file__).resolve(),
'.gitea/workflows/deploy-production.yml': DATA / 'deploy-production.yml'}
for name, local in trusted.items():
require(self.git('show', f'{control_sha}:{name}') == immutable_file(local).read_bytes(),
'Gitea approval workflow differs from installed control files')
jobs = json.loads(http(run_api + '/jobs')).get('jobs') or []
guards = [job for job in jobs if job.get('name') == 'guard' and job.get('conclusion') == 'success'
and str(job.get('run_id')) == run_id and job.get('head_sha') == control_sha
and matches(re.compile('[1-9][0-9]{0,14}'), str(job.get('id')))]
require(len(guards) == 1, 'Gitea approval guard has not succeeded')
guard_id = guards[0]['id']
verify_guard_log(http(f'{repo_url}/actions/runs/{run_id}/jobs/{guard_id}/logs'), request, control_sha)
def fetch_source(self):
self.git('fetch', '--no-tags', '--prune', f'{FORGE}/{REPO}.git', '+refs/heads/*:refs/remotes/origin/*')
def check_ci(self, request):
self.git('fetch', '--no-tags', f'{FORGE}/{REPO}.git', '+refs/heads/main:refs/remotes/origin/main')
self.git('merge-base', '--is-ancestor', request['sha'], 'refs/remotes/origin/main')
self.fetch_source()
refs = self.git('for-each-ref', '--format=%(refname)', '--contains', request['sha'], 'refs/remotes/origin/')
require(refs.strip(), 'candidate commit is not reachable from a repository branch')
contexts = immutable_file(DATA / 'required-contexts.txt').read_text().splitlines()
verify_contexts(json.loads(http(f'{FORGE}/api/v1/repos/{REPO}/commits/{request["sha"]}/status')),
[line.strip() for line in contexts if line.strip() and not line.lstrip().startswith('#')])
@@ -573,7 +596,7 @@ def event_request(event, env):
require(env.get('GITHUB_EVENT_NAME') == 'workflow_dispatch' and env.get('GITHUB_REF') == 'refs/heads/main'
and env.get('GITHUB_REPOSITORY') == REPO, 'manual dispatch from main required')
inputs = event.get('inputs') or {}
request = {name: inputs.get(name, '') for name in ('action', 'sha', 'task', 'bundle', 'confirm', 'approval')}
request = {name: inputs.get(name, '') for name in ('action', 'sha', 'task', 'bundle', 'confirm')}
request['run_id'] = env.get('GITHUB_RUN_ID', '')
validate_request(request)
return request
@@ -636,15 +659,13 @@ def attest(args):
def cli():
parser = argparse.ArgumentParser(description=__doc__)
commands = parser.add_subparsers(dest='command', required=True)
for command in ('guard', 'send', 'approval-message', 'serve', 'pack', 'attest'):
for command in ('guard', 'send', 'serve', 'pack', 'attest'):
sub = commands.add_parser(command)
if command in ('guard', 'send'):
sub.add_argument('--event', type=Path, required=True)
if command in ('approval-message', 'pack', 'attest'):
if command in ('pack', 'attest'):
sub.add_argument('--sha', required=True)
sub.add_argument('--task', required=True)
if command == 'approval-message':
sub.add_argument('--bundle', required=True)
if command in ('pack', 'attest'):
for role in ROLES:
sub.add_argument('--' + role, required=True)
@@ -657,12 +678,9 @@ def cli():
if args.command in ('guard', 'send'):
request = event_request(read_json(args.event), os.environ)
if args.command == 'guard':
print('dispatch parameters verified')
print(guard_proof(request, os.environ.get('GITHUB_SHA')))
else:
send(request)
elif args.command == 'approval-message':
require(matches(SHA, args.sha) and matches(UUID, args.task) and matches(HASH, args.bundle), 'invalid approval binding')
sys.stdout.buffer.write(approval_message(vars(args)))
elif args.command == 'pack':
pack(args)
elif args.command == 'attest':
@@ -677,7 +695,6 @@ def cli():
for item in (DATA / 'receipts').glob('*.json'):
if read_json(item)['state'] in ('cutover_started', 'rollback_failed'):
raise CDError('previous deployment outcome requires operator reconciliation')
host.check_dispatch_run(request)
result = release(host, request)
print(json.dumps({k: result[k] for k in ('state', 'sha', 'task', 'bundle', 'hifox')}, ensure_ascii=False))
@@ -705,7 +722,8 @@ def send(request):
if result.returncode:
error = result.stderr.decode(errors='replace')
for marker in ('DEPLOYED_HIFOX_PENDING', 'rollback_failed:', 'rolled_back:', 'failed_before_cutover:',
'target preflight rejected', 'deployment lock busy'):
'target preflight rejected', 'deployment lock busy', 'writeback requires a verified deployment receipt',
'Gitea approval guard has not succeeded', 'Gitea guard dispatch proof missing or different'):
if marker in error:
raise CDError(error.strip().splitlines()[-1])
raise CDError('target rejected request or outcome unknown; inspect durable receipt')
+15 -3
View File
@@ -20,7 +20,7 @@ class ProductionCD(unittest.TestCase):
self.addCleanup(temp.cleanup)
self.root = Path(temp.name)
self.request = dict(sha=SHA, task=TASK, bundle='b' * 64, confirm='deploy-production',
action='deploy', approval='signed', run_id='42')
action='deploy', run_id='42')
self.manifest = dict(schema=1, target=cd.TARGET, sha=SHA, task=TASK, migrations=[1, 2], images={})
for role, image in IMAGES.items():
archive = self.root / (role + '.tar')
@@ -94,14 +94,14 @@ class ProductionCD(unittest.TestCase):
with self.assertRaisesRegex(cd.CDError, 'DEPLOYED_HIFOX_PENDING'):
self.execute()
self.assertEqual(cd.read_json(self.host.receipt_path)['state'], 'deployed_hifox_pending')
self.request.update(action='writeback', confirm='retry-writeback', approval='')
self.request.update(action='writeback', confirm='retry-writeback')
self.assertEqual(self.execute()['state'], 'published')
self.host.switch.assert_called_once()
self.host.load.assert_called_once()
self.assertEqual(self.host.verify.call_count, 2)
def test_writeback_without_receipt_refuses(self):
self.request.update(action='writeback', confirm='retry-writeback', approval='')
self.request.update(action='writeback', confirm='retry-writeback')
with self.assertRaisesRegex(cd.CDError, 'receipt'):
self.execute()
self.assert_no_deploy()
@@ -122,6 +122,18 @@ class ProductionCD(unittest.TestCase):
self.execute()
self.host.switch.assert_called_once()
def test_gitea_guard_log_binds_the_exact_dispatch(self):
trusted = 'f' * 40
proof = cd.guard_proof(self.request, trusted)
log = f'2026-11-22T17:31:54.013Z {proof}\n'.encode()
cd.verify_guard_log(log, self.request, trusted)
for name, altered in (('sha', 'b' * 40), ('bundle', 'c' * 64), ('run_id', '43'),
('task', 'd5abdf29-d0b5-4992-8073-0cb8fffd84ee2'), ('confirm', 'retry-writeback')):
with self.subTest(name=name), self.assertRaisesRegex(cd.CDError, 'dispatch proof'):
cd.verify_guard_log(log, {**self.request, name: altered}, trusted)
with self.assertRaisesRegex(cd.CDError, 'dispatch proof'):
cd.verify_guard_log(log + log, self.request, trusted)
def test_target_lock_is_exclusive(self):
path = self.root / 'lock'
with cd.deploy_lock(path):