如何通过 GitHub Actions 在 Vercel 部署前执行 E2E 测试或代码扫描?
解读
面试官真正想验证的是:
- 你是否理解 Vercel 默认的“Git Push → Build → Deploy”流水线是黑盒,不可插拔;
- 你是否能把质量门禁(E2E/扫描)提前到“部署”之前,而不是事后补救;
- 你是否熟悉国内网络与合规场景(GitHub Actions 机时、镜像源、License 合规、数据出境)。
回答时要体现“让 Vercel 只负责部署,质量把控由 GitHub Actions 主导”的思路,并给出可落地的 YAML 片段和国内可用替代方案。
知识点
- Vercel 两种触发模式:
– Git 模式(默认):Vercel 监听 push,自动构建与部署,不可插入自定义步骤。
– CLI 模式:本地或 CI 执行 vercel build + vercel deploy --prebuilt,完全由开发者控制。 - GitHub Actions 的 workflow 事件:pull_request / push / workflow_dispatch。
- 质量门禁:E2E(Playwright/Cypress)、静态代码扫描(ESLint、SonarQube)、依赖漏洞扫描(pnpm audit / trivy)。
- 国内加速:actions 镜像(gitee-mirror、npmmirror)、Playwright 淘宝镜像、Docker 镜像源。
- 合规:SonarQube 社区版可本地化部署,避免源码出境;SARIF 结果上传 GitHub Security 面板需开启 GitHub Advanced Security(GHEC 或公共仓库免费)。
- 部署令牌:VERCEL_TOKEN(用户级)、VERCEL_ORG_ID、VERCEL_PROJECT_ID,统一存于 GitHub Environments + Required Reviewers,实现“人+机”双重审批。
- 并发与成本:GitHub 公共仓库免费 2000 分钟/月;私有仓库需团队计费,国内公司常购买 GitHub Enterprise Cloud 或自建 Runner(阿里云 ECS 按量)。
答案
总体策略:关闭 Vercel 的自动 Git 模式,改用“GitHub Actions 主导质量门禁 → 通过 CLI 手动部署”。
步骤 1:关闭自动部署
在 Vercel Dashboard → Git → Ignored Build Step 填写:
[ -n "$VERCEL_AUTOMATION" ] && exit 0 || exit 1
这样 Vercel 仍绑定 Git,但所有 push 被忽略,完全由 Actions 接管。
步骤 2:在 GitHub 仓库 Settings → Secrets and Variables 添加:
VERCEL_TOKEN、VERCEL_ORG_ID、VERCEL_PROJECT_ID,并创建 Environment production,开启 Required Reviewers,确保只有通过 E2E 与扫描的制品才能被人工审核后上线。
步骤 3:编写 .github/workflows/ci.yml(国内加速版)
name: CI + Deploy on: pull_request: branches: [main] push: branches: [main] env: PLAYWRIGHT_DOWNLOAD_HOST: https://npmmirror.com/mirrors/playwright VERCEL_AUTOMATION: true # 告诉 Vercel 忽略 jobs: lint-and-scan: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v2 with: version: 8 run_install: true - run: pnpm audit --audit-level moderate - run: pnpm lint - run: pnpm dlx @cyclonedx/cyclonedx-npm --output-format json --output-file sbom.json - name: SonarQube Scan uses: sonarsource/sonarqube-scan-action@v2 env: SONAR_TOKEN: {{ secrets.SONAR_HOST_URL }} # 内网地址,如 http://sonar.my-company.cn e2e: needs: lint-and-scan runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v2 with: version: 8 run_install: true - run: npx playwright install chromium - run: npx playwright test - if: always() uses: actions/upload-artifact@v4 with: name: playwright-report path: playwright-report/ deploy: needs: e2e runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v2 with: version: 8 run_install: true - run: pnpm build - name: Install Vercel CLI run: pnpm add -g vercel@latest - name: Pull Vercel Env run: vercel pull --yes --environment=production --token={{ secrets.VERCEL_TOKEN }} - name: Deploy run: vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }}
关键点解释
- 三个 job 顺序执行,只有 e2e 全部通过才能进入 deploy。
- 使用 vercel build + vercel deploy --prebuilt,保证本地与云端产物一致,且部署速度最快。
- 国内镜像与私有 SonarQube 避免跨境传输源码,满足合规。
- Environment + Required Reviewers 实现“技术+管理”双 gate,符合国内金融、车企的上线流程。
拓展思考
- 蓝绿与回滚:在 deploy job 中先部署到别名域名(vercel deploy --target=staging),用 GitHub Script 调用 Vercel API 做零宕机切换;若 E2E 在 staging 再次失败,可立即别名回滚。
- 增量扫描:对 monorepo 使用 nx affected:lint 与 CodeQL paths-filter,减少无效扫描,节省 GitHub Actions 机时。
- 多区域合规:若客户数据需留存在中国,可在 deploy 前加一层“同步到国内云厂商静态托管”作为灾备,Vercel 仅面向海外流量。
- 成本优化:对私有仓库,可用阿里云 ECS 自建 Runner,配合 cache 服务(如 MinIO)缓存 pnpm 与 Playwright 浏览器,降低 GitHub 计费分钟数。
- 安全:VERCEL_TOKEN 具备用户级权限,建议创建“机器用户”并仅授权目标项目;同时启用 GitHub OIDC 与 Vercel 的短期令牌交换,实现无长期 Secret 的零信任流水线。