website-scripts/deploy-cc.sh

170 lines
5.3 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.

#!/bin/bash
# 本地修改 → 上传服务器 → 构建 → 部署
# 兼容 macOS (Bash 3.2+) / Ubuntu / Rocky Linux
# 用法bash deploy-cc.sh
set -e
# ANSI 颜色(兼容所有平台的 printf
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
log() { printf "${GREEN}[✓]${NC} %s\n" "$1"; }
warn() { printf "${YELLOW}[!]${NC} %s\n" "$1"; }
# ============================================================
# 选择要部署的站点
# ============================================================
echo ""
printf "${CYAN}=========================================${NC}\n"
printf "${CYAN} 选择要部署的站点${NC}\n"
printf "${CYAN}=========================================${NC}\n"
echo ""
echo " 1) www.tlyq.cc (图灵引擎官网)"
echo " 2) cloud.tlyq.cc (图灵智算系统云平台)"
echo " 3) token.tlyq.cc (Token工厂)"
echo ""
printf "请输入编号 (1/2/3): "
read choice
case "$choice" in
1)
SITE="www"
LOCAL_DIR="/Users/niuniu/programs/docker/www-cc/src"
REMOTE_DIR="/tmp/project-extract-2/turing-engine"
DEPLOY_DIR="/root/docker/www-cc/html"
;;
2)
SITE="cloud"
LOCAL_DIR="/Users/niuniu/programs/docker/cloud-cc/html"
REMOTE_DIR="/root/docker/cloud-cc/html"
DEPLOY_DIR="/root/docker/cloud-cc/html"
;;
3)
SITE="token"
LOCAL_DIR="/Users/niuniu/programs/docker/token-cc/html"
REMOTE_DIR="/root/docker/token-cc/html"
DEPLOY_DIR="/root/docker/token-cc/html"
;;
*)
echo "无效选择,已取消"
exit 1
;;
esac
echo ""
log "部署目标: $LOCAL_DIR → 服务器 $DEPLOY_DIR"
echo ""
# ============================================================
# 部署前:将 localhost 链接替换为正式域名
# ============================================================
restore_files() {
# 恢复本地文件中的 localhost 链接deploy 后 / 异常退出时)
case "$SITE" in
www)
cd "$LOCAL_DIR"
find src -type f -name '*.tsx' -exec sed -i '' \
-e 's|http://localhost:5174/login|https://cloud.tlyq.cc/login|g' \
-e 's|http://localhost:5175/login|https://token.tlyq.cc/login|g' \
{} +
;;
cloud)
sed -i '' 's|http://localhost:5173|https://www.tlyq.cc|g' "$LOCAL_DIR/index.html"
;;
token)
sed -i '' 's|http://localhost:5173|https://www.tlyq.cc|g' "$LOCAL_DIR/index.html"
;;
esac
}
to_production() {
log "将 localhost 链接替换为正式域名..."
case "$SITE" in
www)
cd "$LOCAL_DIR"
find src -type f -name '*.tsx' -exec sed -i '' \
-e 's|http://localhost:5174/login|https://cloud.tlyq.cc/login|g' \
-e 's|http://localhost:5175/login|https://token.tlyq.cc/login|g' \
{} +
;;
cloud)
sed -i '' 's|http://localhost:5173|https://www.tlyq.cc|g' "$LOCAL_DIR/index.html"
;;
token)
sed -i '' 's|http://localhost:5173|https://www.tlyq.cc|g' "$LOCAL_DIR/index.html"
;;
esac
log "域名替换完成"
}
to_localhost() {
log "恢复 localhost 链接..."
case "$SITE" in
www)
cd "$LOCAL_DIR"
find src -type f -name '*.tsx' -exec sed -i '' \
-e 's|https://cloud.tlyq.cc/login|http://localhost:5174/login|g' \
-e 's|https://token.tlyq.cc/login|http://localhost:5175/login|g' \
{} +
;;
cloud)
sed -i '' 's|https://www.tlyq.cc|http://localhost:5173|g' "$LOCAL_DIR/index.html"
;;
token)
sed -i '' 's|https://www.tlyq.cc|http://localhost:5173|g' "$LOCAL_DIR/index.html"
;;
esac
log "已恢复为 localhost 链接"
}
# 确保无论成功或失败都恢复本地文件
trap 'to_localhost' EXIT
to_production
# ============================================================
# www 站点需要构建cloud/token 是静态文件直接上传
# ============================================================
if [ "$SITE" = "www" ]; then
# 打包源码
log "打包源码..."
cd "$LOCAL_DIR"
COPYFILE_DISABLE=1 tar czf /tmp/www-src.tar.gz --exclude='node_modules' --exclude='.next' --exclude='out' .
# 上传
log "上传到服务器..."
scp /tmp/www-src.tar.gz tgz:/tmp/www-src.tar.gz
# 服务器上构建并部署
log "服务器上构建并部署..."
ssh tgz "cd $REMOTE_DIR && rm -rf .next out src && tar xzf /tmp/www-src.tar.gz && npm install && rm -rf .next out && npm run build && rm -rf ${DEPLOY_DIR:?}/* && cp -r out/* $DEPLOY_DIR/ && echo 'deployed'"
else
# cloud/token 直接上传静态文件
log "上传静态文件..."
scp "$LOCAL_DIR"/index.html tgz:$DEPLOY_DIR/index.html
scp "$LOCAL_DIR"/nginx.conf tgz:$DEPLOY_DIR/nginx.conf
# 重启容器
log "重启容器..."
ssh tgz "docker restart ${SITE}-cc"
fi
# 验证
echo ""
log "验证部署..."
case "$SITE" in
www) URL="https://www.tlyq.cc" ;;
cloud) URL="https://cloud.tlyq.cc" ;;
token) URL="https://token.tlyq.cc" ;;
esac
STATUS=$(ssh tgz "curl -s -o /dev/null -w '%{http_code}' -k '$URL'")
if [ "$STATUS" = "200" ]; then
log "部署成功!访问 $URL"
else
warn "返回状态码: $STATUS,请检查"
fi