#!/usr/bin/env bash set -Eeuo pipefail IFS=$'\n\t' umask 077 usage() { printf 'Usage: %s TAG IMAGE_PREFIX\n' "$0" >&2 } [[ $# -eq 2 ]] || { usage; exit 2; } TAG="$1" IMAGE_PREFIX="$2" PROFILES="${CONTROL_PLANE_PROFILES:-}" [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { printf 'invalid deployment tag: %s\n' "$TAG" >&2 exit 2 } [[ "$IMAGE_PREFIX" =~ ^[a-z0-9][a-z0-9./:_-]*[a-z0-9]$ ]] || { printf 'invalid container image prefix\n' >&2 exit 2 } [[ -z "$PROFILES" || "$PROFILES" == observability ]] || { printf 'CONTROL_PLANE_PROFILES must be empty or observability\n' >&2 exit 2 } for command_name in docker flock mktemp; do command -v "$command_name" >/dev/null || { printf '%s is required on the deployment server\n' "$command_name" >&2 exit 1 } done docker compose version >/dev/null for required_path in .env docker-compose.yml docker-compose.prod.yml secrets; do [[ -e "$required_path" ]] || { printf 'required deployment path is missing: %s/%s\n' "$PWD" "$required_path" >&2 exit 1 } done exec 9>.deploy.lock if ! flock -n 9; then printf 'another control-plane deployment is already running\n' >&2 exit 1 fi NEXT_ENV="$(mktemp .release.env.next.XXXXXX)" cleanup() { rm -f "$NEXT_ENV" } trap cleanup EXIT printf '%s\n' \ "CONTROL_PLANE_BACKEND_IMAGE=${IMAGE_PREFIX}-backend:${TAG}" \ "CONTROL_PLANE_FRONTEND_IMAGE=${IMAGE_PREFIX}-frontend:${TAG}" \ "CONTROL_PLANE_GRAFANA_IMAGE=${IMAGE_PREFIX}-grafana:${TAG}" \ > "$NEXT_ENV" PROFILE_ARGS=() SERVICES=(postgres backend frontend) if [[ "$PROFILES" == observability ]]; then PROFILE_ARGS=(--profile observability) SERVICES+=(grafana) fi compose() { local release_env="$1" shift docker compose \ --env-file .env \ --env-file "$release_env" \ -f docker-compose.yml \ -f docker-compose.prod.yml \ "${PROFILE_ARGS[@]}" \ "$@" } # Validate all interpolation and secret paths before changing the active tag. compose "$NEXT_ENV" config --quiet HAD_PREVIOUS=false if [[ -f .release.env ]]; then cp -f .release.env .release.env.previous HAD_PREVIOUS=true fi mv -f "$NEXT_ENV" .release.env NEXT_ENV="" printf 'pulling control-plane images for %s\n' "$TAG" compose .release.env pull backend frontend if [[ "$PROFILES" == observability ]]; then compose .release.env pull grafana fi printf 'deploying control plane %s\n' "$TAG" if compose .release.env up \ --detach \ --no-build \ --remove-orphans \ --wait \ --wait-timeout 180 \ "${SERVICES[@]}"; then compose .release.env ps printf 'control plane %s is healthy\n' "$TAG" exit 0 fi printf 'deployment %s failed health checks\n' "$TAG" >&2 if [[ "$HAD_PREVIOUS" == true ]]; then printf 'restoring the previous container image set\n' >&2 cp -f .release.env.previous .release.env compose .release.env pull backend frontend || true if [[ "$PROFILES" == observability ]]; then compose .release.env pull grafana || true fi if ! compose .release.env up \ --detach \ --no-build \ --remove-orphans \ --wait \ --wait-timeout 180 \ "${SERVICES[@]}"; then printf 'automatic image rollback also failed; operator action is required\n' >&2 fi else printf 'no previous release metadata exists; automatic rollback is unavailable\n' >&2 fi exit 1