Integrate schema-first Stage rollout with immutable CI commit pinning

This commit is contained in:
devops committed 2026-10-23 07:52:49 +00:00
1 parent 841eab0556
commit c7f1d25e98
8 files changed
+225 -46

No files matched your search

+21 -44
View File
@@ -1,22 +1,17 @@
#!/bin/bash
set -e
# Explicit no-data-write stage rollout. Run only after testing and pushing HEAD.
if [ "${2:-}" = "stage-backend-only" ]; then
[ "${1:-}" = "develop" ] || exit 2
cd /opt/services/canina/stage
exec 9>/opt/services/canina/stage-backend-deploy.lock
flock -n 9 || { echo "Stage deployment already in progress"; exit 1; }
[ "$(git branch --show-current)" = develop ]
git fetch origin develop
[ "$(git rev-parse HEAD)" = "$(git rev-parse origin/develop)" ]
git diff --quiet -- backend scripts/compose.stage.yml scripts/deploy.sh
# Read tracked compose directly; preserve generated config and nginx.
docker compose -p stage -f scripts/compose.stage.yml --project-directory . build backend_stage
docker compose -p stage -f scripts/compose.stage.yml --project-directory . up -d --no-deps --remove-orphans backend_stage
exit 0
set -euo pipefail
# CI/CD-only Stage rollout; Production awaits separate reviewed authorization.
if [ "${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}}" != true ] || [ "${1:-}" != develop ] || [ "$#" != 1 ]; then
echo 'ERROR: refusing deployment outside CI/CD Stage rollout' >&2
exit 2
fi
if [[ ! "${DEPLOY_SHA:-}" =~ ^[0-9a-f]{40}$ ]]; then
echo "ERROR: refusing deployment without an immutable commit SHA" >&2
exit 2
fi
# END ROLLOUT GUARD
exec 9>/opt/services/canina/stage-backend-deploy.lock
flock -n 9 || { echo 'Stage deployment already in progress' >&2; exit 1; }
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
@@ -56,11 +51,16 @@ mkdir -p "$TARGET_DIR"
if [ ! -d "$TARGET_DIR/.git" ]; then
echo "Cloning repository into $TARGET_DIR..."
git clone -b "$BRANCH" https://git.parsaaghayi.ir/parsa/canina.git "$TARGET_DIR"
git -C "$TARGET_DIR" cat-file -e "${DEPLOY_SHA}^{commit}"
git -C "$TARGET_DIR" merge-base --is-ancestor "$DEPLOY_SHA" "origin/$BRANCH"
git -C "$TARGET_DIR" reset --hard "$DEPLOY_SHA"
else
echo "Pulling latest changes in $TARGET_DIR..."
cd "$TARGET_DIR"
git fetch origin "$BRANCH"
git reset --hard "origin/$BRANCH"
git cat-file -e "${DEPLOY_SHA}^{commit}"
git merge-base --is-ancestor "$DEPLOY_SHA" "origin/$BRANCH"
git reset --hard "$DEPLOY_SHA"
cd -
fi
@@ -125,32 +125,9 @@ docker compose --progress plain build \
--build-arg VITE_API_URL="${API_URL}" \
${NPM_REGISTRY_ARG}
echo "Building support-chat bot image from committed source..."
BOT_IMAGE="canina-support-bot:${BRANCH}"
DOCKER_BUILDKIT=0 docker build --pull=false -f scripts/support-chat/Dockerfile -t "${BOT_IMAGE}" .
BOT_ENV="${TARGET_DIR}/.support-bot.runtime.env"
docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' canina-bot > "${BOT_ENV}"
chmod 600 "${BOT_ENV}"
echo "Starting containers..."
docker compose up -d --remove-orphans
echo "Starting support-chat bot container..."
docker rm -f canina-bot >/dev/null 2>&1 || true
docker run -d --name canina-bot --restart unless-stopped --env-file "${BOT_ENV}" -e HERMES_HOME=/opt/data -e HERMES_RUNTIME_DIR=/opt/hermes/tools -e API_SERVER_HOST=0.0.0.0 -e API_SERVER_PORT=8642 --network traefik_public -v canina_bot_data:/opt/data "${BOT_IMAGE}" gateway run
docker network connect --ip 172.22.0.15 vpn_net canina-bot
rm -f "${BOT_ENV}"
echo "Waiting for backend to start..."
sleep 15
# Apply committed Prisma migrations as part of the CI/CD deployment.
# This is intentionally migrate deploy (not db push): it is repeatable,
# fail-closed, and applies only migrations already reviewed and committed.
echo "Applying committed database migrations..."
if [ "$BRANCH" = "main" ]; then
docker exec -e PRISMA_HIDE_UPDATE_MESSAGE=true "${CONTAINER_PREFIX}" npx prisma migrate resolve --applied 20261013210253_add_admin_modules >/dev/null 2>&1 || true
fi
docker exec -e PRISMA_HIDE_UPDATE_MESSAGE=true "${CONTAINER_PREFIX}" npx prisma migrate deploy
# Code-only deployment: seed and destructive data changes are not performed here.
# Never recreate the shared Production support bot during a Stage rollout.
source scripts/rollout-stage.sh
stage_rollout
# Auto-cleanup old docker builder cache to keep disk healthy (keeps latest 6GB)
echo "Cleaning up old docker build cache..."