Files
Workflow config file is invalid. Please check your config file: job deploy: Unrecognized named-value: 'env'

167 lines
6.4 KiB
YAML
Raw Permalink 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.
# =============================================================================
# PERLER LAB — GitHub Actions CI/CD 流水线
# 触发条件:推送到 master 分支 或 手动触发
# 流程:
# 1. test — 安装依赖 + 运行测试(前后端并行)
# 2. build — 构建 Docker 镜像(验证 Dockerfile 正确)
# 3. deploy — SSH 到生产服务器,git pull + docker compose up -d
# =============================================================================
name: Deploy
on:
push:
branches: [master]
workflow_dispatch:
concurrency:
# 同一分支同一工作流的运行串行执行,防止并行部署到生产服务器
# cancel-in-progress: false —— 排队等待而非取消,避免中断进行中的部署
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
env:
# 镜像标签——使用 commit SHA 保证唯一性,部署时通过 docker compose up -d --build 重建
# 若需推送镜像到 registry,在此取消注释并配置后续步骤
# REGISTRY: ghcr.io
# IMAGE_TAG: ${{ github.sha }}
jobs:
# ── 测试 ──
test:
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
project:
- name: perler-lab
# perler-lab 有 package-lock.json,使用 npm ci 保证可复现
install-cmd: npm ci
- name: perler-lab-server
# perler-lab-server 有 package-lock.json,使用 npm ci 保证可复现
install-cmd: npm ci
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: ${{ matrix.project.name }}/package-lock.json
- name: Install & Test (${{ matrix.project.name }})
working-directory: ${{ matrix.project.name }}
run: |
${{ matrix.project.install-cmd }} --no-audit --no-fund
npm test
# 类型检查:两个项目均执行 tsc --noEmit(严格模式)
- name: Typecheck (${{ matrix.project.name }})
working-directory: ${{ matrix.project.name }}
run: npm run typecheck
# 前端生产构建验证(vite build);后端 tsc 编译由 typecheck 覆盖
- name: Frontend production build
if: matrix.project.name == 'perler-lab'
working-directory: perler-lab
run: npm run build
# 构建产物校验 + 新页面路由冒烟(T-109)
# 目的:① 确认 vite build 产物存在(dist/index.html + assets JS bundle,含
# /color-chart 系列 /showcase /library 等新页面的静态打包代码);
# ② 用 vite preview(自带 SPA fallback,行为等价于生产 nginx 的
# try_files $uri $uri/ /index.html)对每个新路由做直访冒烟,
# 防止「路由直访 404」回归。
- name: Verify SPA dist & route smoke
if: matrix.project.name == 'perler-lab'
working-directory: perler-lab
run: |
set -e
# ① dist 基础产物存在性校验
[ -f dist/index.html ] || { echo "❌ dist/index.html 缺失"; exit 1; }
ls dist/assets/*.js > /dev/null 2>&1 || { echo "❌ dist/assets 无 JS bundle"; exit 1; }
echo "✅ dist 产物校验通过(index.html + JS bundle)"
# ② 启动 vite preview(模拟生产 nginx,含 SPA fallback)
npx vite preview --port 4173 --strictPort > /tmp/vite-preview.log 2>&1 &
PID=$!
trap "kill $PID 2>/dev/null || true" EXIT
for i in $(seq 1 30); do
curl -sf http://localhost:4173/ > /dev/null 2>&1 && break
sleep 0.5
done
curl -sf http://localhost:4173/ > /dev/null 2>&1 || {
echo "❌ vite preview 未能启动"; cat /tmp/vite-preview.log; exit 1; }
# ③ 新页面路由直访冒烟(nginx try_files → index.html → 200)
for route in / /chibi /color-chart /color-chart/mard /color-chart/artkal-c /showcase /library; do
code=$(curl -s -o /dev/null -w '%{http_code}' "http://localhost:4173$route")
echo "GET $route → $code"
[ "$code" = "200" ] || { echo "❌ 路由 $route 返回 $code(预期 200)"; exit 1; }
done
echo "✅ 全部新页面路由冒烟通过"
# ── 构建 Docker 镜像(验证 Dockerfile 正确性) ──
build:
needs: test
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- name: Build frontend image
run: docker build -t perler-lab-frontend:ci perler-lab
- name: Build backend image
run: docker build -t perler-lab-server:ci perler-lab-server
# ── 部署到生产服务器 ──
deploy:
needs: build
runs-on: ubuntu-latest
timeout-minutes: 30
# 需要 GitHub Secrets 配置以下密钥,否则跳过(上一步 build 仍可通过)
# 注意:if 条件中不能直接引用 secrets,通过 env 间接判断
env:
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
if: env.DEPLOY_HOST != ''
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1.1.0
with:
host: ${{ secrets.DEPLOY_HOST }}
username: ${{ secrets.DEPLOY_USER }}
key: ${{ secrets.DEPLOY_SSH_KEY }}
# 可选:用端口转发跳过跳板机
# port: ${{ secrets.DEPLOY_PORT || '22' }}
script_stop: true
script: |
set -e
echo "===== 进入部署目录 ====="
cd ${{ secrets.DEPLOY_PATH || '/opt/perler-lab' }}
echo "===== 拉取最新代码 ====="
git pull origin master
echo "===== 构建并启动服务 ====="
docker compose -f perler-lab-server/docker-compose.yml up -d --build
echo "===== 健康检查:等待后端就绪 ====="
for i in $(seq 1 12); do
if curl -sf http://localhost:8080/healthz > /dev/null 2>&1; then
echo "✅ 前端健康检查通过"
break
fi
if curl -sf http://localhost:8788/health > /dev/null 2>&1; then
echo "✅ 后端健康检查通过"
break
fi
echo "等待服务就绪($i/12)..."
sleep 5
done
echo "===== 清理旧镜像 ====="
docker image prune -f
echo "===== 部署完成 ====="