Files
vtuber/deploy/deploy-prod.sh

66 lines
2.0 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
source "${SCRIPT_DIR}/.env.prod"
if [ -z "${DEPLOY_HOST:-}" ] || [ -z "${DEPLOY_USER:-}" ] || [ -z "${DEPLOY_DIR:-}" ]; then
echo "請先建立 deploy/.env.prod 並設定 DEPLOY_HOST / DEPLOY_USER / DEPLOY_DIR" >&2
exit 1
fi
SSH_OPTS=(-p "${DEPLOY_PORT:-22}")
REMOTE="${DEPLOY_USER}@${DEPLOY_HOST}"
echo "Preparing deployment content to ${REMOTE}:${DEPLOY_DIR}"
synced=0
if ssh "${SSH_OPTS[@]}" "$REMOTE" "[ -d \"${DEPLOY_DIR}/.git\" ]"; then
ssh "${SSH_OPTS[@]}" "$REMOTE" "
set -euo pipefail
cd \"${DEPLOY_DIR}\"
if git show-ref --verify --quiet \"refs/heads/${DEPLOY_BRANCH}\"; then
git fetch --all || true
git checkout \"${DEPLOY_BRANCH}\" || true
git pull origin \"${DEPLOY_BRANCH}\" || true
exit 0
else
echo \"[warn] 遠端 git 無法找到 ${DEPLOY_BRANCH},改用 rsync 同步部署檔案。\"\n
exit 1
fi
" && synced=1 || synced=0
# 分支存在但拉取失敗時fallback 改為 rsync
if [ ${synced} -ne 1 ]; then
ssh "${SSH_OPTS[@]}" "$REMOTE" "[ -d \"${DEPLOY_DIR}\" ] || mkdir -p \"${DEPLOY_DIR}\""
fi
else
synced=0
fi
if [ ${synced} -ne 1 ]; then
echo "遠端未完成 git 同步,改用 rsync 直接推送部署內容..."
rsync -av --delete \
--exclude 'node_modules' \
--exclude '.next' \
--exclude '.turbo' \
--exclude 'dist' \
--exclude 'coverage' \
--exclude '.env' \
--exclude '*.log' \
-e "ssh ${SSH_OPTS[*]}" \
"${ROOT_DIR}/" "$REMOTE:${DEPLOY_DIR}/"
fi
ssh "${SSH_OPTS[@]}" "$REMOTE" "
set -euo pipefail
cd \"${DEPLOY_DIR}\"
cp deploy/.env.prod .env
docker compose -f deploy/docker-compose.prod.yml --env-file deploy/.env.prod down || true
docker compose -f deploy/docker-compose.prod.yml --env-file deploy/.env.prod up -d --build
"
echo "VTuber production deploy sent to ${DEPLOY_USER}@${DEPLOY_HOST}"