- 修改了目标目录为临时目录`/tmp/frontend-build`,并在部署后将文件移动到生产目录`/home/gitea/www/photography/`。 - 新增了创建目标目录、清空旧文件、移动新文件、清理临时文件和验证部署结果的步骤,提升了部署的可靠性和可维护性。 此更改增强了前端部署的流程和文件管理。
192 lines
5.8 KiB
YAML
192 lines
5.8 KiB
YAML
name: 部署前端网站
|
|
|
|
on:
|
|
push:
|
|
branches: [ main ]
|
|
paths:
|
|
- 'frontend/**'
|
|
- '.gitea/workflows/deploy-frontend.yml'
|
|
workflow_dispatch:
|
|
|
|
env:
|
|
BUN_VERSION: 'latest'
|
|
CACHE_KEY: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
|
|
|
|
jobs:
|
|
test-and-build:
|
|
name: 🧪 测试和构建
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: 📥 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 🦀 设置 Bun 环境
|
|
uses: oven-sh/setup-bun@v1
|
|
with:
|
|
bun-version: ${{ env.BUN_VERSION }}
|
|
|
|
- name: 💾 缓存 Bun 依赖
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: |
|
|
~/.bun/install/cache
|
|
frontend/node_modules
|
|
key: ${{ env.CACHE_KEY }}-${{ hashFiles('frontend/bun.lock') }}
|
|
restore-keys: |
|
|
${{ env.CACHE_KEY }}-
|
|
|
|
- name: 📦 安装依赖
|
|
working-directory: ./frontend
|
|
run: bun install --frozen-lockfile
|
|
|
|
- name: 🏗️ 并行构建和检查
|
|
working-directory: ./frontend
|
|
run: |
|
|
# 并行执行代码检查和构建
|
|
bun run lint &
|
|
bun run type-check &
|
|
wait
|
|
env:
|
|
NEXT_PUBLIC_API_URL: https://api.photography.iriver.top
|
|
NEXT_PUBLIC_SITE_URL: https://photography.iriver.top
|
|
NEXT_PUBLIC_SITE_NAME: 摄影作品集
|
|
|
|
- name: 🏗️ 构建生产版本
|
|
working-directory: ./frontend
|
|
env:
|
|
NEXT_PUBLIC_API_URL: https://api.photography.iriver.top
|
|
NEXT_PUBLIC_SITE_URL: https://photography.iriver.top
|
|
NEXT_PUBLIC_SITE_NAME: 摄影作品集
|
|
run: bun run build
|
|
|
|
- name: 📦 打包构建产物
|
|
uses: actions/upload-artifact@v3
|
|
with:
|
|
name: frontend-dist-${{ github.sha }}
|
|
path: frontend/out/
|
|
retention-days: 1
|
|
|
|
deploy:
|
|
name: 🚀 部署到生产环境
|
|
runs-on: ubuntu-latest
|
|
needs: test-and-build
|
|
if: github.ref == 'refs/heads/main'
|
|
|
|
steps:
|
|
- name: 📥 检出代码
|
|
uses: actions/checkout@v4
|
|
|
|
- name: 🦀 设置 Bun 环境
|
|
uses: oven-sh/setup-bun@v1
|
|
with:
|
|
bun-version: ${{ env.BUN_VERSION }}
|
|
|
|
- name: 💾 缓存 Bun 依赖
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: |
|
|
~/.bun/install/cache
|
|
frontend/node_modules
|
|
key: ${{ env.CACHE_KEY }}-${{ hashFiles('frontend/bun.lock') }}
|
|
|
|
- name: 📦 安装依赖
|
|
working-directory: ./frontend
|
|
run: bun install --frozen-lockfile
|
|
|
|
- name: 🏗️ 构建生产版本
|
|
working-directory: ./frontend
|
|
env:
|
|
NEXT_PUBLIC_API_URL: https://api.photography.iriver.top
|
|
NEXT_PUBLIC_SITE_URL: https://photography.iriver.top
|
|
NEXT_PUBLIC_SITE_NAME: 摄影作品集
|
|
run: bun run build
|
|
|
|
- name: 📤 上传文件到服务器
|
|
uses: appleboy/scp-action@v0.1.6
|
|
with:
|
|
host: ${{ secrets.ALIYUN_IP }}
|
|
username: ${{ secrets.ALIYUN_USER_NAME }}
|
|
password: ${{ secrets.ALIYUN_PWD }}
|
|
port: 22
|
|
source: "frontend/out/"
|
|
target: "/tmp/frontend-build"
|
|
rm: true
|
|
|
|
- name: 🔄 部署文件到生产目录
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.ALIYUN_IP }}
|
|
username: ${{ secrets.ALIYUN_USER_NAME }}
|
|
password: ${{ secrets.ALIYUN_PWD }}
|
|
port: 22
|
|
script: |
|
|
echo "🔄 部署文件到生产目录..."
|
|
|
|
# 创建目标目录
|
|
mkdir -p /home/gitea/www/photography
|
|
|
|
# 清空旧文件
|
|
rm -rf /home/gitea/www/photography/*
|
|
|
|
# 移动新文件到生产目录
|
|
cp -r /tmp/frontend-build/frontend/out/* /home/gitea/www/photography/ || exit 1
|
|
|
|
# 清理临时文件
|
|
rm -rf /tmp/frontend-build
|
|
|
|
# 验证部署结果
|
|
echo "📋 验证部署文件..."
|
|
ls -la /home/gitea/www/photography/ | head -10
|
|
|
|
- name: 🔧 设置文件权限
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.ALIYUN_IP }}
|
|
username: ${{ secrets.ALIYUN_USER_NAME }}
|
|
password: ${{ secrets.ALIYUN_PWD }}
|
|
port: 22
|
|
script: |
|
|
echo "🔧 设置文件权限..."
|
|
|
|
# 设置所有者
|
|
chown -R gitea:gitea /home/gitea/www/photography
|
|
|
|
# 设置权限
|
|
chmod -R 755 /home/gitea/www/photography
|
|
find /home/gitea/www/photography -type f \( -name '*.html' -o -name '*.js' -o -name '*.css' -o -name '*.json' \) -exec chmod 644 {} \;
|
|
|
|
# 重新加载 Caddy
|
|
if command -v sudo &> /dev/null; then
|
|
sudo systemctl reload caddy
|
|
else
|
|
systemctl reload caddy || true
|
|
fi
|
|
|
|
echo "✅ 前端部署完成!"
|
|
|
|
- name: 🔍 健康检查
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.ALIYUN_IP }}
|
|
username: ${{ secrets.ALIYUN_USER_NAME }}
|
|
password: ${{ secrets.ALIYUN_PWD }}
|
|
port: 22
|
|
script: |
|
|
echo "🔍 执行健康检查..."
|
|
|
|
# 检查文件是否存在
|
|
if [ -f '/home/gitea/www/photography/index.html' ]; then
|
|
echo '✅ index.html 文件存在'
|
|
else
|
|
echo '❌ index.html 文件不存在'
|
|
exit 1
|
|
fi
|
|
|
|
# 快速检查
|
|
sleep 3
|
|
if curl -f -s -o /dev/null https://photography.iriver.top; then
|
|
echo '✅ 前端网站访问正常'
|
|
else
|
|
echo '⚠️ 前端网站访问异常,请手动检查'
|
|
fi |