127 lines
3.7 KiB
YAML
127 lines
3.7 KiB
YAML
name: Deploy Frontend via Git
|
|
|
|
# 触发条件
|
|
on:
|
|
# 当推送到主分支且修改前端相关文件时自动触发
|
|
push:
|
|
branches: [ main ]
|
|
paths:
|
|
- 'frontend/**'
|
|
- '.github/workflows/deploy_frontend.git.yml'
|
|
# 支持手动触发
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
build-and-deploy:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
# 检出代码
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v4
|
|
|
|
# 设置Node.js环境
|
|
- name: Set up Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '18'
|
|
cache: 'npm'
|
|
cache-dependency-path: frontend/package-lock.json
|
|
|
|
# 缓存node_modules和构建输出
|
|
- name: Cache dependencies and build
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: |
|
|
frontend/node_modules
|
|
frontend/production
|
|
key: ${{ runner.os }}-frontend-${{ hashFiles('frontend/package-lock.json', 'frontend/src/**/*', 'frontend/vite.config.ts') }}
|
|
restore-keys: |
|
|
${{ runner.os }}-frontend-${{ hashFiles('frontend/package-lock.json') }}
|
|
${{ runner.os }}-frontend-
|
|
|
|
# 安装依赖
|
|
- name: Install dependencies
|
|
run: |
|
|
cd frontend
|
|
npm ci
|
|
|
|
# 并行执行类型检查和构建
|
|
- name: Type check and build
|
|
run: |
|
|
cd frontend
|
|
# 并行执行类型检查和构建以节省时间
|
|
npm run type-check &
|
|
npm run build:prod &
|
|
wait
|
|
# 检查构建结果
|
|
echo "Build completed, checking production directory:"
|
|
ls -la production/
|
|
|
|
# 预处理部署目录权限
|
|
- name: Prepare deployment directory
|
|
uses: appleboy/ssh-action@v1.0.3
|
|
with:
|
|
host: ${{ secrets.HOST_PI }}
|
|
username: pi
|
|
password: ${{ secrets.PASS_PI }}
|
|
port: 22
|
|
script: |
|
|
echo "Preparing deployment directory..."
|
|
|
|
# 停止nginx避免文件占用
|
|
sudo nginx -s quit 2>/dev/null || true
|
|
|
|
# 清理旧文件并重建目录结构
|
|
sudo rm -rf /home/pi/lm-www/frontend
|
|
sudo mkdir -p /home/pi/lm-www/frontend
|
|
|
|
# 设置正确的目录权限供文件传输
|
|
sudo chown -R pi:pi /home/pi/lm-www/frontend
|
|
sudo chmod -R 755 /home/pi/lm-www/frontend
|
|
|
|
# 将构建产物传输到服务器
|
|
- name: Copy build files to server
|
|
uses: appleboy/scp-action@v0.1.7
|
|
with:
|
|
host: ${{ secrets.HOST_PI }}
|
|
username: pi
|
|
password: ${{ secrets.PASS_PI }}
|
|
port: 22
|
|
source: "frontend/production/*"
|
|
target: "/home/pi/lm-www/"
|
|
overwrite: true
|
|
|
|
# 完成部署配置
|
|
- name: Finalize deployment
|
|
uses: appleboy/ssh-action@v1.0.3
|
|
with:
|
|
host: ${{ secrets.HOST_PI }}
|
|
username: pi
|
|
password: ${{ secrets.PASS_PI }}
|
|
port: 22
|
|
script: |
|
|
echo "Finalizing deployment..."
|
|
|
|
# 设置Web服务器权限
|
|
sudo chown -R www-data:www-data /home/pi/lm-www/frontend/
|
|
sudo chmod -R 644 /home/pi/lm-www/frontend/
|
|
sudo find /home/pi/lm-www/frontend/ -type d -exec chmod 755 {} \;
|
|
|
|
# 启动nginx并验证
|
|
echo "Starting nginx..."
|
|
if sudo nginx -t; then
|
|
sudo systemctl start nginx
|
|
sudo systemctl reload nginx
|
|
|
|
if sudo systemctl is-active --quiet nginx; then
|
|
echo "✅ Frontend deployment completed successfully!"
|
|
else
|
|
echo "❌ Nginx failed to start"
|
|
sudo systemctl status nginx --no-pager -l
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "❌ Nginx configuration test failed!"
|
|
exit 1
|
|
fi |