第一个项目部署 #

部署方式概览 #

Vercel 提供多种部署方式:

text
┌─────────────────────────────────────────┐
│           Vercel 部署方式                │
├─────────────────────────────────────────┤
│  Git 集成   →  自动部署,推荐方式        │
│  CLI 部署   →  命令行,灵活控制          │
│  API 部署   →  编程方式,自动化          │
│  拖拽上传   →  静态文件,快速部署        │
└─────────────────────────────────────────┘

方式一:Git 集成部署(推荐) #

步骤 1:准备项目 #

确保你的项目满足以下条件:

text
项目结构示例:
my-project/
├── package.json        # 依赖配置
├── src/               # 源代码
│   └── index.js
├── public/            # 静态资源
└── README.md

package.json 示例:

json
{
  "name": "my-project",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "^14.0.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}

步骤 2:推送到 Git #

bash
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/username/my-project.git
git push -u origin main

步骤 3:导入项目 #

  1. 登录 Vercel Dashboard
  2. 点击 “Add New…” → “Project”
text
┌─────────────────────────────────────────┐
│  Import Git Repository                  │
├─────────────────────────────────────────┤
│  ┌─────────────────────────────────┐    │
│  │ 🔍 Search repositories...       │    │
│  └─────────────────────────────────┘    │
│                                         │
│  Recent Repositories                    │
│  ┌─────────────────────────────────┐    │
│  │ username/my-project      [Import]│   │
│  └─────────────────────────────────┘    │
└─────────────────────────────────────────┘

步骤 4:配置项目 #

text
┌─────────────────────────────────────────┐
│  Configure Project                      │
├─────────────────────────────────────────┤
│  Project Name: my-project               │
│                                         │
│  Framework Preset: [Next.js ▼]          │
│                                         │
│  Root Directory: [./ ▼]                 │
│                                         │
│  Build Command: [npm run build]         │
│  Output Directory: [.next]              │
│  Install Command: [npm install]         │
│                                         │
│  [Deploy]                               │
└─────────────────────────────────────────┘

步骤 5:等待构建 #

text
┌─────────────────────────────────────────┐
│  Building...                            │
├─────────────────────────────────────────┤
│  ✓ Cloning repository                   │
│  ✓ Installing dependencies              │
│  ○ Building application                 │
│  · Optimizing assets                    │
│                                         │
│  [View Build Logs]                      │
└─────────────────────────────────────────┘

步骤 6:部署成功 #

text
┌─────────────────────────────────────────┐
│  🎉 Deployment Ready!                   │
├─────────────────────────────────────────┤
│  Production: https://my-project.vercel.app
│                                         │
│  [Visit] [View Build Logs]              │
└─────────────────────────────────────────┘

方式二:CLI 部署 #

安装 CLI #

bash
npm i -g vercel

登录 #

bash
vercel login

部署项目 #

bash
cd my-project
vercel
text
? Set up and deploy "~/my-project"? [Y/n] y
? Which scope do you want to deploy to? username
? Link to existing project? [y/N] n
? What's your project's name? my-project
? In which directory is your code located? ./
? Want to modify these settings? [y/N] n

🔗  Linked to username/my-project
🚀  Deployed to production in 1m 23s

📝  https://my-project.vercel.app

部署到生产环境 #

bash
vercel --prod

常用 CLI 命令 #

命令 说明
vercel 部署预览环境
vercel --prod 部署生产环境
vercel list 查看部署列表
vercel logs [url] 查看日志
vercel inspect [url] 查看部署详情
vercel domains 管理域名
vercel env 管理环境变量
vercel pull 拉取环境变量

方式三:拖拽上传 #

适合纯静态网站快速部署:

步骤 #

  1. 访问 vercel.com/new
  2. 选择 “Continue with a template” 或拖拽文件夹
text
┌─────────────────────────────────────────┐
│  Drop files here to deploy              │
│                                         │
│     ┌───────────────────────────┐       │
│     │                           │       │
│     │    📁 Drag & Drop         │       │
│     │                           │       │
│     └───────────────────────────┘       │
│                                         │
│  Supported: HTML, CSS, JS, Images       │
└─────────────────────────────────────────┘

框架预设配置 #

Vercel 自动识别主流框架:

Next.js #

json
{
  "buildCommand": "next build",
  "outputDirectory": ".next",
  "framework": "nextjs"
}

Create React App #

json
{
  "buildCommand": "react-scripts build",
  "outputDirectory": "build",
  "framework": "create-react-app"
}

Vue.js #

json
{
  "buildCommand": "vue-cli-service build",
  "outputDirectory": "dist",
  "framework": "vue"
}

Nuxt.js #

json
{
  "buildCommand": "nuxt build",
  "outputDirectory": ".output",
  "framework": "nuxtjs"
}

Vite #

json
{
  "buildCommand": "vite build",
  "outputDirectory": "dist",
  "framework": "vite"
}

静态站点 #

json
{
  "buildCommand": null,
  "outputDirectory": "public",
  "framework": null
}

查看部署详情 #

部署列表 #

text
Project → Deployments
text
┌─────────────────────────────────────────────────────────┐
│  Deployments                                            │
├─────────────────────────────────────────────────────────┤
│  Production                                             │
│  ├── abc123  main  2 hours ago  ✓ Ready  [Visit]       │
│  └── def456  main  1 day ago    ✓ Ready  [Visit]       │
│                                                         │
│  Preview                                                │
│  └── ghi789  feat/x  30 mins ago  ✓ Ready  [Visit]     │
└─────────────────────────────────────────────────────────┘

构建日志 #

text
Deployment → Build Logs
text
[00:00:00.000] Cloning repository...
[00:00:01.234] Cloning completed: 234ms
[00:00:01.500] Running "vercel build"
[00:00:02.000] Detecting framework...
[00:00:02.100] Framework detected: Next.js
[00:00:02.200] Installing dependencies...
[00:00:15.300] npm install completed
[00:00:15.400] Running "npm run build"
[00:00:45.600] Build completed
[00:00:46.000] Deploying to edge network...
[00:01:00.000] Deployment ready

函数日志 #

text
Deployment → Functions
text
┌─────────────────────────────────────────┐
│  Function: /api/hello                   │
├─────────────────────────────────────────┤
│  2024-01-15 10:30:00  Request received  │
│  2024-01-15 10:30:00  Processing...     │
│  2024-01-15 10:30:01  Response sent     │
└─────────────────────────────────────────┘

回滚部署 #

通过 Dashboard #

text
Deployments → 选择历史部署 → Promote to Production

通过 CLI #

bash
vercel rollback [deployment-url]

删除项目 #

text
Project Settings → General → Delete Project

⚠️ 注意:删除项目将同时删除所有部署和域名配置。

常见问题 #

构建失败 #

text
常见原因:
├── 依赖安装失败 → 检查 package.json
├── 构建命令错误 → 检查 Build Command
├── 内存不足 → 升级计划或优化构建
└── 环境变量缺失 → 配置环境变量

部署超时 #

text
解决方案:
├── 优化构建速度
├── 减少依赖数量
├── 使用构建缓存
└── 检查网络问题

404 错误 #

text
检查项:
├── Output Directory 是否正确
├── 路由配置是否正确
├── 静态文件是否生成
└── vercel.json 配置

下一步 #

成功部署第一个项目后,接下来学习 部署配置详解 深入了解 Vercel 的配置选项!

最后更新:2026-03-28