-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy_github_render.sh
More file actions
executable file
·157 lines (129 loc) · 4.11 KB
/
deploy_github_render.sh
File metadata and controls
executable file
·157 lines (129 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# GitHub Pages + Render 一键部署脚本
set -e
echo "🚀 开始部署到 GitHub Pages + Render..."
# 检查环境
check_environment() {
echo "📋 检查部署环境..."
# 检查是否在 Git 仓库中
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "❌ 当前目录不是 Git 仓库"
exit 1
fi
# 检查是否有未提交的更改
if ! git diff --quiet; then
echo "⚠️ 有未提交的更改,将自动添加到提交中"
fi
echo "✅ 环境检查通过"
}
# 配置前端环境
configure_frontend() {
echo "🔧 配置前端环境..."
cd frontend
# 检查是否有 .env.production 文件
if [ ! -f ".env.production" ]; then
echo "⚠️ 未找到 .env.production 文件"
echo "请确保设置了正确的 VITE_API_BASE_URL"
# 创建示例文件
cat > .env.production << EOF
VITE_API_BASE_URL=https://your-render-backend.onrender.com
EOF
echo "✅ 创建了示例 .env.production 文件,请根据实际情况修改"
fi
# 安装依赖
if [ -f "package.json" ]; then
echo "📦 安装前端依赖..."
npm install
fi
# 测试构建
echo "🔨 测试前端构建..."
npm run build
cd ..
echo "✅ 前端配置完成"
}
# 配置后端环境
configure_backend() {
echo "🔧 配置后端环境..."
cd backend-python
# 检查生产依赖
if [ ! -f "requirements_prod.txt" ]; then
echo "❌ 未找到 requirements_prod.txt"
exit 1
fi
# 测试后端启动
echo "🧪 测试后端配置..."
python3 -c "
import app
print('✅ 后端配置检查通过')
"
cd ..
echo "✅ 后端配置完成"
}
# 部署到 GitHub
deploy_to_github() {
echo "📤 部署到 GitHub..."
# 添加所有更改
git add .
# 提交更改
commit_message="Deploy to GitHub Pages + Render - $(date '+%Y-%m-%d %H:%M:%S')"
git commit -m "$commit_message" || echo "没有新的更改需要提交"
# 推送到 GitHub
git push origin main
echo "✅ 代码已推送到 GitHub"
}
# 显示部署信息
show_deployment_info() {
echo ""
echo "🎉 部署准备完成!"
echo "==================="
echo ""
echo "📋 接下来的步骤:"
echo ""
echo "1. 🎨 后端部署 (Render):"
echo " - 访问 https://render.com"
echo " - 连接 GitHub 仓库"
echo " - 创建 Web Service"
echo " - 选择 backend-python 目录"
echo " - 设置构建命令: pip install -r requirements_prod.txt"
echo " - 设置启动命令: gunicorn -w 4 -b 0.0.0.0:\$PORT app:app"
echo " - 设置环境变量:"
echo " * JWT_SECRET_KEY=your_secure_key"
echo " * FLASK_ENV=production"
echo " * CORS_ORIGINS=https://$(git config user.name).github.io"
echo " - 创建 PostgreSQL 数据库并连接"
echo ""
echo "2. 📦 前端部署 (GitHub Pages):"
echo " - 进入 GitHub 仓库设置"
echo " - 启用 GitHub Pages"
echo " - 选择 'GitHub Actions' 作为源"
echo " - GitHub Actions 会自动部署"
echo ""
echo "3. 🔗 更新 API 地址:"
echo " - 获取 Render 后端 URL"
echo " - 更新 frontend/.env.production"
echo " - 重新推送触发前端部署"
echo ""
echo "📊 预期地址:"
echo " - 前端: https://$(git config user.name).github.io/quote-web"
echo " - 后端: https://your-render-backend.onrender.com"
echo ""
echo "📚 详细指南: backend-python/docs/GITHUB_PAGES_RENDER_GUIDE.md"
echo ""
echo "💡 小贴士:"
echo " - Render 免费服务会在15分钟无活动后休眠"
echo " - 首次访问休眠的服务需要等待30秒启动"
echo " - 所有服务都完全免费使用"
echo ""
}
# 主函数
main() {
echo "🎯 GitHub Pages + Render 部署助手"
echo "================================="
check_environment
configure_frontend
configure_backend
deploy_to_github
show_deployment_info
}
# 运行主函数
main "$@"