| name | netlify-deploy |
| description | Deploy sites and functions to Netlify: netlify deploy --prod, environment variables, redirects, edge functions, form handling. Trigger: when deploying to Netlify, netlify deploy, Netlify functions, netlify.toml, Netlify edge functions, Netlify redirects, Netlify env variables |
| version | 1 |
| argument-hint | [deploy|env|functions|status|open] |
| allowed-tools | ["bash","read","write","grep","glob"] |
Netlify Deployment
You are now operating in Netlify deployment mode.
Installation
npm install -g netlify-cli
netlify --version
netlify login
netlify status
Core Deployment
netlify deploy --dir=dist
netlify deploy --prod --dir=dist
netlify deploy --prod --dir=dist --open
netlify deploy --alias=feature-branch --dir=dist
netlify deploy --prod --dir=public
netlify deploy --prod --dir=dist 2>&1 | tee deploy.log
netlify.toml Configuration
[build]
command = "npm run build"
publish = "dist"
functions = "netlify/functions"
[build.environment]
NODE_VERSION = "20"
NPM_FLAGS = "--prefer-offline"
[context.production]
command = "npm run build:prod"
[context.deploy-preview]
command = "npm run build:preview"
[context.branch-deploy]
command = "npm run build:staging"
[[headers]]
for = "/*"
[headers.values]
X-Frame-Options = "DENY"
X-Content-Type-Options = "nosniff"
Referrer-Policy = "strict-origin-when-cross-origin"
[[headers]]
for = "/assets/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
Redirects and Rewrites
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[[redirects]]
from = "/api/*"
to = "https://api.example.com/:splat"
status = 200
force = true
[[redirects]]
from = "/old-page"
to = "/new-page"
status = 301
[[redirects]]
from = "/"
to = "/uk/"
status = 302
conditions = {Country = ["GB"]}
[[redirects]]
from = "http://example.com/*"
to = "https://example.com/:splat"
status = 301
force = true
Alternatively, use _redirects file in the publish directory:
# _redirects
/api/* https://api.example.com/:splat 200
/old /new 301
/* /index.html 200
Environment Variables
netlify env:list
netlify env:set API_KEY "mysecretkey"
netlify env:set DATABASE_URL "postgres://..." --context production
netlify env:set DATABASE_URL "postgres://..." --context deploy-preview
netlify env:get API_KEY
netlify env:unset API_KEY
netlify env:import .env.production
Netlify Functions (Serverless)
netlify functions:create my-function
netlify functions:list
netlify functions:invoke my-function --payload '{"name":"world"}'
netlify dev
Function Example (Node.js)
exports.handler = async (event, context) => {
const name = event.queryStringParameters?.name || "World";
return {
statusCode: 200,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: `Hello, ${name}!` }),
};
};
Function Example (Go)
package main
import (
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
return events.APIGatewayProxyResponse{
StatusCode: 200,
Body: `{"message":"Hello from Go!"}`,
}, nil
}
func main() { lambda.Start(handler) }
Edge Functions
export default async (request, context) => {
const url = new URL(request.url);
const name = url.searchParams.get("name") || "World";
return new Response(`Hello, ${name}!`, {
headers: { "Content-Type": "text/plain" },
});
};
export const config = { path: "/greet" };
[[edge_functions]]
path = "/greet"
function = "greet"
Form Handling
<form name="contact" method="POST" data-netlify="true">
<input type="hidden" name="form-name" value="contact" />
<input type="text" name="name" required />
<input type="email" name="email" required />
<textarea name="message" required></textarea>
<button type="submit">Send</button>
</form>
netlify api listSiteFormSubmissions --data '{"site_id":"<SITE_ID>","form_id":"<FORM_ID>"}'
Local Development
netlify dev
netlify dev --port 8888
netlify link --name my-site-name
netlify open
netlify status
CI/CD Integration
export NETLIFY_AUTH_TOKEN="${NETLIFY_AUTH_TOKEN}"
export NETLIFY_SITE_ID="${NETLIFY_SITE_ID}"
npm run build
netlify deploy --prod --dir=dist
DEPLOY_URL=$(netlify deploy --prod --dir=dist --json | jq -r '.url')
curl -f "${DEPLOY_URL}/health" || exit 1
Post-Deploy Verification
SITE_URL="https://my-site.netlify.app"
EXPECTED_VERSION="1.2.3"
curl -s "${SITE_URL}/version.json" | jq -r '.version'
netlify api getSiteDeploy --data '{"site_id":"<SITE_ID>","deploy_id":"<DEPLOY_ID>"}'
curl -f "${SITE_URL}/health" || echo "Health check failed"
Best Practices
- Use
netlify deploy (draft) before netlify deploy --prod to preview changes.
- Store
NETLIFY_AUTH_TOKEN and NETLIFY_SITE_ID as CI secrets — never commit them.
- Use
netlify.toml for all configuration (not the Netlify UI) to keep settings in version control.
- Set environment variables per context (production vs deploy-preview vs branch-deploy).
- Use the
_redirects file or [[redirects]] in netlify.toml for the SPA fallback — never configure it in the UI only.
- Use Netlify Functions for backend logic instead of exposing API keys in client-side code.
- Enable branch deploys for staging environments to test before promoting to production.