ci: route exact limited timestamp release branch to Stage
Deploy Canina / deploy (push) Successful in 2m51s
Deploy Canina / deploy (push) Successful in 2m51s
This commit is contained in:
1 parent
c97ea01ba9
commit
dc34c57a0c
4 files changed
+131
-3
No files matched your search
@@ -98,8 +98,20 @@ done
|
||||
[[ $lock_acquired == true ]] || { printf 'Stage host lock not confirmed\n' >&2; exit 1; }
|
||||
release_stage() {
|
||||
cd "$STAGE_DIR"
|
||||
timeout --foreground --kill-after=2s 60 git fetch origin develop
|
||||
[[ $(git rev-parse origin/develop) == "$DEPLOY_SHA" ]] || exit 2
|
||||
# Stage source ref gate: exact allowlist, never infer production from a short name.
|
||||
case "${STAGE_SOURCE_REF:-refs/heads/develop}" in
|
||||
refs/heads/develop)
|
||||
timeout --foreground --kill-after=2s 60 git fetch origin develop
|
||||
[[ $(git rev-parse origin/develop) == "$DEPLOY_SHA" ]] || exit 2
|
||||
;;
|
||||
refs/heads/release/blog-timestamp-limited-20261030)
|
||||
timeout --foreground --kill-after=2s 60 git fetch origin \
|
||||
refs/heads/release/blog-timestamp-limited-20261030:refs/remotes/origin/release/blog-timestamp-limited-20261030
|
||||
[[ $(git rev-parse refs/remotes/origin/release/blog-timestamp-limited-20261030) == "$DEPLOY_SHA" ]] || exit 2
|
||||
;;
|
||||
*) printf 'Unsupported Stage source ref\n' >&2; exit 2 ;;
|
||||
esac
|
||||
# End Stage source ref gate
|
||||
# Never reset/clean the operational checkout or overwrite its generated configs.
|
||||
# Every attempt has its own directory: retain incomplete and active releases.
|
||||
RELEASE_ROOT=/opt/services/canina/cache-releases
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Execute the tracked workflow route and Stage ref guard without network/daemon."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
RELEASE = 'refs/heads/release/blog-timestamp-limited-20261030'
|
||||
|
||||
|
||||
def workflow_run():
|
||||
source = (ROOT / '.gitea/workflows/deploy.yml').read_text()
|
||||
return '\n'.join(
|
||||
line[10:] for line in source.split(' run: |\n', 1)[1].splitlines())
|
||||
|
||||
|
||||
class LimitedStageRouteTests(unittest.TestCase):
|
||||
def route(self, ref, sha='a' * 40):
|
||||
run = workflow_run()
|
||||
# Execute the actual fail-closed preamble before its first external action.
|
||||
route = run.split('# 1.', 1)[0]
|
||||
result = subprocess.run(['bash', '-c', route + '\nprintf "%s|%s|%s" "$BRANCH" "$STAGE_SOURCE_REF" "$DEPLOY_SHA"'],
|
||||
env=dict(os.environ, DEPLOY_REF=ref, DEPLOY_SHA=sha), text=True, capture_output=True)
|
||||
return result
|
||||
|
||||
def test_exact_push_allowlist_and_sha_binding(self):
|
||||
source = (ROOT / '.gitea/workflows/deploy.yml').read_text()
|
||||
branches = re.findall(r'^ - (.+)$', source.split('\njobs:', 1)[0], re.M)
|
||||
self.assertEqual(branches, ['main', 'develop', RELEASE.removeprefix('refs/heads/')])
|
||||
self.assertIn('DEPLOY_REF: ${{ github.ref }}', source)
|
||||
self.assertIn('DEPLOY_SHA: ${{ github.sha }}', source)
|
||||
self.assertIn('./deploy.sh "${BRANCH}"', source)
|
||||
self.assertNotIn('github.ref_name', source)
|
||||
|
||||
def test_routes(self):
|
||||
for ref, branch, stage_ref in [('refs/heads/main', 'main', ''), ('refs/heads/develop', 'develop', 'refs/heads/develop'), (RELEASE, 'develop', RELEASE)]:
|
||||
with self.subTest(ref=ref):
|
||||
result = self.route(ref)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(result.stdout, f'{branch}|{stage_ref}|'+ 'a'*40)
|
||||
|
||||
def test_rejects_similar_tags_unknown_refs_and_bad_sha(self):
|
||||
for ref in ['main', 'refs/tags/main', 'refs/tags/'+RELEASE.split('/')[-1], RELEASE+'-other', 'refs/heads/release/other', 'refs/heads/main;touch bad']:
|
||||
self.assertNotEqual(self.route(ref).returncode, 0, ref)
|
||||
for sha in ['', 'abc', 'a'*40+';touch bad', 'A'*40]:
|
||||
self.assertNotEqual(self.route(RELEASE, sha).returncode, 0, sha)
|
||||
|
||||
def test_stage_exact_source_guard(self):
|
||||
source = (ROOT / 'scripts/release-cache-stage.sh').read_text()
|
||||
guard = source.split('# Stage source ref gate', 1)[1].split('# End Stage source ref gate', 1)[0]
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
git = Path(d)/'git'
|
||||
git.write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$CALLS"\nif [ "$1" = rev-parse ]; then printf "%s\\n" "$REMOTE_SHA"; fi\n')
|
||||
git.chmod(0o755)
|
||||
calls = Path(d)/'calls'
|
||||
for ref, remote, expected in [('refs/heads/develop', 'a'*40, 0), (RELEASE, 'a'*40, 0), (RELEASE, 'b'*40, 2), ('refs/heads/main', 'a'*40, 2), (RELEASE+'x', 'a'*40, 2)]:
|
||||
calls.unlink(missing_ok=True)
|
||||
result = subprocess.run(['bash', '-c', 'set -euo pipefail\n'+guard], env=dict(os.environ, PATH=d+':'+os.environ['PATH'], CALLS=str(calls), REMOTE_SHA=remote, STAGE_SOURCE_REF=ref, DEPLOY_SHA='a'*40), capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, expected, result.stderr)
|
||||
if ref not in ('refs/heads/develop', RELEASE):
|
||||
self.assertFalse(calls.exists(), 'invalid ref reached git')
|
||||
elif ref == RELEASE:
|
||||
self.assertIn('fetch origin '+RELEASE+':refs/remotes/origin/release/blog-timestamp-limited-20261030', calls.read_text())
|
||||
|
||||
def test_production_dispatch_is_not_selected_for_release(self):
|
||||
deploy = (ROOT / 'scripts/deploy.sh').read_text()
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
root = Path(d)
|
||||
(root/'deploy.sh').write_text(deploy)
|
||||
for target in ('stage', 'prod'):
|
||||
(root/f'release-cache-{target}.sh').write_text(f'printf "{target}|%s" "$DEPLOY_SHA"\n')
|
||||
selected = self.route(RELEASE).stdout.split('|')[0]
|
||||
result = subprocess.run(['bash', str(root/'deploy.sh'), selected], env=dict(os.environ, DEPLOY_SHA='a'*40), capture_output=True, text=True, check=True)
|
||||
self.assertEqual(result.stdout, 'stage|'+'a'*40)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user