Vercel 自带 Git 集成,但在复杂场景下仍需 GitHub Actions:多项目 monorepo、自定义构建步骤、私有依赖、环境保护规则、以及需要在部署前执行额外检查(安全扫描、代码生成等)。本文提供完整的 Vercel + GitHub Actions 部署方案。
一、基础方案:Vercel CLI
# .github/workflows/deploy.yml
name: Deploy to Vercel
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
jobs:
deploy-preview:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm install --global vercel@latest
- run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
- run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
- id: deploy
run: echo "url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }})" >> $GITHUB_OUTPUT
- name: Comment Preview URL
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 Preview deployed: ${{ steps.deploy.outputs.url }}`
})
deploy-production:
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment:
name: Production
url: ${{ steps.deploy.outputs.url }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
- run: npm install --global vercel@latest
- run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
- run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
- id: deploy
run: echo "url=$(vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }})" >> $GITHUB_OUTPUT
二、多项目 Monorepo 部署
jobs:
deploy-web:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install --global vercel@latest
- working-directory: ./apps/web
run: |
vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
vercel build --token=${{ secrets.VERCEL_TOKEN }}
vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}
deploy-admin:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- working-directory: ./apps/admin
run: |
vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
vercel build --token=${{ secrets.VERCEL_TOKEN }}
vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }}
三、部署环境保护(Environment Protection)
jobs:
deploy-production:
environment:
name: Production
url: ${{ steps.deploy.outputs.url }}
steps:
- name: Deploy
run: vercel --prod --token=${{ secrets.VERCEL_TOKEN }}
配置环境保护规则:
- GitHub Repo → Settings → Environments → New environment
- Name:
Production - Protection rules:
- Required reviewers: 1(至少 1 人审批)
- Wait timer: 5 分钟(延迟部署)
- Deployment branches:
main
四、回滚策略
jobs:
rollback:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.commit_sha }}
- run: npm install --global vercel@latest
- run: vercel --prod --token=${{ secrets.VERCEL_TOKEN }}
触发方式:GitHub Actions → Run workflow → 输入 commit SHA。
五、Secrets 配置清单
| Secret | 来源 | 说明 |
|---|---|---|
VERCEL_TOKEN | Vercel Settings → Tokens | 个人访问令牌 |
VERCEL_ORG_ID | Vercel Project → Settings → General | 组织 ID |
VERCEL_PROJECT_ID | Vercel Project → Settings → General | 项目 ID |
相关阅读
- GitHub Actions 详解 — 核心概念与语法
- GitHub Actions Node.js CI — 测试流水线
- Vercel 专题 — Vercel 平台深度指南
- Next.js 专题 — Next.js App Router 部署
- React 专题 — React 19 部署兼容性
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。
「saas」更多文章
自定义域名接入 Cloudflare:CDN 加速与缓存规则配置实战
手把手讲解自定义域名接入 Cloudflare CDN 的完整流程:NS 接管、SSL/TLS 模式选择、橙云代理与灰云 DNS 的区别,并用 2024 年后的新 Cache Rules 引擎实战配置静态资源长缓存、HTML 不缓存、后台 Bypass,让边缘缓存命中率最大化。
Hugo 部署到 Cloudflare Pages 实战:从 Git 推送到全球边缘上线
详解 Cloudflare Pages 部署 Hugo 静态站的完整流程,涵盖框架预设、HUGO_VERSION 环境变量固定、构建命令、自定义域名、分支预览、_headers 缓存安全头配置及常见构建失败排查,一次推送即可全球边缘上线。
Cloudflare Workers 入门实战:在边缘运行你的第一行代码
一份面向初学者的 cloudflare workers 教程,从 V8 Isolate 运行时模型讲起,手把手演示 wrangler 初始化、路由分发、KV 存储、Cache API 缓存响应与环境变量配置,帮你在 30 分钟内部署第一个生产可用的边缘 Serverless 应用。