MB-ITA

Diese Seite ist passwortgeschützt.

claude-code — hooks.md
~/wiki/claude-code $ cat hooks.md

Hooks & Automation

Pre/Post-Hooks, automatische Validierung, CI/CD-Integration.

Was sind Hooks?

Hooks sind Shell-Befehle, die an bestimmten Lifecycle-Punkten ausgeführt werden. Sie laufen deterministisch (immer, ohne LLM-Entscheidung).

  • Code formatieren nach Datei-Änderungen
  • Geschützte Dateien blocken vor Edits
  • Benachrichtigungen senden bei Permission-Requests
  • Kontext injizieren nach Session-Start
  • Änderungen auditieren für Compliance

Lifecycle Events

EventWannBlockiert?
SessionStartSession beginnt/wird fortgesetztNein
UserPromptSubmitVor Prompt-VerarbeitungNein
PreToolUseVor Tool-AusführungJa
PostToolUseNach Tool-ErfolgNein
PostToolUseFailureNach Tool-FehlerNein
PermissionRequestPermission-Dialog erscheintNein
StopClaude beendet AntwortJa
NotificationBenachrichtigung gesendetNein
ConfigChangeConfig-Datei geändertJa
SessionEndSession endetNein

Hook-Typen

TypBeschreibungUse Case
commandShell-Befehl (empfohlen)Deterministische Regeln
promptEinmal-LLM-AbfrageUrteilsentscheidungen
agentMulti-Turn mit ToolsVerifikation mit Datei-Zugriff
httpPOST an URLExterne Services
Exit Codes: 0 = Aktion erlauben (stdout → Claude Context), 2 = Aktion blockieren (stderr → Feedback an Claude)

Ersten Hook einrichten

# Interaktives Menü /hooks # → Event wählen (z.B. "Notification") # → "Add new hook" → Command eingeben # → Scope wählen (User/Project)
Desktop-Benachrichtigung (Windows)
powershell.exe -Command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('Claude braucht Aufmerksamkeit', 'Claude Code')"

Praxisbeispiele

{ "hooks": { "PostToolUse": [{ "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write" }] }] } }
.claude/hooks/protect-files.sh
#!/bin/bash INPUT=$(cat) FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty') PROTECTED=(".env" "package-lock.json" ".git/") for pattern in "${PROTECTED[@]}"; do if [[ "$FILE_PATH" == *"$pattern"* ]]; then echo "Blocked: $FILE_PATH" >&2 exit 2 fi done exit 0
{ "hooks": { "SessionStart": [{ "matcher": "compact", "hooks": [{ "type": "command", "command": "echo 'Reminder: Nutze pnpm, nicht npm.'" }] }] } }
{ "hooks": { "PostToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "jq -r '.tool_input.command' >> ~/.claude/command-log.txt" }] }] } }

CI/CD Integration

Non-Interactive Mode (Headless)
# Einmal-Abfrage claude -p "erkläre diese Funktion" # Mit Budget-Limit claude -p "review code" --max-budget-usd 5.00 # Turn-Limit claude -p "fix tests" --max-turns 3 # JSON Output claude -p "analyse" --output-format json
GitHub Actions
name: Claude Code on: pull_request: types: [opened, synchronize] jobs: claude: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: anthropics/claude-code-action@v1 with: api_key: $ command: "Review this PR"

Konfiguration Scopes

OrtScopeTeilbar?
~/.claude/settings.jsonAlle ProjekteNein
.claude/settings.jsonEinzelnes ProjektJa (Git)
.claude/settings.local.jsonEinzelnes ProjektNein (.gitignore)
Nutze /hooks zum interaktiven Hinzufügen — Änderungen wirken sofort.