Files
syops/scripts/deploy_compose.sh
T
2026-04-24 14:26:26 +08:00

69 lines
1.7 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ENV_NAME="${1:?Environment is required: dev|test|prod}"
FRONTEND_IMAGE="${2:-}"
BACKEND_IMAGE="${3:-}"
FRONTEND_CHANGED="${4:-false}"
BACKEND_CHANGED="${5:-false}"
DEPLOY_CONFIG_CHANGED="${6:-false}"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
COMPOSE_FILE="${ROOT_DIR}/deploy/${ENV_NAME}/docker-compose.yml"
ENV_FILE="${ROOT_DIR}/deploy/${ENV_NAME}/.env"
if [[ ! -f "${COMPOSE_FILE}" ]]; then
echo "Missing compose file: ${COMPOSE_FILE}"
exit 1
fi
if [[ ! -f "${ENV_FILE}" ]]; then
echo "Missing env file: ${ENV_FILE}"
exit 1
fi
if docker compose version >/dev/null 2>&1; then
DC_CMD=(docker compose)
elif command -v docker-compose >/dev/null 2>&1; then
DC_CMD=(docker-compose)
else
echo "Missing docker compose command. Install docker compose plugin or docker-compose."
exit 1
fi
echo "Using compose command: ${DC_CMD[*]}"
if [[ -n "${FRONTEND_IMAGE}" ]]; then
export FRONTEND_IMAGE
fi
if [[ -n "${BACKEND_IMAGE}" ]]; then
export BACKEND_IMAGE
fi
cd "${ROOT_DIR}"
services=()
if [[ "${FRONTEND_CHANGED}" == "true" ]]; then
services+=("hxops-frontend")
fi
if [[ "${BACKEND_CHANGED}" == "true" ]]; then
services+=("hxops-backend")
fi
if [[ ${#services[@]} -eq 0 && "${DEPLOY_CONFIG_CHANGED}" == "true" ]]; then
services=("hxops-frontend" "hxops-backend")
fi
if [[ ${#services[@]} -eq 0 ]]; then
echo "No service changes detected, skipping deployment."
exit 0
fi
echo "Deploying environment: ${ENV_NAME}"
echo "Services: ${services[*]}"
"${DC_CMD[@]}" -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" up -d "${services[@]}"
"${ROOT_DIR}/scripts/healthcheck.sh" "${ENV_NAME}" "${COMPOSE_FILE}" "${ENV_FILE}"