Files
AIcreat/deploy.sh
2026-04-28 11:55:04 +08:00

68 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"
resolve_default_branch() {
local current
current="$(git branch --show-current 2>/dev/null || true)"
if [[ -n "$current" ]]; then
echo "$current"
return
fi
local remote_head
remote_head="$(git symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null || true)"
if [[ -n "$remote_head" ]]; then
echo "${remote_head#origin/}"
return
fi
echo "main"
}
TARGET_BRANCH="${1:-$(resolve_default_branch)}"
echo "==> Deploy directory: $PROJECT_ROOT"
echo "==> Target branch: $TARGET_BRANCH"
if [[ -n "$(git status --porcelain)" ]]; then
STASH_NAME="auto-deploy-$(date +%Y%m%d-%H%M%S)"
echo "==> Working tree is not clean, auto stashing: $STASH_NAME"
git stash push -u -m "$STASH_NAME" >/dev/null
fi
echo "==> Fetching remote updates..."
git fetch --all --prune
echo "==> Checking out branch: $TARGET_BRANCH"
git checkout "$TARGET_BRANCH"
echo "==> Pulling latest code from origin/$TARGET_BRANCH"
git pull --ff-only origin "$TARGET_BRANCH"
if [[ ! -f ".env" && -f ".env.example" ]]; then
cp ".env.example" ".env"
echo "==> Created .env from .env.example"
fi
if docker compose version >/dev/null 2>&1; then
COMPOSE_CMD="docker compose"
elif command -v docker-compose >/dev/null 2>&1; then
COMPOSE_CMD="docker-compose"
else
COMPOSE_CMD=""
fi
if [[ -n "$COMPOSE_CMD" ]]; then
echo "==> Restarting service with $COMPOSE_CMD"
$COMPOSE_CMD down
$COMPOSE_CMD up -d --build
echo "==> Deployment finished. Service: http://localhost:18000"
else
echo "==> Docker Compose not found, fallback to ./start.sh"
chmod +x ./start.sh
./start.sh
fi