Files
canina/scripts/release-cache-stage.sh
T

56 lines
3.0 KiB
Bash

#!/usr/bin/env bash
# Narrow code-only Stage release in the existing CI runner/daemon boundary.
set -euo pipefail
set +x
umask 077
[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || exit 2
STAGE_DIR=/opt/services/canina/stage
exec 9>/opt/services/canina/stage-backend-deploy.lock
flock -n 9 || { printf 'Stage deployment locked\n' >&2; exit 1; }
cd "$STAGE_DIR"
git fetch origin develop
[[ $(git rev-parse origin/develop) == "$DEPLOY_SHA" ]] || exit 2
# Never reset/clean the operational checkout or overwrite its generated configs.
TARGET_DIR="/opt/services/canina/cache-releases/$DEPLOY_SHA"
[[ ! -e "$TARGET_DIR" ]] || { printf 'Release directory already exists\n' >&2; exit 2; }
mkdir -p "$TARGET_DIR"
git archive "$DEPLOY_SHA" | tar -x -C "$TARGET_DIR"
[[ $(git rev-parse HEAD) == "$DEPLOY_SHA" ]] || printf 'Operational HEAD intentionally preserved\n'
command -v python3 >/dev/null || apk add --no-cache python3
cd "$TARGET_DIR"
python3 scripts/provision-revalidation-env.py --environment stage --repair-empty \
--source "$STAGE_DIR/.revalidation.env" --output "$TARGET_DIR/.revalidation.env"
# Build with the existing operational nginx configuration, without changing it.
cp "$STAGE_DIR/nginx.conf" "$TARGET_DIR/nginx.conf"
# Compose uses the existing operational runtime configuration plus a private,
# release-specific override. Neither the database nor Redis is recreated.
export STAGE_DIR TARGET_DIR DEPLOY_SHA
python3 - <<'PY'
import json, os
from pathlib import Path
root=Path(os.environ['TARGET_DIR']); sha=os.environ['DEPLOY_SHA']
services={}
for kind in ('backend','frontend'):
name=kind+'_stage'
services[name]={
'build': {'context': str(root/('backend' if kind=='backend' else '.')),
'labels': {'org.opencontainers.image.revision': sha},
'args': {'NPM_REGISTRY': 'https://registry.parsaaghayi.ir/repository/npm/'}},
'labels': {'ir.canina.release.sha': sha},
'env_file': [{'path': str(root/'.revalidation.env'), 'required': True}],
}
services['backend_stage']['environment']={'FRONTEND_INTERNAL_URL':'http://frontend_stage:3000'}
services['backend_stage']['command']=['node','dist/main.js']
services['frontend_stage']['environment']={'INTERNAL_API_URL':'http://backend_stage:3000/api'}
Path('release.override.json').write_text(json.dumps({'services':services}))
PY
compose=(docker compose -p stage --project-directory "$STAGE_DIR" -f "$STAGE_DIR/docker-compose.yml" -f "$TARGET_DIR/release.override.json")
"${compose[@]}" config --quiet
"${compose[@]}" --progress plain build backend_stage frontend_stage
"${compose[@]}" up -d --no-deps --force-recreate backend_stage frontend_stage
for name in canino_backend_stage canino_frontend_stage; do
[[ $(docker inspect -f '{{index .Config.Labels "ir.canina.release.sha"}}' "$name") == "$DEPLOY_SHA" ]]
docker inspect -f '{{.Name}} image={{.Image}} release={{index .Config.Labels "ir.canina.release.sha"}}' "$name"
done
printf 'Code-only Stage release complete: %s\n' "$DEPLOY_SHA"