Files
doc_ai_frontend/deploy.sh
2026-01-26 07:10:58 +08:00

146 lines
3.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
# Document AI Frontend 部署脚本
# 功能:构建项目并部署到 ecs 服务器
set -e # 遇到错误立即退出
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 服务器配置
ECS_HOST="ecs"
DEPLOY_PATH="/texpixel"
# 打印带颜色的消息
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 检查命令是否存在
check_command() {
if ! command -v $1 &> /dev/null; then
print_error "$1 命令未找到,请先安装"
exit 1
fi
}
# 部署到服务器
deploy_to_server() {
local server=$1
print_info "开始部署到 ${server}..."
# 上传构建产物
print_info "上传 dist 目录到 ${server}..."
if scp -r dist ${server}:~ > /dev/null 2>&1; then
print_success "文件上传成功"
else
print_error "文件上传失败"
return 1
fi
# SSH 执行部署操作
print_info "${server} 上执行部署操作..."
ssh ${server} << EOF
set -e
cd ${DEPLOY_PATH}
# 备份旧版本
if [ -d "dist" ]; then
echo "备份旧版本..."
rm -rf dist_bak/
mv dist dist_bak
fi
# 移动新版本
if [ -d ~/dist ]; then
mv ~/dist .
echo "部署完成!"
else
echo "错误:找不到 ~/dist 目录"
exit 1
fi
# 重新加载 nginx如果配置了
if command -v nginx &> /dev/null; then
echo "重新加载 nginx..."
nginx -t && nginx -s reload || echo "警告nginx 重新加载失败,请手动检查"
fi
EOF
if [ $? -eq 0 ]; then
print_success "${server} 部署成功!"
else
print_error "${server} 部署失败!"
return 1
fi
}
# 主函数
main() {
print_info "=========================================="
print_info "Document AI Frontend 部署脚本"
print_info "=========================================="
echo ""
# 检查必要的命令
print_info "检查环境..."
check_command "npm"
check_command "scp"
check_command "ssh"
# 步骤1: 构建项目
print_info "步骤 1/2: 构建项目..."
if npm run build; then
print_success "构建完成!"
else
print_error "构建失败!"
exit 1
fi
echo ""
# 检查 dist 目录是否存在
if [ ! -d "dist" ]; then
print_error "dist 目录不存在,构建可能失败"
exit 1
fi
# 步骤2: 部署到 ecs
print_info "步骤 2/2: 部署到 ecs..."
if deploy_to_server ${ECS_HOST}; then
print_success "ecs 部署完成"
else
print_error "ecs 部署失败"
exit 1
fi
echo ""
# 完成
print_info "清理临时文件..."
# 可以选择是否删除本地的 dist 目录
# rm -rf dist
print_success "=========================================="
print_success "部署完成!"
print_success "=========================================="
}
# 运行主函数
main