netlify-cli-and-deploy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Netlify CLI and Deployment

Netlify CLI 与站点部署

Installation

安装

bash
npm install -g netlify-cli    # Global (for local dev)
npm install netlify-cli -D    # Local (for CI)
Requires Node.js 18.14.0+.
bash
npm install -g netlify-cli    # 全局安装(用于本地开发)
npm install netlify-cli -D    # 本地安装(用于CI环境)
要求 Node.js 版本 18.14.0 及以上。

Authentication

身份验证

bash
netlify login       # Opens browser for OAuth
netlify status      # Check auth + linked site status
For CI, set
NETLIFY_AUTH_TOKEN
environment variable instead.
bash
netlify login       # 打开浏览器进行OAuth认证
netlify status      # 检查认证状态及关联站点情况
在CI环境中,可通过设置
NETLIFY_AUTH_TOKEN
环境变量来完成认证。

Linking a Site

关联站点

Check if already linked with
netlify status
. If not:
bash
undefined
使用
netlify status
检查是否已关联站点。若未关联:
bash
undefined

Interactive

交互式关联

netlify link
netlify link

By Git remote (if using Git)

通过Git远程仓库关联(使用Git时)

netlify link --git-remote-url https://github.com/org/repo
netlify link --git-remote-url https://github.com/org/repo

Create new site

创建新站点

netlify init # With Git CI/CD setup netlify init --manual # Without Git CI/CD

Site ID is stored in `.netlify/state.json`. Add `.netlify` to `.gitignore`.
netlify init # 配置Git CI/CD流程 netlify init --manual # 不配置Git CI/CD流程

站点ID会存储在`.netlify/state.json`文件中。请将`.netlify`目录添加到`.gitignore`忽略列表。

Deploying

部署

Git-Based Deploys (Continuous Deployment)

基于Git的部署(持续部署)

Set up with
netlify init
. Automatic deploys trigger on push/PR:
  • Push to production branch → production deploy
  • Open PR → deploy preview with unique URL
  • Push to other branches → branch deploy
Build runs on Netlify's servers. Configure build settings in
netlify.toml
.
使用
netlify init
完成配置。推送代码或创建PR时会自动触发部署:
  • 推送到生产分支 → 生产环境部署
  • 打开PR → 生成带唯一URL的部署预览
  • 推送到其他分支 → 分支环境部署
构建流程在Netlify服务器上运行。可在
netlify.toml
中配置构建设置。

Manual / Local Deploys (No Git Required)

手动/本地部署(无需Git)

Build locally, then upload:
bash
netlify deploy          # Draft deploy (preview URL)
netlify deploy --prod   # Production deploy
netlify deploy --dir=dist  # Specify output directory
This works without Git — useful for prototypes, local-only projects, or CI pipelines.
先在本地完成构建,再上传部署:
bash
netlify deploy          # 草稿部署(生成预览URL)
netlify deploy --prod   # 生产环境部署
netlify deploy --dir=dist  # 指定输出目录
该方式无需依赖Git,适用于原型开发、仅本地运行的项目或CI流水线场景。

Local Development

本地开发

Option 1: netlify dev

方式1:netlify dev

bash
netlify dev
Wraps your framework's dev server and provides:
  • Environment variable injection
  • Functions and edge functions
  • Redirects and headers processing
bash
netlify dev
该命令会封装框架的开发服务器,并提供以下功能:
  • 环境变量注入
  • 函数与边缘函数支持
  • 重定向与请求头处理

Option 2: Netlify Vite Plugin (Vite-based projects)

方式2:Netlify Vite 插件(基于Vite的项目)

