Files
photography/.gitea/workflows/deploy-frontend.yml
xujiang 34ac109f93
Some checks failed
部署管理后台 / 🚀 部署到生产环境 (push) Failing after 1m59s
部署管理后台 / 🔒 安全扫描 (push) Successful in 1m43s
部署管理后台 / 🧪 测试和构建 (push) Successful in 1m48s
部署前端网站 / 🧪 测试和构建 (push) Successful in 3m32s
部署前端网站 / 🚀 部署到生产环境 (push) Failing after 4m52s
feat: 优化部署工作流,使用新Action简化上传和解压流程
- 在`deploy-admin.yml`中,使用`appleboy/scp-action`和`appleboy/ssh-action`替代sshpass,简化文件上传和解压步骤
- 在`deploy-frontend.yml`中,更新文件上传和权限设置步骤,提升部署效率
- 增强了健康检查脚本的可读性和一致性

此更改提升了部署的效率和可靠性。
2025-07-16 17:31:49 +08:00

149 lines
4.4 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 }}
key: ${{ secrets.ALIYUN_SSH_KEY }}
port: 22
source: "frontend/out/*"
target: "/home/gitea/www/photography/"
strip_components: 2
rm: true
- name: 🔧 设置文件权限
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.ALIYUN_IP }}
username: ${{ secrets.ALIYUN_USER_NAME }}
key: ${{ secrets.ALIYUN_SSH_KEY }}
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
sudo systemctl reload caddy
echo "✅ 前端部署完成!"
- name: 🔍 健康检查
run: |
echo "🔍 执行健康检查..."
sleep 5
# 快速健康检查
if curl -f -s -o /dev/null --max-time 10 https://photography.iriver.top; then
echo "✅ 前端网站访问正常"
else
echo "⚠️ 前端网站访问异常,请手动检查"
fi