You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add site search and improve non-interactive linking (#8127)
# Improve `netlify link` and Add `netlify sites:search` for
Non-Interactive Environments
## Overview
This PR improves the Netlify CLI's behavior in non-interactive
environments (CI/CD, scripts, AI agents) by fixing `netlify link`
hanging issues and introducing a new `sites:search` command for site
discovery.
## Changes
### 1. 🔍 New Command: `netlify sites:search`
Added a new command for searching projects by name, making site
discovery easier for both users and automation tools.
**Usage:**
```bash
netlify sites:search my-project
netlify sites:search "partial name" --json
```
**Features:**
- Searches by full or partial project name
- Returns matching projects with ID, URL, repo, and account info
- Supports `--json` flag for programmatic use
- Works seamlessly in CI/CD environments
### 2. 🔗 Fixed `netlify link` Non-Interactive Behavior
Previously, `netlify link` would hang indefinitely when run without
options in CI/CD environments. Now it provides helpful guidance instead.
**Before:**
```bash
# In CI/CD:
netlify link
# ❌ Hangs trying to prompt user interactively
```
**After:**
```bash
# In CI/CD:
netlify link
# ✅ Error: No project specified. In non-interactive mode, you must specify how to link:
#
# Link by project ID:
# netlify link --id <project-id>
#
# Link by project name:
# netlify link --name <project-name>
#
# To search for projects:
# netlify sites:search <search-term>
```
**Updated error messages to guide users:**
- When no site found by name → suggests `sites:search` and `link --id`
- When no site found by git remote → suggests `sites:search` and manual
linking options
- When no options provided in CI/CD → shows all available linking
methods
## Example Scenarios
### Scenario 1: AI Agent Linking a Project
**Before:**
```bash
Agent: netlify link
CLI: [hangs waiting for interactive input]
Agent: [timeout or confusion]
```
**After:**
```bash
Agent: netlify link
CLI: Error with helpful guidance showing all options
Agent: netlify sites:search my-project --json
CLI: [{"id":"site-123","name":"my-project",...}]
Agent: netlify link --id site-123
CLI: ✓ Linked to my-project
```
### Scenario 2: Agent Searching for a Site
**Before:**
```bash
Agent: [tries to use undocumented API or link --name with guessing]
CLI: [may link to wrong site or fail]
```
**After:**
```bash
Agent: netlify sites:search "customer-portal" --json
CLI: [
{"id":"site-1","name":"customer-portal-staging",...},
{"id":"site-2","name":"customer-portal-prod",...}
]
Agent: [can now make informed decision about which site to use]
```
### Scenario 3: CI/CD Script Needing to Link
**Before:**
```bash
# CI/CD script without knowing site ID:
netlify link
# ❌ Hangs indefinitely, pipeline fails
```
**After:**
```bash
# CI/CD script can discover and link:
SITE_ID=$(netlify sites:search "$PROJECT_NAME" --json | jq -r '.[0].id')
netlify link --id "$SITE_ID"
# ✅ Successfully linked
```
## Testing
### New Tests Added
- **`sites:search`**: 6 tests covering search, JSON output, empty
results, non-interactive mode
- **`link`**: 3 tests for non-interactive error handling
### Test Coverage
- ✅ All tests passing
- ✅ Explicit CI/CD environment tests (`CI=true`, `isTTY=false`)
- ✅ Verified no hanging in non-interactive scenarios
- ✅ Verified helpful error messages appear correctly
### Documentation
- ✅ Generated command documentation for `sites:search`
- ✅ Updated help snapshots
- ✅ All commands properly documented with examples
## Breaking Changes
None. All changes are backward compatible and only add features or
improve error handling.
## Related Issues
Addresses common issues with:
- CLI hanging in CI/CD environments
- AI agents unable to discover and link sites programmatically
- Unclear guidance when commands fail in non-interactive mode
---
**Summary:** This PR makes the Netlify CLI more robust for automated
environments by preventing `netlify link` from hanging in CI/CD and
providing a new `sites:search` command for programmatic site discovery.
---------
Co-authored-by: Sean Roberts <sean.roberts@netlify.com>
0 commit comments