Nâng CaoGit iconGit

Server-side hooks (GitHub Actions) vs client-side hooks (Husky): nên dùng cái nào để enforce gì?

Client-side hooks (Husky): chạy trên máy developer.
- pre-commit: lint, format, type-check
- commit-msg: commitlint
- pre-push: unit tests
- Có thể bypass: git commit --no-verify, git push --no-verify
- Share được qua git (Husky v7+ lưu hook files trong .husky/ — committed vào repo, cài tự động qua npm prepare; .git/hooks/ vẫn là client-side và vẫn bypassable)

Server-side hooks (GitHub Actions/CI): chạy trên server, developer không thể bypass.

yaml
# .github/workflows/ci.yml
on: [pull_request]
jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npm run lint
      - run: npm run typecheck
      - run: npm run test
      - run: npx commitlint --from ${{ github.event.pull_request.base.sha }}

Phân chia trách nhiệm:

TaskClient HookServer Hook
Fast lint/formatpre-commit ✓không cần
Type checkpre-commit ✓CI backup ✓
Unit testspre-push ✓CI ✓
Integration testskhôngCI ✓
Commit messagecommit-msg ✓CI ✓
Security scankhôngCI ✓

Nguyên tắc: client hooks = fast feedback loop cho developer, server hooks = enforcement không thể bypass. Không dùng client hooks thay thế server hooks — chỉ dùng bổ sung.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git