如何在GitHub Actions中使用Husky配置?
解读
面试官问“如何在 GitHub Actions 中使用 Husky 配置”,并不是想听你背一段“npm install husky” 的流程,而是考察三件事:
- 你是否真的理解 Husky 的触发时机(本地 .git/hooks)与 CI 环境(裸仓库、无 TTY、无交互)之间的本质差异;
- 在国内网络与镜像源场景下,你有没有踩过“husky install 失败”“lint-staged 找不到”这类坑;
- 你是否能把“本地拦截”与“云端兜底”做成互补方案,而不是让 Husky 在 CI 里变成“聋子的耳朵”。
一句话:他想知道你怎样在 GitHub Actions 里“不依赖 Husky 钩子,却又能复用同一套质量门禁脚本”,并且保证国内流水线稳定可重现。
知识点
- Husky 8.x 的核心机制:在 postinstall 阶段把 .husky 目录下的 shell 脚本映射到 .git/hooks;CI 默认拉的是裸仓库,.git 目录与本地不同,钩子不会自动复制。
- GitHub Actions 默认容器环境:ubuntu-latest 自带 npm 14+,但国内裸连 GitHub 经常 443 timeout;需要配置 npmmirror 或 actions/cache 加速。
- 环境变量 HUSKY 的“总开关”:在 CI 里只要 HUSKY=0,husky install 会直接跳过钩子安装,避免 postinstall 报错。
- lint-staged 的两种调用模式:本地通过 husky 触发 lint-staged;CI 里可直接 npx lint-staged --diff="origin/main...HEAD" 实现“增量检查”,与本地行为保持一致。
- 国内私有仓库场景:很多公司用 Gitea/GitLab + GitHub Actions(self-hosted runner),需要把 .husky 目录打进仓库,并给 runner 配置 core.hooksPath,才能让钩子生效。
- 权限陷阱:Windows runner 下 .husky/*.sample 默认无执行位,需要 git update-index --chmod=+x .husky/pre-push,否则 CI 会报 126 错误。
- 互补策略:本地用 Husky 做“事前拦截”,CI 用同一套 npm script(lint:test:prettier)做“兜底”,保证即使有人绕过 --no-verify 也能被拦下。
答案
下面给出一套在国内网络环境下可直接落地的 GitHub Actions 模板,同时解释每一步为什么这么做,方便你在面试时边写边讲。
步骤 1:仓库根目录保留 .husky 目录,并把钩子脚本命名为 pre-push、commit-msg 等,但内容里不要写死路径,统一走 npm script:
// package.json
"scripts": {
"lint": "eslint . --ext .js,.ts,.vue",
"format:check": "prettier --check .",
"test:unit": "jest",
"lint-staged": "lint-staged",
"ci:quality": "npm run lint && npm run format:check && npm run test:unit"
},
"lint-staged": {
"*.{js,ts,vue}": "eslint --fix",
"*.{js,ts,vue,css,md}": "prettier --write"
},
"devDependencies": {
"husky": "^8.0.3",
"lint-staged": "^13.2.0"
}
步骤 2:本地安装 Husky 时走国内镜像,并强制把钩子脚本加执行位:
# 安装依赖
npm config set registry https://registry.npmmirror.com
npm install
# 给钩子加执行位,兼容 Windows WSL 与 macOS
git update-index --chmod=+x .husky/pre-push
git update-index --chmod=+x .husky/commit-msg
git add .husky
git commit -m "chore: add husky hooks"
步骤 3:在 CI 里显式关闭 Husky 安装,但复用同一套 quality 脚本,并做增量检查:
# .github/workflows/ci.yml
name: CI
on:
pull_request:
branches: [main]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # 需要完整历史才能 diff
- uses: actions/setup-node@v4
with:
node-version: 18
cache: 'npm'
registry-url: 'https://registry.npmmirror.com'
- name: 安装依赖并跳过 husky
run: |
echo "HUSKY=0" >> $GITHUB_ENV
npm ci
- name: 增量 lint-staged(与本地行为对齐)
run: npx lint-staged --diff="origin/main...HEAD"
- name: 全量兜底检查
run: npm run ci:quality
步骤 4(可选):如果公司用 self-hosted runner 且希望在 CI 也触发钩子,可手动指定 hooksPath:
git config core.hooksPath .husky
npm run ci:quality
面试时你可以边写边说: “Husky 只在本地 .git/hooks 生效,CI 裸仓库里没有钩子,所以我用 HUSKY=0 跳过安装;但质量脚本全部收敛到 npm script,保证本地和云端同一套命令。lint-staged 在 CI 里用 diff 范围实现增量检查,既省时间又能对齐本地行为。国内网络我用 registry.npmmirror.com 配合 actions/cache,把 node_modules 缓存到 10 秒级恢复,彻底解决 GitHub 超时问题。”
拓展思考
- 如果团队有人习惯 git push --no-verify,CI 阶段如何“强制回滚”这次提交? 思路:在 workflow 最后一步用 GitHub API 把本次 PR 的 head commit 状态置为 failure,并评论“检测到绕过 Husky,请本地修复后重新 push”。
- 大型 Monorepo 下 .husky 目录放在根还是每个子包?如何配合 nx affected 做增量? 思路:根目录统一放钩子,脚本里调用 nx affected --target=lint 即可,CI 同样复用 nx 计算 affected 范围,时间从 10min 降到 2min。
- 国内私有 GitLab 想完全复刻 GitHub Actions 体验,有没有轻量级替代? 思路:用 Gitea + act runner,镜像源保持 npmmirror,把 .github/workflows 目录原样放到 Gitea 仓库,act 会自动识别,零改造迁移。
- 未来 Husky 9.x 计划把钩子脚本改成 JS 插件,CI 里是否还需要 shell 脚本? 思路:官方提案是“插件即 npm 包”,CI 里直接 node .husky/pre-push.js 即可,但权限模型与跨平台兼容仍需验证,建议先观望,稳定后再升级。