jenkins CICD

This commit is contained in:
sophie committed 2026-04-24 14:26:26 +08:00
1 parent c5f868a8ef
commit d710a88d04
20 files changed
+1174 -25

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
#!/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}"
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -euo pipefail
ENV_NAME="${1:?Environment is required}"
COMPOSE_FILE="${2:?Compose file path required}"
ENV_FILE="${3:?Env file path required}"
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
max_attempts=20
sleep_seconds=3
for ((i=1; i<=max_attempts; i++)); do
status=$("${DC_CMD[@]}" -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" ps --status running --services | wc -l)
if [[ "${status}" -ge 2 ]]; then
echo "Healthcheck passed for ${ENV_NAME} environment."
exit 0
fi
echo "Healthcheck waiting (${i}/${max_attempts})..."
sleep "${sleep_seconds}"
done
echo "Healthcheck failed for ${ENV_NAME} environment."
"${DC_CMD[@]}" -f "${COMPOSE_FILE}" --env-file "${ENV_FILE}" ps
exit 1