This skill automates multi-platform deployments with zero-downtime, robust monitoring, and rollback capabilities across ci/cd, containers, and cloud services.
npx playbooks add skill mrowaisabdullah/ai-humanoid-robotics --skill deployment-engineer
Automate and manage production deployments across multiple platforms with zero-downtime, proper monitoring, and rollback capabilities. This skill encapsulates hard-won lessons from real-world deployment scenarios.
Use this skill when:
Lesson Learned: Always verify platform-specific requirements before deployment.
# .github/workflows/deploy-backend.yml
# Critical patterns discovered through painful debugging:
# 1. Branch Name Consistency
on:
push:
# NEVER assume 'main' - always verify actual branch name
branches: [master] # Fixed from 'main' after repo inspection
# 2. Authentication for External Services
- name: Deploy to HuggingFace
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
run: |
# Pattern: Use credential helper for Git auth
git config credential.helper store
echo "https://hf:$HF_TOKEN@huggingface.co" > ~/.git-credentials
git remote set-url origin https://hf:$HF_TOKEN@huggingface.co/spaces/${{ env.HF_SPACE_NAME }}
# 3. Error Handling and Verification
- name: Verify Deployment
run: |
# Always add post-deployment verification
curl -f "${{ env.DEPLOY_URL }}/health" || echo "Health check failed - space might still be starting"
Lesson Learned: Order of operations in Dockerfile is critical for build success.
# backend/Dockerfile - Battle-tested pattern
# 1. Use specific Python version
FROM python:3.11-slim
# 2. Install system dependencies FIRST
RUN apt-get update && apt-get install -y \
gcc \
curl \
&& rm -rf /var/lib/apt/lists/*
# 3. Set working directory early
WORKDIR /app
# 4. Copy requirements BEFORE source code (leverages Docker cache)
COPY pyproject.toml requirements.txt README.md ./
# 5. Install Python dependencies
RUN pip install uv
RUN uv pip install --system -e .
# 6. Copy application code
COPY . .
# 7. Create non-root user AFTER installation
RUN useradd -m -u 1000 user && chown -R user:user /app
USER user
# 8. Expose port and health check
EXPOSE 7860
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD curl -f http://localhost:7860/health || exit 1
# 9. CMD must be last
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
Lesson Learned: Different platforms require different environment variable strategies.
# backend/main.py - Environment loading pattern
from dotenv import load_dotenv
# Load .env for local development
load_dotenv()
class Settings(BaseSettings):
"""Always provide defaults for critical settings"""
# OpenAI Configuration
openai_api_key: str = os.getenv("OPENAI_API_KEY", "")
openai_model: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # Default to stable model
# Platform Detection
is_hf_spaces: bool = os.getenv("SPACE_ID") is not None
is_production: bool = os.getenv("NODE_ENV") == "production"
@property
def api_endpoint(self) -> str:
"""Auto-detect API endpoint based on platform"""
if self.is_hf_spaces:
# HuggingFace Spaces
space_name = os.getenv("SPACE_ID", "")
return f"https://{space_name.replace(' ', '-').lower()}.hf.space"
elif self.is_production:
# Production environment
return os.getenv("API_URL", "")
else:
# Local development
return "http://localhost:7860"
Lesson Learned: Frontend and backend on different domains require explicit CORS setup.
# backend/main.py - CORS configuration
app = FastAPI()
# Dynamic CORS origins based on environment
cors_origins = []
if os.getenv("NODE_ENV") == "production":
cors_origins = [
"https://yourusername.github.io",
"https://yourdomain.com"
]
else:
cors_origins = ["http://localhost:3000", "http://localhost:7860"]
app.add_middleware(
CORSMiddleware,
allow_origins=cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Lesson Learned: Frontend must adapt to different deployment environments.
// src/theme/Root.tsx - Dynamic API endpoint detection
const getChatkitEndpoint = () => {
// Check environment variable first
if (process.env.REACT_APP_CHAT_API_URL) {
return process.env.REACT_APP_CHAT_API_URL;
}
const hostname = window.location.hostname;
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return 'http://localhost:7860/chat';
}
// Production URLs
if (hostname.includes('github.io')) {
// GitHub Pages
return 'https://your-space.hf.space/chat';
} else if (hostname.includes('hf.space')) {
// HuggingFace Spaces
return `https://${hostname}/chat`;
}
return '/chat'; // Same domain deployment
};
Problem: GitHub Actions configured for 'main' but repo uses 'master'
# NEVER hard-code branch names
branches: [master] # Verify with `git branch` first
Problem: Permission errors during package installation
# Install dependencies BEFORE switching to non-root user
RUN uv pip install --system -e . # As root
USER user # Switch AFTER installation
Problem: Using models that require different APIs
# Wrong: gpt-5-nano requires Responses API, not Chat Completions
# Correct: Use compatible models
openai_model: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
Problem: Backend crashes on short queries like "hi"
# Allow single character queries
if not query or len(query.strip()) < 1:
raise ValueError("Query must be at least 1 character long")
Problem: No way to verify deployment success
@app.get("/health")
async def health_check():
"""Always implement health endpoints"""
return {
"status": "healthy",
"version": "1.0.0",
"timestamp": datetime.utcnow().isoformat(),
"services": {
"database": await check_database(),
"openai": bool(os.getenv("OPENAI_API_KEY"))
}
}
Problem: pip install -e . fails with OSError: Readme file does not exist: README.md
# ❌ Wrong - README.md not copied before pip install
COPY pyproject.toml ./
RUN pip install --no-cache-dir -e .
# ✅ Correct - Copy README.md with pyproject.toml
COPY pyproject.toml README.md ./
RUN pip install --no-cache-dir -e .
Root Cause: pyproject.toml has readme = "README.md" but hatchling can't find it during install.
Files Affected: Dockerfile, Dockerfile.hf
Problem: HuggingFace Spaces uses Dockerfile by default, not Dockerfile.hf
# You have TWO files:
Dockerfile # Used by HF Spaces by default
Dockerfile.hf # IGNORED by HF Spaces unless specified
# Solution: Keep BOTH files in sync or use one file
# Or specify in README.md frontmatter:
# sdk: docker
# dockerfile: Dockerfile.hf # Optional override
Lesson Learned: When you have multiple Dockerfiles, HuggingFace uses Dockerfile by default. Either keep them synchronized or explicitly specify which one to use.
Problem: ReferenceError: window is not defined or localStorage is not defined during build
// ❌ Wrong - Runs during SSR build
function setupAPIConfig() {
window.__API_BASE_URL__ = 'http://localhost:7860';
}
setupAPIConfig(); // Runs immediately at module load
// ✅ Correct - SSR guard
function setupAPIConfig() {
window.__API_BASE_URL__ = 'http://localhost:7860';
}
if (typeof window !== 'undefined') {
setupAPIConfig(); // Only runs in browser
}
For AuthContext with localStorage:
// ❌ Wrong - getInitialState accesses localStorage during SSR
const getInitialState = (): AuthState => {
const tokens = tokenManager.getTokens(); // Uses localStorage
return { token: tokens.token, ... };
};
// ✅ Correct - SSR guard in init function
const getInitialState = (): AuthState => {
// Return default state during SSR
if (typeof window === 'undefined') {
return {
user: null,
token: null,
refreshToken: null,
isLoading: false,
isAuthenticated: false,
error: null,
};
}
const tokens = tokenManager.getTokens();
return { token: tokens.token, ... };
};
Files Affected: src/clientModules/apiConfig.ts, src/context/AuthContext.tsx
Problem: "Missing configuration in README" error
# ❌ Wrong - README.md missing YAML frontmatter
# My Backend
FastAPI backend...
# ✅ Correct - YAML frontmatter at TOP of README.md
---
title: AI Book Backend
emoji: 🤖
colorFrom: blue
colorTo: indigo
sdk: docker
sdk_version: "3.11"
app_file: main.py
pinned: false
license: mit
---
# AI Book Backend
FastAPI backend...
Root Cause: HuggingFace Spaces requires YAML configuration in README.md at the ROOT of the repository.
Files Affected: backend/README.md
Problem: Client modules execute during SSR build in Docusaurus
// ❌ Wrong - Immediately executes code that needs browser APIs
// src/clientModules/apiConfig.ts
const config = window.location.hostname; // FAILS during build
// ✅ Correct - Lazy execution with guard
// src/clientModules/apiConfig.ts
function setupAPIConfig() {
if (typeof window !== 'undefined') {
window.__API_BASE_URL__ = 'http://localhost:7860';
}
}
// Only call if in browser
if (typeof window !== 'undefined') {
setupAPIConfig();
}
export default {};
Key Insight: Docusaurus client modules are bundled server-side. Always check typeof window !== 'undefined' before accessing browser APIs.
Problem: Module import errors after code reorganization
# ❌ Old import paths from refactored code
from database.config import get_db, SessionLocal, create_tables
from auth.auth import verify_token
# ✅ Fix: Update to new module structure
from src.core.database import get_async_db, SessionLocal, create_all_tables
from src.core.security import verify_token
# For sync operations in tests/migrations:
from src.core.database import get_sync_db
Common Patterns:
get_db → get_async_db (async) or get_sync_db (sync)Session → AsyncSession (async type hints)create_tables → create_all_tablesdatabase.config → src.core.databaseFiles Affected: All files referencing old database modules after refactoring
Problem: AttributeError: 'Settings' object has no attribute 'X'
# ❌ Settings class missing required attributes
class Settings(BaseSettings):
database_url: str
jwt_secret_key: str
# Missing: openai_api_key, qdrant_url, etc.
# ✅ Fix: Add all required attributes with defaults
class Settings(BaseSettings):
# Core
database_url: str = "sqlite:///./database/auth.db"
jwt_secret_key: str = "your-secret-key"
# OpenAI (for RAG features)
openai_api_key: Optional[str] = Field(default=None)
openai_model: str = "gpt-4o-mini"
openai_embedding_model: str = "text-embedding-3-small"
# Qdrant (for vector search)
qdrant_url: Optional[str] = Field(default=None)
qdrant_api_key: Optional[str] = Field(default=None)
# RAG settings
chunk_size: int = 512
chunk_overlap: int = 50
batch_size: int = 32
max_context_messages: int = 10
Root Cause: Settings class refactored but main.py still references old attributes.
Files Affected: src/core/config.py, main.py
Problem: NameError: name 'DATABASE_URL' is not defined in init scripts
# ❌ Using undefined global variable
print(f"Initializing database at: {DATABASE_URL}")
# ✅ Fix: Use Settings object
from src.core.config import settings
print(f"Initializing database at: {settings.database_url_sync}")
Files Affected: init_database.py, startup scripts
Problem: Docker build fails with various errors on HuggingFace Spaces
| Error | Cause | Solution |
|-------|--------|----------|
| OSError: Readme file does not exist: README.md | pyproject.toml references README.md but Dockerfile doesn't copy it | COPY pyproject.toml README.md ./ before pip install -e . |
| ModuleNotFoundError: No module named 'X' | Outdated import paths after refactoring | Update all imports to new module structure |
| AttributeError: 'Settings' object has no attribute 'X' | Settings class missing attributes | Add all required attributes to Settings class |
| NameError: name 'VAR' is not defined | Using undefined global variables | Use from src.core.config import settings and access via settings object |
| Config file '.env' not found | Missing .env file (warning only) | Ensure all required env vars set in HF Space secrets |
| AttributeError: 'AsyncSession' object has no attribute 'query' | Using sync query() with AsyncSession | Use await db.execute(select(Model)) instead of db.query(Model) |
| asyncpg.exceptions._base.InterfaceError: connection is closed | Database connection pool giving stale connections | Add pool_pre_ping=True and reduce pool_recycle to 1800 for Neon |
Problem: Trying to use async functions in sync context during startup
# ❌ Wrong: Calling async function without await
async def create_all_tables():
await conn.run_sync(Base.metadata.create_all)
# In startup sync context:
create_all_tables() # Doesn't actually create tables!
# ✅ Fix: Use sync engine for startup
from src.core.database import sync_engine, Base
Base.metadata.create_all(sync_engine)
# OR use async properly:
import asyncio
asyncio.create_task(create_all_tables()) # Fire and forget
Files Affected: main.py lifespan function, init_database.py
Problem: AttributeError: 'AsyncSession' object has no attribute 'query'
# ❌ Wrong: Using sync query() method with AsyncSession
@router.get("/users")
async def get_users(db: AsyncSession = Depends(get_async_db)):
users = db.query(User).all() # Error: AsyncSession has no 'query'
return users
# ✅ Fix: Use select() with execute() for async
from sqlalchemy import select
@router.get("/users")
async def get_users(db: AsyncSession = Depends(get_async_db)):
result = await db.execute(select(User))
users = result.scalars().all()
return users
# For single record:
result = await db.execute(select(User).filter(User.id == user_id))
user = result.scalar_one_or_none()
# For filtering:
result = await db.execute(
select(User).filter(User.email == email)
)
user = result.scalar_one_or_none()
Common Async Patterns:
db.query(Model).filter(...).first() → result = await db.execute(select(Model).filter(...)); user = result.scalar_one_or_none()db.query(Model).all() → result = await db.execute(select(Model)); users = result.scalars().all()db.commit() → await db.commit()db.refresh(obj) → await db.refresh(obj)db.add(obj) → db.add(obj) (no await needed)db.delete(obj) → await db.delete(obj) (if iterating) or use delete statementFiles Affected: All files using AsyncSession (routes, services, auth modules)
Critical: When converting from sync to async SQLAlchemy, ALL database operations must use the async pattern.
Problem: asyncpg.exceptions._base.InterfaceError: connection is closed
Database session error: (sqlalchemy.dialects.postgresql.asyncpg.InterfaceError)
<class 'asyncpg.exceptions._base.InterfaceError'>: connection is closed
Cause: Database connection pool giving stale/closed connections, especially after idle periods.
Fix: Configure async engine with proper pool settings:
# ❌ Wrong: Missing pool_pre_ping and incorrect pool settings
async_engine = create_async_engine(
settings.database_url_async,
pool_size=5,
max_overflow=10,
pool_recycle=3600, # Too long for Neon's 5-min idle timeout
# Missing pool_pre_ping
)
# ✅ Fix: Add pool_pre_ping and optimize settings for async
async_engine = create_async_engine(
settings.database_url_async,
echo=settings.debug,
pool_size=3, # Reduced from 5 for async
max_overflow=5, # Reduced from 10
pool_timeout=30,
pool_recycle=1800, # 30 min (reduced from 3600 for Neon's idle timeout)
pool_pre_ping=True, # CRITICAL: Verify connections before use
connect_args={
"server_settings": {
"application_name": "ai_book_backend",
"timezone": "utc"
},
"command_timeout": 60,
# Note: SSL configured via DATABASE_URL (sslmode=require)
},
pool_use_lifo=True, # Use LIFO to reduce stale connections
pool_drop_on_rollback=False,
)
Common Issues:
ssl directly in connect_args doesn't work with asyncpgFiles Affected: src/core/database.py
Critical for Neon PostgreSQL: Reduce pool_recycle to 1800 (30 min) or less, and always use pool_pre_ping=True.
---
1. README.md with YAML Frontmatter (REQUIRED)
---
title: AI Book Backend
emoji: 🤖
colorFrom: blue
colorTo: indigo
sdk: docker
sdk_version: "3.11"
app_file: main.py
pinned: false
license: mit
---
Must be at ROOT of repository with YAML at the TOP.
2. Dockerfile Requirements
# MUST copy README.md before pip install
COPY pyproject.toml README.md ./
RUN pip install --no-cache-dir -e .
# Not just:
COPY pyproject.toml ./ # ❌ Will fail if pyproject.toml has readme field
3. Environment Variables (Set in Space Settings)
JWT_SECRET_KEY=your-super-secret-jwt-key-at-least-32-chars
DATABASE_URL=sqlite:///./database/auth.db
ALLOWED_ORIGINS=https://your-frontend.github.io,https://huggingface.co
4. Import Path Consistency
All Python imports must use the new module structure:
# Old (broken):
from database.config import get_db
from auth.auth import verify_token
# New (working):
from src.core.database import get_async_db
from src.core.security import verify_token
5. Database Session Types
# For async endpoints (most FastAPI routes):
from src.core.database import get_async_db
from sqlalchemy.ext.asyncio import AsyncSession
@router.get("/users")
async def get_users(db: AsyncSession = Depends(get_async_db)):
result = await db.execute(select(User))
return result.scalars().all()
# For sync operations (migrations, scripts):
from src.core.database import get_sync_db, sync_engine
from sqlalchemy.orm import Session
def run_migration():
Base.metadata.create_all(sync_engine)
Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.