Files
photography/.gitea/workflows/deploy-frontend.yml
xujiang b35ee56848
Some checks failed
部署后端服务 / 🚀 构建并部署 (push) Failing after 41s
部署前端网站 / 🧪 测试和构建 (push) Successful in 2m58s
部署后端服务 / 🔄 回滚部署 (push) Failing after 2s
部署前端网站 / 🚀 部署到生产环境 (push) Failing after 4m13s
feat: 优化后端CI/CD配置,使用生产环境PostgreSQL凭据
- 添加生产环境docker-compose.prod.yml,移除PostgreSQL和Redis容器
- 更新CI/CD工作流使用secrets.POSTGRES_PHOTO_USER和secrets.POSTGRES_PHOTO_PWD
- 修复服务名称从backend改为api以匹配配置
- 自动创建生产环境.env文件并注入正确的数据库凭据

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-14 18:08:52 +08:00

138 lines
4.8 KiB
YAML
Raw 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.

name: 部署前端网站
on:
push:
branches: [ main ]
paths:
- 'frontend/**'
- '.gitea/workflows/deploy-frontend.yml'
workflow_dispatch:
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: latest
- name: 📦 安装依赖
working-directory: ./frontend
run: bun install
- name: 🔍 代码检查
working-directory: ./frontend
run: |
bun run lint
bun run type-check
- 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
path: frontend/out/
retention-days: 7
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: latest
- name: 📦 安装依赖
working-directory: ./frontend
run: bun install
- 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: 🚀 部署到服务器
run: |
# 安装部署工具
sudo apt-get update && sudo apt-get install -y openssh-client rsync sshpass
# 设置 SSH 环境
export SSHPASS=${{ secrets.ALIYUN_PWD }}
echo "🔗 测试 SSH 连接..."
sshpass -e ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "echo 'SSH 连接成功'"
echo "📁 创建部署目录..."
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "mkdir -p /home/gitea/www/photography"
echo "📦 备份当前版本..."
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "
if [ -d '/home/gitea/www/photography' ] && [ \"\$(ls -A /home/gitea/www/photography)\" ]; then
mkdir -p /home/gitea/backups/photography-frontend
tar -czf /home/gitea/backups/photography-frontend/frontend-\$(date +%Y%m%d-%H%M%S).tar.gz -C /home/gitea/www/photography .
echo '✅ 备份完成'
fi
"
echo "🚀 部署新版本..."
sshpass -e rsync -avz --delete --progress -e "ssh -o StrictHostKeyChecking=no" frontend/out/ ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }}:/home/gitea/www/photography/
echo "🔐 设置文件权限..."
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "
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' | xargs chmod 644
"
echo "🔄 重新加载 Web 服务器..."
sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} "sudo systemctl reload caddy"
echo "✅ 前端部署完成!"
echo "📁 部署路径:/home/gitea/www/photography/"
echo "🌐 访问地址https://photography.iriver.top"
- name: 🔍 健康检查
run: |
echo "🔍 执行健康检查..."
sleep 10
# 检查网站是否可访问
if curl -f -s -o /dev/null https://photography.iriver.top; then
echo "✅ 前端网站访问正常"
else
echo "⚠️ 前端网站访问异常"
exit 1
fi
- name: 📧 发送部署通知
if: always()
run: |
if [ "${{ job.status }}" = "success" ]; then
echo "✅ 摄影作品集前端部署成功!"
echo "🌐 访问地址: https://photography.iriver.top"
else
echo "❌ 摄影作品集前端部署失败!"
fi