netlify-deploy

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Netlify Deployment Skill

Netlify 部署技能

Deploy web projects to Netlify using the Netlify CLI with intelligent detection of project configuration and deployment context.
使用Netlify CLI将Web项目部署到Netlify,可智能检测项目配置和部署环境。

Overview

概述

This skill automates Netlify deployments by:
  • Verifying Netlify CLI authentication
  • Detecting project configuration and framework
  • Linking to existing sites or creating new ones
  • Deploying to production or preview environments
本技能通过以下方式自动化Netlify部署:
  • 验证Netlify CLI身份认证状态
  • 检测项目配置和框架
  • 关联现有站点或创建新站点
  • 部署到生产或预览环境

Prerequisites

前提条件

  • Netlify CLI: Installed via npx (no global install required)
  • Authentication: Netlify account with active login session
  • Project: Valid web project in current directory
  • When sandboxing blocks the deployment network calls, rerun with
    sandbox_permissions=require_escalated
    .
  • The deployment might take a few minutes. Use appropriate timeout values.
  • Netlify CLI:通过npx安装(无需全局安装)
  • 身份认证:处于登录状态的Netlify账户
  • 项目:当前目录下的有效Web项目
  • 当沙箱阻止部署网络调用时,使用
    sandbox_permissions=require_escalated
    重新运行。
  • 部署可能需要几分钟,请设置合适的超时时间。

Authentication Pattern

身份认证模式

The skill uses the pre-authenticated Netlify CLI approach:
  1. Check authentication status with
    npx netlify status
  2. If not authenticated, guide user through
    npx netlify login
  3. Fail gracefully if authentication cannot be established
Authentication uses either:
  • Browser-based OAuth (primary):
    netlify login
    opens browser for authentication
  • API Key (alternative): Set
    NETLIFY_AUTH_TOKEN
    environment variable
本技能采用预认证Netlify CLI方式:
  1. 使用
    npx netlify status
    检查身份认证状态
  2. 若未认证,引导用户执行
    npx netlify login
  3. 若无法完成认证,优雅地终止流程
身份认证可通过以下两种方式:
  • 基于浏览器的OAuth(首选):
    netlify login
    会打开浏览器进行认证
  • API密钥(备选):设置
    NETLIFY_AUTH_TOKEN
    环境变量

Workflow

工作流程

1. Verify Netlify CLI Authentication

1. 验证Netlify CLI身份认证

Check if the user is logged into Netlify:
bash
npx netlify status
Expected output patterns:
  • ✅ Authenticated: Shows logged-in user email and site link status
  • ❌ Not authenticated: "Not logged into any site" or authentication error
If not authenticated, guide the user:
bash
npx netlify login
This opens a browser window for OAuth authentication. Wait for user to complete login, then verify with
netlify status
again.
Alternative: API Key authentication
If browser authentication isn't available, users can set:
bash
export NETLIFY_AUTH_TOKEN=your_token_here
检查用户是否已登录Netlify:
bash
npx netlify status
预期输出模式
  • ✅ 已认证:显示已登录用户的邮箱和站点关联状态
  • ❌ 未认证:“未登录任何站点”或认证错误
若未认证,引导用户执行:
bash
npx netlify login
这会打开一个浏览器窗口进行OAuth认证。等待用户完成登录后,再次使用
netlify status
验证。
备选:API密钥认证
若无法使用浏览器认证,用户可设置:
bash
export NETLIFY_AUTH_TOKEN=your_token_here

2. Detect Site Link Status

2. 检测站点关联状态

From
netlify status
output, determine:
  • Linked: Site already connected to Netlify (shows site name/URL)
  • Not linked: Need to link or create site
netlify status
的输出中判断:
  • 已关联:站点已连接到Netlify(显示站点名称/URL)
  • 未关联:需要关联或创建站点

3. Link to Existing Site or Create New

3. 关联现有站点或创建新站点

If already linked → Skip to step 4
If not linked, attempt to link by Git remote:
bash
undefined
若已关联 → 跳至步骤4
若未关联,尝试通过Git远程仓库关联:
bash
undefined

