From 3fb1e417c63c8c8582aa44f9f5e77db7dbe159f6 Mon Sep 17 00:00:00 2001 From: Parsa Production Ops Date: Mon, 26 Oct 2026 05:52:27 +0000 Subject: [PATCH] fix(ops): reconcile CI backend wrapper and gate locked dependency image before swap --- .gitea/workflows/deploy.yml | 24 ++++++++++++++++-- Dockerfile | 1 + ops/backend.compose.yml | 7 ++++++ ops/deploy-backend.sh | 28 +++++++++++++++++++++ tests/deploy_fixture.py | 50 +++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 ops/backend.compose.yml create mode 100644 ops/deploy-backend.sh create mode 100644 tests/deploy_fixture.py diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index b52c682..9a07e2d 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -24,5 +24,25 @@ jobs: echo "$SSH_KEY" > ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519 ssh-keyscan -p 2234 -H 172.17.0.1 >> ~/.ssh/known_hosts - - name: Deploy - run: ssh -p 2234 devops@172.17.0.1 'bash /opt/services/parsa/backend/deploy.sh' + - name: Reconcile reviewed deployment wrapper and deploy + env: + RELEASE_SHA: ${{ gitea.sha }} + run: | + case "$RELEASE_SHA" in ''|*[!0-9a-f]*) exit 1;; esac + test "${#RELEASE_SHA}" = 40 + ssh -p 2234 devops@172.17.0.1 "bash -s -- $RELEASE_SHA" <<'REMOTE' + set -euo pipefail + expected=$1 + cd /opt/services/parsa/backend/repo + test -z "$(git status --porcelain --untracked-files=normal)" + export GIT_SSH_COMMAND='ssh -i /home/devops/.ssh/deploy_key -o StrictHostKeyChecking=no' + git fetch origin main + test "$(git rev-parse origin/main)" = "$expected" + git merge --ff-only "$expected" + test "$(git rev-parse HEAD)" = "$expected" + python3 tests/deploy_fixture.py + bash -n ops/deploy-backend.sh + # CI owns the host wrapper; never execute the old untracked implementation. + sudo install -m 0755 ops/deploy-backend.sh /opt/services/parsa/backend/deploy.sh + bash /opt/services/parsa/backend/deploy.sh "$expected" + REMOTE diff --git a/Dockerfile b/Dockerfile index e948bd1..c27ee89 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,6 +30,7 @@ RUN php artisan storage:link \ # before validation ever sees it, and Next's Server Action limit was raised to # 8 MB. post_max_size has to exceed upload_max_filesize or the extra fields # (and therefore $_POST) are dropped along with the file. +USER root RUN INI_DIR="$(php --ini | sed -n 's/^Scan for additional .ini files in: //p' | tr -d '"')" \ && test -n "$INI_DIR" \ && mkdir -p "$INI_DIR" \ diff --git a/ops/backend.compose.yml b/ops/backend.compose.yml new file mode 100644 index 0000000..c8e81ff --- /dev/null +++ b/ops/backend.compose.yml @@ -0,0 +1,7 @@ +# Overlay only build selection and image identity. Existing mounts/networks/labels stay intact. +services: + backend: + image: ${PARSA_BACKEND_IMAGE:?CI image identity required} + build: + context: ${PARSA_BUILD_CONTEXT:?reviewed checkout required} + dockerfile: Dockerfile diff --git a/ops/deploy-backend.sh b/ops/deploy-backend.sh new file mode 100644 index 0000000..eeace67 --- /dev/null +++ b/ops/deploy-backend.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# CI-owned backend recovery: no migrations, cleanup, or dependency-container changes. +set -euo pipefail +APP_DIR=${APP_DIR:-/opt/services/parsa/backend/repo} +BASE_COMPOSE=${BASE_COMPOSE:-/opt/services/parsa/backend/compose.yml} +EXPECTED=${1:?expected immutable commit required} +[[ "$EXPECTED" =~ ^[0-9a-f]{40}$ ]] +cd "$APP_DIR" +test "$(git rev-parse HEAD)" = "$EXPECTED" +test -z "$(git status --porcelain --untracked-files=normal)" +export PARSA_BUILD_CONTEXT="$APP_DIR" +export PARSA_BACKEND_IMAGE="parsa-backend-ci:$EXPECTED" +compose=(docker compose -p backend -f "$BASE_COMPOSE" -f "$APP_DIR/ops/backend.compose.yml") +"${compose[@]}" config --quiet +"${compose[@]}" build backend +# Exercise the autoloader in the built image before touching the current container. +docker run --rm --network none --entrypoint php "$PARSA_BACKEND_IMAGE" -r 'require "/var/www/html/vendor/autoload.php"; if (!class_exists("Illuminate\\Foundation\\Application")) { exit(1); }' +"${compose[@]}" up -d --no-deps --no-build backend +for i in {1..30}; do + code=$(curl --max-time 15 -s -o /dev/null -w '%{http_code}' -H 'Accept: application/json' https://backend.parsaaghayi.ir/api/website/about-me/1/translation/en || true) + if [ "$code" = 200 ]; then + echo "Backend deployed successfully: $EXPECTED (real API HTTP200)" + exit 0 + fi + sleep 2 +done +echo "ERROR: real API health gate failed (HTTP ${code:-none})" >&2 +exit 1 diff --git a/tests/deploy_fixture.py b/tests/deploy_fixture.py new file mode 100644 index 0000000..54a2758 --- /dev/null +++ b/tests/deploy_fixture.py @@ -0,0 +1,50 @@ +"""Disposable deployment boundary tests. Never contacts Docker or production.""" +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + +class DeployBoundary(unittest.TestCase): + def run_fixture(self, broken=False): + with tempfile.TemporaryDirectory() as tmp: + p = Path(tmp) + (p/'bin').mkdir() + (p/'repo/ops').mkdir(parents=True) + (p/'repo/ops/backend.compose.yml').write_text('services: {}\n') + (p/'base.yml').write_text('services: {}\n') + log = p/'calls' + for name, body in { + 'git': 'if [ "$1" = rev-parse ]; then printf "%040d\\n" 1; fi', + 'docker': 'printf "%s\\n" "$*" >> "$CALLS"; if [ "$1" = run ] && [ "$BROKEN" = 1 ]; then exit 17; fi', + 'curl': 'printf 200', + 'sleep': ':', + }.items(): + f = p/'bin'/name + f.write_text('#!/bin/sh\n'+body+'\n') + f.chmod(0o755) + env = dict(os.environ, PATH=str(p/'bin')+':'+os.environ['PATH'], + APP_DIR=str(p/'repo'), BASE_COMPOSE=str(p/'base.yml'), + CALLS=str(log), BROKEN=str(int(broken))) + result = subprocess.run(['bash', str(ROOT/'ops/deploy-backend.sh'), '0'*39+'1'], env=env, capture_output=True, text=True) + return result, log.read_text() if log.exists() else '' + + def test_missing_vendor_prevents_container_swap(self): + r, calls = self.run_fixture(True) + self.assertNotEqual(r.returncode, 0) + self.assertIn('run --rm --network none --entrypoint php', calls) + self.assertNotIn(' up ', calls) + + def test_build_assertion_precedes_backend_only_swap(self): + r, calls = self.run_fixture() + self.assertEqual(r.returncode, 0, r.stderr) + self.assertLess(calls.index(' build backend'), calls.index('run --rm')) + self.assertLess(calls.index('run --rm'), calls.index(' up ')) + self.assertIn('up -d --no-deps --no-build backend', calls) + self.assertNotIn('migrate', calls) + self.assertNotIn('prune', calls) + +if __name__ == '__main__': + unittest.main()