From dc34c57a0cd41db41d6277df08ffd2d4e7788e12 Mon Sep 17 00:00:00 2001 From: Canina Hermes Date: Fri, 30 Oct 2026 13:28:55 +0000 Subject: [PATCH] ci: route exact limited timestamp release branch to Stage --- .gitea/workflows/deploy.yml | 16 +++- scripts/release-cache-stage.sh | 16 +++- scripts/tests/test_limited_stage_route.py | 80 +++++++++++++++++++ .../limited-stage-routing-evidence.md | 22 +++++ 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 scripts/tests/test_limited_stage_route.py create mode 100644 specs/blog-timestamp-metadata/limited-stage-routing-evidence.md diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 8b8f1e7e..91d91bb7 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -5,6 +5,7 @@ on: branches: - main - develop + - release/blog-timestamp-limited-20261030 jobs: deploy: @@ -12,10 +13,23 @@ jobs: steps: - name: Run deploy.sh from within the scripts folder env: - BRANCH: ${{ github.ref_name }} + DEPLOY_REF: ${{ github.ref }} DEPLOY_SHA: ${{ github.sha }} GITHUB_ACTIONS: 'true' run: | + set -eu + case "${DEPLOY_REF}" in + refs/heads/main) BRANCH=main; STAGE_SOURCE_REF= ;; + refs/heads/develop|refs/heads/release/blog-timestamp-limited-20261030) + BRANCH=develop; STAGE_SOURCE_REF="$DEPLOY_REF" ;; + *) printf 'Unsupported deployment ref\n' >&2; exit 2 ;; + esac + # Bind downloads and both release dispatchers to the exact event commit. + case "$DEPLOY_SHA" in + *[!0-9a-f]*|'') exit 2 ;; + esac + [ "${#DEPLOY_SHA}" -eq 40 ] || exit 2 + export BRANCH STAGE_SOURCE_REF # 1. رفع مشکل DNS و غیرفعال کردن پروکسی برای دامنه محلی echo "87.248.133.138 git.parsaaghayi.ir" >> /etc/hosts export http_proxy="" diff --git a/scripts/release-cache-stage.sh b/scripts/release-cache-stage.sh index e37a46b9..575c50dd 100644 --- a/scripts/release-cache-stage.sh +++ b/scripts/release-cache-stage.sh @@ -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 diff --git a/scripts/tests/test_limited_stage_route.py b/scripts/tests/test_limited_stage_route.py new file mode 100644 index 00000000..08d7d94f --- /dev/null +++ b/scripts/tests/test_limited_stage_route.py @@ -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() diff --git a/specs/blog-timestamp-metadata/limited-stage-routing-evidence.md b/specs/blog-timestamp-metadata/limited-stage-routing-evidence.md new file mode 100644 index 00000000..c9b69b1a --- /dev/null +++ b/specs/blog-timestamp-metadata/limited-stage-routing-evidence.md @@ -0,0 +1,22 @@ +# Limited release Stage routing — 2026-10-30 + +Scope: preserve mixed `develop` and its historical evidence. Add only the explicit `release/blog-timestamp-limited-20261030` push ref to the official deployment workflow. No push or deployment was performed by the preparing worker. + +## Source contract + +- Workflow binds `DEPLOY_REF` to full event `github.ref` and leaves `DEPLOY_SHA` bound to `github.sha`. Unknown refs and malformed SHAs fail before downloads. +- Only `refs/heads/main` selects `deploy.sh main`; develop and the exact dedicated release ref select `deploy.sh develop` with an explicit Stage source ref. +- `deploy.sh develop` execs `release-cache-stage.sh`. That script previously fetched only develop and required `origin/develop == DEPLOY_SHA`; workflow routing alone was therefore insufficient. +- Stage now fetches the exact allowlisted dedicated source into its own remote-tracking ref and requires its SHA to equal `DEPLOY_SHA` before archive/build. The develop path remains unchanged. No force refspec or operational checkout reset is introduced. +- `deploy.sh` and `release-cache-prod.sh` are unchanged. Production still fetches main and requires `origin/main == DEPLOY_SHA`. Both official release scripts are code-only, building/recreating only backend/frontend through the existing lock/runtime/provenance checks; no migrations added. + +## Actual local execution + +- New source-executing regression suite was run RED before implementation: missing allowlisted push ref, missing Stage source-ref guard, and missing fail-closed full-ref routing failed. +- `python -m unittest scripts.tests.test_limited_stage_route scripts.tests.test_cache_stage_release scripts.tests.test_cache_release_safety -v`: **13 tests passed**, 17.845s. Includes actual embedded workflow shell preamble execution, exact/near-match/tag/injection rejection, invalid SHA rejection, Stage source-ref guard with a fake Git transport, mismatched remote SHA rejection, and actual deploy dispatcher execution with isolated release doubles. +- `bash -n scripts/deploy.sh scripts/release-cache-stage.sh scripts/release-cache-prod.sh`: passed. +- `git diff --check`: passed. +- Additional `test_production_code_only_ci` run: 2 passed, 1 failed because the baseline production script already has `--repair-empty` while an old test forbids it. Reproduced that exact assertion using `git show 00ba4360:scripts/release-cache-prod.sh`; production source is byte-identical to baseline. No production script/test was changed to hide that existing failure. +- Local Graphify inventory was queried: deploy.sh nodes exist but their old locations describe an obsolete script and no current release-cache-stage/workflow nodes were present. Actual source above is authoritative; no claim of a fresh graph or graph-derived release acceptance. + +Existing timestamp test files and evidence were preserved byte-for-byte. This evidence is local routing verification only, not official Stage acceptance or Production acceptance. Parent remains sole push owner and must verify CI/run SHA, immutable images, and live timestamp behavior before promoting the identical accepted SHA to main without a merge commit.