netlify-cli-and-deploy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNetlify 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 statusFor CI, set environment variable instead.
NETLIFY_AUTH_TOKENbash
netlify login # 打开浏览器进行OAuth认证
netlify status # 检查认证状态及关联站点情况在CI环境中,可通过设置环境变量来完成认证。
NETLIFY_AUTH_TOKENLinking a Site
关联站点
Check if already linked with . If not:
netlify statusbash
undefined使用检查是否已关联站点。若未关联:
netlify statusbash
undefinedInteractive
交互式关联
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 . Automatic deploys trigger on push/PR:
netlify init- 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使用完成配置。推送代码或创建PR时会自动触发部署:
netlify init- 推送到生产分支 → 生产环境部署
- 打开PR → 生成带唯一URL的部署预览
- 推送到其他分支 → 分支环境部署
构建流程在Netlify服务器上运行。可在中配置构建设置。
netlify.tomlManual / 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 directoryThis 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 devWraps 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-plugintypescript
// vite.config.ts
import netlify from "@netlify/vite-plugin";
export default defineConfig({ plugins: [netlify()] });Then run your normal dev command () — no wrapper needed. This gives you access to Blobs, DB, Functions, and environment variables during development.
npm run devnetlify devSee the netlify-frameworks skill for framework-specific local dev guidance.
对于使用Vite的项目(如React单页应用、TanStack Start、SvelteKit、Remix),Vite插件可在框架开发服务器中直接提供Netlify平台原语支持:
bash
npm install @netlify/vite-plugintypescript
// vite.config.ts
import netlify from "@netlify/vite-plugin";
export default defineConfig({ plugins: [netlify()] });之后运行常规的开发命令()即可,无需使用封装。此方式可在开发过程中访问Blobs、数据库、函数及环境变量。
npm run devnetlify dev如需框架专属的本地开发指导,请查看netlify-frameworks技能文档。
Environment Variables
环境变量
CLI Management
CLI管理
bash
undefinedbash
undefinedSet
设置变量
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
undefinednetlify env:unset API_KEY
undefinedContext 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-xAccessing in Code
代码中访问变量
- Server-side (Functions): Use (preferred) or
Netlify.env.get("VAR")process.env.VAR - Client-side (Vite): Only -prefixed vars via
VITE_import.meta.env.VITE_VAR - Client-side (Astro): Only -prefixed vars via
PUBLIC_import.meta.env.PUBLIC_VAR
Never use or prefix for secrets — these are exposed to the browser.
VITE_PUBLIC_- 服务端(函数):推荐使用,或使用
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
常用命令
| Command | Description |
|---|---|
| Auth and site link status |
| Start local dev server |
| Run build locally (mimics Netlify environment) |
| Draft deploy |
| Production deploy |
| Run command with Netlify environment loaded |
| List environment variables |
| Clone, link, and set up in one step |
| 命令 | 描述 |
|---|---|
| 查看认证状态及站点关联情况 |
| 启动本地开发服务器 |
| 在本地运行构建(模拟Netlify服务器环境) |
| 草稿部署 |
| 生产环境部署 |
| 加载Netlify环境变量后运行指定命令 |
| 列出所有环境变量 |
| 一键完成仓库克隆、站点关联及配置 |