For projects using Vite (React SPA, TanStack Start, SvelteKit, Remix), the Vite plugin provides Netlify platform primitives directly in the framework's dev server:
bash
npm install @netlify/vite-plugin
typescript
// vite.config.ts
import netlify from "@netlify/vite-plugin";
export default defineConfig({ plugins: [netlify()] });
Then run your normal dev command (
npm run dev
) — no
netlify dev
wrapper needed. This gives you access to Blobs, DB, Functions, and environment variables during development.
See the netlify-frameworks skill for framework-specific local dev guidance.
对于使用Vite的项目(如React单页应用、TanStack Start、SvelteKit、Remix),Vite插件可在框架开发服务器中直接提供Netlify平台原语支持:
bash
npm install @netlify/vite-plugin
typescript
// vite.config.ts
import netlify from "@netlify/vite-plugin";
export default defineConfig({ plugins: [netlify()] });
之后运行常规的开发命令(
npm run dev
)即可,无需使用
netlify dev
封装。此方式可在开发过程中访问Blobs、数据库、函数及环境变量。
如需框架专属的本地开发指导,请查看netlify-frameworks技能文档。

Environment Variables

环境变量

CLI Management

CLI管理

bash
undefined
bash
undefined

Set

设置变量

netlify env:set API_KEY "value" netlify env:set API_KEY "value" --secret # Hidden from logs netlify env:set API_KEY "value" --context production # Context-specific
netlify env:set API_KEY "value" netlify env:set API_KEY "value" --secret # 变量内容会在日志中隐藏 netlify env:set API_KEY "value" --context production # 按部署环境设置变量

Get

获取变量

netlify env:get API_KEY
netlify env:get API_KEY

List

列出变量

netlify env:list netlify env:list --plain > .env # Export to file
netlify env:list netlify env:list --plain > .env # 导出到.env文件

Import from file

从文件导入

netlify env:import .env
netlify env:import .env

Delete

删除变量

netlify env:unset API_KEY
undefined
netlify env:unset API_KEY
undefined

Context Scoping

环境作用域

Variables can be scoped to deploy contexts:
bash
netlify env:set API_URL "https://api.prod.com" --context production
netlify env:set API_URL "https://api.staging.com" --context deploy-preview
netlify env:set DEBUG "true" --context branch:feature-x
变量可按部署环境设置作用域:
bash
netlify env:set API_URL "https://api.prod.com" --context production
netlify env:set API_URL "https://api.staging.com" --context deploy-preview
netlify env:set DEBUG "true" --context branch:feature-x

Accessing in Code

代码中访问变量

  • Server-side (Functions): Use
    Netlify.env.get("VAR")
    (preferred) or
    process.env.VAR
  • Client-side (Vite): Only
    VITE_
    -prefixed vars via
    import.meta.env.VITE_VAR
  • Client-side (Astro): Only
    PUBLIC_
    -prefixed vars via
    import.meta.env.PUBLIC_VAR
Never use
VITE_
or
PUBLIC_
prefix for secrets
— these are exposed to the browser.
  • 服务端(函数):推荐使用
    Netlify.env.get("VAR")
    ,或使用
    process.env.VAR
  • 客户端(Vite):仅支持带有
    VITE_
    前缀的变量,通过
    import.meta.env.VITE_VAR
    访问
  • 客户端(Astro):仅支持带有
    PUBLIC_
    前缀的变量,通过
    import.meta.env.PUBLIC_VAR
    访问
请勿为敏感变量添加
VITE_
PUBLIC_
前缀
——这些变量会暴露给浏览器。

Useful Commands

常用命令

CommandDescription
netlify status
Auth and site link status
netlify dev
Start local dev server
netlify build
Run build locally (mimics Netlify environment)
netlify deploy
Draft deploy
netlify deploy --prod
Production deploy
netlify dev:exec <cmd>
Run command with Netlify environment loaded
netlify env:list
List environment variables
netlify clone org/repo
Clone, link, and set up in one step
命令描述
netlify status
查看认证状态及站点关联情况
netlify dev
启动本地开发服务器
netlify build
在本地运行构建(模拟Netlify服务器环境)
netlify deploy
草稿部署
netlify deploy --prod
生产环境部署
netlify dev:exec <cmd>
加载Netlify环境变量后运行指定命令
netlify env:list
列出所有环境变量
netlify clone org/repo
一键完成仓库克隆、站点关联及配置