fix(ops): reconcile CI backend wrapper and gate locked dependency image before swap
Deploy Backend / deploy (push) Successful in 1m17s
Deploy Backend / deploy (push) Successful in 1m17s
This commit is contained in:
1 parent
c2960c4ed0
commit
3fb1e417c6
5 files changed
+108
-2
No files matched your search
@@ -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
|
||||
@@ -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" \
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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()
|
||||
Reference in new issue
Block a user