91 lines
5.0 KiB
Bash
91 lines
5.0 KiB
Bash
#!/usr/bin/env bash
|
|
# Invoked only by the authenticated CI host boundary; never run manually.
|
|
set -euo pipefail
|
|
set +x
|
|
fail() { printf 'ERROR: %s\n' "$1" >&2; exit 2; }
|
|
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ && $# == 0 ]] || fail 'CI-only immutable production release'
|
|
[[ $(id -u) == 0 && ! -e /.dockerenv && ! -e /run/.containerenv ]] || fail 'approved host root context required'
|
|
exec 8>/opt/services/canina/production-deploy.lock
|
|
flock -n 8 || fail 'Production rollout already in progress'
|
|
# Acceptance depends on a Stage artifact: serialize against Stage replacing it.
|
|
exec 9>/opt/services/canina/stage-backend-deploy.lock
|
|
flock -n 9 || fail 'Stage rollout in progress'
|
|
umask 077
|
|
export DOCKER_BUILDKIT=1 COMPOSE_DOCKER_CLI_BUILD=1
|
|
ROOT=/opt/services/canina/production-releases
|
|
mkdir -p "$ROOT"
|
|
[[ ! -L "$ROOT" && $(realpath "$ROOT") == "$ROOT" ]] || fail 'unsafe release root'
|
|
TARGET_DIR="$ROOT/$DEPLOY_SHA"
|
|
# Existing directories (including failed attempts) are never overwritten.
|
|
mkdir "$TARGET_DIR"
|
|
git clone --no-checkout --single-branch --branch main https://git.parsaaghayi.ir/parsa/canina.git "$TARGET_DIR"
|
|
cd "$TARGET_DIR"
|
|
[[ $(git rev-parse origin/main) == "$DEPLOY_SHA" ]] || fail 'main must equal accepted Stage SHA (fast-forward promotion only)'
|
|
git checkout --detach "$DEPLOY_SHA"
|
|
python3 scripts/production-release.py receipt
|
|
# No schema/seed operation is allowed by this code-only release path.
|
|
# Operational checkout is intentionally dirty and is NEVER reset/pulled/rendered.
|
|
python3 scripts/provision-revalidation-env.py --environment prod --source /opt/services/canina/prod/.revalidation.env --output "$TARGET_DIR/.revalidation.env"
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
p=Path('nginx.conf');s=p.read_text()
|
|
for k,v in {'__BACKEND_HOST__':'backend_prod','__APP_HOST__':'canina.ir','__ADMIN_HOST__':'admin.canina.ir'}.items(): s=s.replace(k,v)
|
|
assert '__BACKEND_HOST__' not in s
|
|
p.write_text(s)
|
|
PY
|
|
# Build contexts must not contain private snapshots or env files.
|
|
printf '\n.prod-*.json\n.*.env\n' >> .dockerignore
|
|
printf '\n.prod-*.json\n' >> .git/info/exclude
|
|
python3 scripts/production-release.py snapshot
|
|
docker volume inspect prod_canina_prod_db prod_canina_prod_uploads >/dev/null
|
|
docker network inspect traefik_public >/dev/null
|
|
compose() { command docker compose -p prod --env-file /dev/null --project-directory "$TARGET_DIR" -f "$TARGET_DIR/$1" "${@:2}"; }
|
|
compose .prod-compose.json config --quiet
|
|
compose .prod-rollback.json config --quiet
|
|
compose .prod-compose.json --progress plain build backend_prod frontend_prod
|
|
# Always check the generated target client against live read-only production catalog.
|
|
# This precedes even rollback arming: incompatible images never replace runtime.
|
|
python3 scripts/production-release.py schema-check
|
|
# Snapshot uses immutable old image IDs, runtime environment, mounts and labels.
|
|
# DB/Redis/support bot are not in this app-only model and must not be orphan-pruned.
|
|
rollback() {
|
|
rc=$?
|
|
trap - EXIT
|
|
if [[ ${started:-false} == true ]]; then
|
|
if compose .prod-rollback.json up -d --no-deps --force-recreate backend_prod frontend_prod; then
|
|
printf 'Production failed; prior immutable app images restored; live acceptance still required\n' >&2
|
|
else
|
|
printf 'CRITICAL: automatic rollback failed; retain private release evidence and escalate\n' >&2
|
|
fi
|
|
fi
|
|
exit "$rc"
|
|
}
|
|
trap rollback EXIT
|
|
started=true
|
|
compose .prod-compose.json up -d --no-deps --force-recreate backend_prod frontend_prod
|
|
# Private acceptance output has no response bodies, SQL or credentials.
|
|
python3 - <<'PY'
|
|
import json, subprocess, time, urllib.request
|
|
from pathlib import Path
|
|
expected=json.loads(Path('.prod-compose.json').read_text())['services']
|
|
for name,s in expected.items():
|
|
x=json.loads(subprocess.check_output(['docker','inspect',s['container_name']]))[0]
|
|
image=json.loads(subprocess.check_output(['docker','image','inspect',s['image']]))[0]['Id']
|
|
assert x['State']['Running'] and x['Image']==image
|
|
for url in ['https://api.canina.ir/api/products?limit=1','https://canina.ir/blog','https://admin.canina.ir']:
|
|
for n in range(30):
|
|
try:
|
|
with urllib.request.urlopen(url,timeout=10) as r: assert r.status==200
|
|
break
|
|
except Exception:
|
|
if n==29: raise RuntimeError('Production public acceptance failed') from None
|
|
time.sleep(2)
|
|
PY
|
|
# Verify real backend-to-Next authentication, never expose the key or body.
|
|
docker exec canino_backend_prod node -e '
|
|
(async()=>{const key=process.env.REVALIDATION_SECRET;if(!key)throw Error();
|
|
const r=await fetch("http://frontend_prod:3000/api/revalidate",{method:"POST",headers:{"Content-Type":"application/json","x-revalidate-secret":key},body:JSON.stringify({paths:["/blog"]}),signal:AbortSignal.timeout(10000)});if(!r.ok)throw Error();})().catch(()=>{console.error("Production authenticated revalidation failed");process.exitCode=1});'
|
|
started=false
|
|
trap - EXIT
|
|
printf 'Production immutable code-only rollout complete: %s; protected/live feature acceptance remains required\n' "$DEPLOY_SHA"
|