FEATURE ARTICLE

配置模板

Claude Code 环境配置实践,包含 CLAUDE.md 设计原则、分层加载和常见配置建议。

2026-05-22 技术 Claude Code / 环境配置 / CLAUDE.md

A.1 settings.json 模板

个人全局设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// ~/.claude/settings.json
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(git status)",
"Bash(git diff *)",
"Bash(git log *)",
"Bash(git add *)",
"Bash(git commit *)"
]
},
"hooks": {
"Notification": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code 需要你的注意\" with title \"Claude Code\"'"
}
]
}
]
},
"env": {
"CLAUDE_CODE_EFFORT_LEVEL": "high"
}
}

项目共享设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// .claude/settings.json
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(npm run lint)",
"Bash(npm run test *)",
"Bash(npm run build)",
"Bash(npm run typecheck)"
],
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Bash(rm -rf *)",
"Bash(curl *)"
]
},
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write 2>/dev/null || true"
}
]
}
]
}
}

企业托管策略

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// /Library/Application Support/ClaudeCode/managed-settings.json (macOS)
{
"allowManagedHooksOnly": true,
"allowManagedPermissionRulesOnly": true,
"disableBypassPermissionsMode": "disable",
"permissions": {
"deny": [
"Bash(curl *)",
"Bash(wget *)",
"Read(./**/*.pem)",
"Read(./**/*.key)",
"Read(./**/.env*)"
]
}
}

A.2 Hook 脚本模板

protect-files.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# .claude/hooks/protect-files.sh
# 阻止编辑受保护的文件

INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')

PROTECTED_PATTERNS=(
".env"
"package-lock.json"
"pnpm-lock.yaml"
".git/"
"migrations/"
".pem"
".key"
)

for pattern in "${PROTECTED_PATTERNS[@]}"; do
if [[ "$FILE_PATH" == *"$pattern"* ]]; then
echo "Blocked: $FILE_PATH matches protected pattern '$pattern'" >&2
exit 2
fi
done

exit 0

filter-test-output.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# ~/.claude/hooks/filter-test-output.sh
# 过滤测试输出,只显示失败

input=$(cat)
cmd=$(echo "$input" | jq -r '.tool_input.command')

if [[ "$cmd" =~ ^(npm\ test|pnpm\ test|pytest|go\ test|bun\ test) ]]; then
filtered_cmd="$cmd 2>&1 | grep -A 5 -E '(FAIL|ERROR|error:)' | head -100"
jq -n --arg cmd "$filtered_cmd" \
'{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow","updatedInput":{"command":$cmd}}}'
else
echo "{}"
fi

A.3 GitHub Actions 模板

基础 @claude 响应

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# .github/workflows/claude.yml
name: Claude Code
on:
issue_comment:
types: [created]
pull_request_review_comment:
types: [created]
jobs:
claude:
if: contains(github.event.comment.body, '@claude')
runs-on: ubuntu-latest
steps:
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}

定时报告

1
2
3
4
5
6
7
8
9
10
11
12
name: Daily Report
on:
schedule:
- cron: "0 9 * * *"
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: anthropics/claude-code-action@v1
with:
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
prompt: "生成昨天的提交总结和未关闭 Issue 报告"