| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #!/usr/bin/env bash
- # deploy.sh — chạy trên Pi 4 khi nhận webhook từ Gogs
- # Credentials đọc từ /opt/timelapse/.env (KHÔNG commit .env lên git)
- set -euo pipefail
- APP_DIR="${APP_DIR:-/opt/timelapse}"
- BRANCH="${1:-main}"
- # Load env vars từ local file (không có trong git)
- ENV_FILE="$APP_DIR/.env"
- if [[ -f "$ENV_FILE" ]]; then
- set -a
- source "$ENV_FILE"
- set +a
- fi
- log() { echo "[$(date '+%H:%M:%S')] $*"; }
- # Build authenticated git URL nếu có Gogs token
- REPO_URL="${GOGS_REPO_URL:-https://git.k9tech.space/kingkong/timelapse-2.git}"
- if [[ -n "${GOGS_TOKEN:-}" ]]; then
- REPO_URL="${REPO_URL/https:\/\//https://${GOGS_TOKEN}@}"
- fi
- deploy() {
- log "Deploying from $REPO_URL (branch: $BRANCH)"
- mkdir -p "$(dirname "$APP_DIR")"
- if [[ -d "$APP_DIR/.git" ]]; then
- cd "$APP_DIR"
- git remote set-url origin "$REPO_URL"
- git fetch origin
- git checkout "$BRANCH"
- git pull origin "$BRANCH"
- else
- log "Cloning fresh..."
- git clone --branch "$BRANCH" "$REPO_URL" "$APP_DIR"
- fi
- log "Installing deps..."
- cd "$APP_DIR"
- npm install --workspaces --include-workspace-root --ignore-scripts
- log "Building..."
- npm run build --workspace=packages/shared-types
- npm run build --workspace=apps/api-server
- npm run build --workspace=apps/web-dashboard
- log "Running migrations..."
- npm run migrate --workspace=apps/api-server || log "Migration skipped (no DB or error)"
- log "Restarting Docker Compose..."
- cd "$APP_DIR"
- docker compose down
- docker compose up -d --build postgres redis
- sleep 8
- docker compose up -d api-server web-dashboard worker || log "Some services skipped"
- log "Deploy complete!"
- }
- deploy
|