Check if project is Git-based

检查项目是否基于Git

git remote show origin
git remote show origin

If Git-based, extract remote URL

若基于Git,提取远程仓库URL

Format: https://github.com/username/repo or git@github.com:username/repo.git

格式:https://github.com/username/repogit@github.com:username/repo.git

Try to link by Git remote

尝试通过Git远程仓库关联

npx netlify link --git-remote-url <REMOTE_URL>

**If link fails** (site doesn't exist on Netlify):

```bash
npx netlify link --git-remote-url <REMOTE_URL>

**若关联失败**(Netlify上不存在该站点):

```bash

Create new site interactively

交互式创建新站点

npx netlify init

This guides user through:
1. Choosing team/account
2. Setting site name
3. Configuring build settings
4. Creating netlify.toml if needed
npx netlify init

这会引导用户完成以下步骤:
1. 选择团队/账户
2. 设置站点名称
3. 配置构建设置
4. 必要时创建netlify.toml文件

4. Verify Dependencies

4. 验证依赖项

Before deploying, ensure project dependencies are installed:
bash
undefined
部署前,确保已安装项目依赖项:
bash
undefined

For npm projects

针对npm项目

npm install
npm install

For other package managers, detect and use appropriate command

针对其他包管理器,自动检测并使用相应命令

yarn install, pnpm install, etc.

yarn install, pnpm install 等

undefined
undefined

5. Deploy to Netlify

5. 部署到Netlify

Choose deployment type based on context:
Preview/Draft Deploy (default for existing sites):
bash
npx netlify deploy
This creates a deploy preview with a unique URL for testing.
Production Deploy (for new sites or explicit production deployments):
bash
npx netlify deploy --prod
This deploys to the live production URL.
Deployment process:
  1. CLI detects build settings (from netlify.toml or prompts user)
  2. Builds the project locally
  3. Uploads built assets to Netlify
  4. Returns deployment URL
根据环境选择部署类型:
预览/草稿部署(现有站点默认选项):
bash
npx netlify deploy
这会创建一个带有唯一URL的部署预览用于测试。
生产部署(针对新站点或明确的生产部署需求):
bash
npx netlify deploy --prod
这会部署到线上生产环境URL。
部署流程
  1. CLI检测构建设置(来自netlify.toml或提示用户输入)
  2. 在本地构建项目
  3. 将构建后的资源上传到Netlify
  4. 返回部署URL

6. Report Results

6. 报告结果

After deployment, report to user:
  • Deploy URL: Unique URL for this deployment
  • Site URL: Production URL (if production deploy)
  • Deploy logs: Link to Netlify dashboard for logs
  • Next steps: Suggest
    netlify open
    to view site or dashboard
部署完成后,向用户报告以下信息:
  • 部署URL:本次部署的唯一URL
  • 站点URL:生产环境URL(若为生产部署)
  • 部署日志:Netlify控制台的日志链接
  • 后续步骤:建议使用
    netlify open
    查看站点或控制台

Handling netlify.toml

处理netlify.toml文件

If a
netlify.toml
file exists, the CLI uses it automatically. If not, the CLI will prompt for:
  • Build command: e.g.,
    npm run build
    ,
    next build
  • Publish directory: e.g.,
    dist
    ,
    build
    ,
    .next
Common framework defaults:
  • Next.js: build command
    npm run build
    , publish
    .next
  • React (Vite): build command
    npm run build
    , publish
    dist
  • Static HTML: no build command, publish current directory
The skill should detect framework from
package.json
if possible and suggest appropriate settings.
若存在
netlify.toml
文件,CLI会自动使用其中的配置。若不存在,CLI会提示用户输入:
  • 构建命令:例如
    npm run build
    next build
  • 发布目录:例如
    dist
    build
    .next
常见框架默认配置:
  • Next.js:构建命令
    npm run build
    ,发布目录
    .next
  • React (Vite):构建命令
    npm run build
    ,发布目录
    dist
  • 静态HTML:无需构建命令,发布目录为当前目录
本技能会尽可能从
package.json
中检测框架,并推荐合适的配置。

Example Full Workflow

完整工作流程示例

bash
undefined
bash
undefined

1. Check authentication

1. 检查身份认证状态

npx netlify status
npx netlify status

If not authenticated:

若未认证:

npx netlify login
npx netlify login

2. Link site (if needed)

2. 关联站点(若需要)

Try Git-based linking first

优先尝试基于Git的关联

git remote show origin npx netlify link --git-remote-url https://github.com/user/repo
git remote show origin npx netlify link --git-remote-url https://github.com/user/repo

If no site exists, create new one:

若站点不存在,创建新站点:

npx netlify init
npx netlify init

3. Install dependencies

3. 安装依赖项

npm install
npm install

4. Deploy (preview for testing)

4. 部署(预览版本用于测试)

npx netlify deploy
npx netlify deploy

5. Deploy to production (when ready)

5. 部署到生产环境(准备就绪后)

npx netlify deploy --prod
undefined
npx netlify deploy --prod
undefined

Error Handling

错误处理

Common issues and solutions:
"Not logged in" → Run
npx netlify login
"No site linked" → Run
npx netlify link
or
npx netlify init
"Build failed" → Check build command and publish directory in netlify.toml or CLI prompts → Verify dependencies are installed → Review build logs for specific errors
"Publish directory not found" → Verify build command ran successfully → Check publish directory path is correct
常见问题及解决方案:
“未登录” → 执行
npx netlify login
“未关联任何站点” → 执行
npx netlify link
npx netlify init
“构建失败” → 检查netlify.toml或CLI提示中的构建命令和发布目录 → 验证依赖项是否已安装 → 查看构建日志中的具体错误
“发布目录未找到” → 验证构建命令是否执行成功 → 检查发布目录路径是否正确

Troubleshooting

故障排除

Escalated Network Access

提升网络访问权限

If deployment fails due to network issues (timeouts, DNS errors, connection resets), rerun the deploy with escalated permissions (use
sandbox_permissions=require_escalated
). The deploy requires escalated network access when sandbox networking blocks outbound requests.
Example guidance to the user:
The deploy needs escalated network access to deploy to Netlify. I can rerun the command with escalated permissions—want me to proceed?
若因网络问题(超时、DNS错误、连接重置)导致部署失败,使用提升的权限重新运行部署命令(使用
sandbox_permissions=require_escalated
)。当沙箱网络阻止出站请求时,部署需要提升的网络访问权限。
给用户的示例指引:
部署需要提升网络访问权限才能部署到Netlify。我可以使用提升的权限重新运行命令——是否继续?

Environment Variables

环境变量

For secrets and configuration:
  1. Never commit secrets to Git
  2. Set in Netlify dashboard: Site Settings → Environment Variables
  3. Access in builds via
    process.env.VARIABLE_NAME
针对密钥和配置:
  1. 切勿将密钥提交到Git
  2. 在Netlify控制台中设置:站点设置 → 环境变量
  3. 在构建中通过
    process.env.VARIABLE_NAME
    访问

Tips

小贴士

  • Use
    netlify deploy
    (no
    --prod
    ) first to test before production
  • Run
    netlify open
    to view site in Netlify dashboard
  • Run
    netlify logs
    to view function logs (if using Netlify Functions)
  • Use
    netlify dev
    for local development with Netlify Functions
  • 先使用
    netlify deploy
    (不带
    --prod
    参数)进行测试,再部署到生产环境
  • 执行
    netlify open
    在Netlify控制台中查看站点
  • 执行
    netlify logs
    查看函数日志(若使用Netlify Functions)
  • 执行
    netlify dev
    结合Netlify Functions进行本地开发

Reference

参考资料

Bundled References (Load As Needed)

捆绑参考资料(按需加载)

  • CLI commands
  • Deployment patterns
  • netlify.toml guide
  • CLI命令
  • 部署模式
  • netlify.toml指南