Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何做 commit lint【热度: 425】 #771

Open
yanlele opened this issue May 26, 2024 · 0 comments
Open

如何做 commit lint【热度: 425】 #771

yanlele opened this issue May 26, 2024 · 0 comments
Labels
工程化 web 项目工程化 腾讯 公司标签
Milestone

Comments

@yanlele
Copy link
Member

yanlele commented May 26, 2024

关键词:commit 规范

Commit lint 是一种实践,用于在代码库中规范化提交信息的格式。这种做法通常有助于团队成员理解代码库的历史记录,以及自动化生成变更日志。下面是实施 Commit lint 的一些基本步骤:

  1. 选择 Commit 信息规范: 首先,你需要选择一个提交信息的规范,最常见的是Conventional Commits,它具有明确的结构和规则。

  2. 配置 Linter 工具: commitlint 是一个流行的工具,用于检查提交信息是否符合规定的格式。安装 commitlint,通常是作为项目的开发依赖。

    npm install --save-dev @commitlint/{config-conventional,cli}
  3. 设置 commitlint 配置: 在你的项目根目录下创建一个名为 commitlint.config.js 的文件,并且导入你选择的规范:

    module.exports = { extends: ["@commitlint/config-conventional"] };
  4. 安装钩子(Hook)管理工具: Husky 是一个钩子管理工具,它可以助你轻松的在 Git 挂钩中添加脚本(例如,在 commit 之前检查提交信息格式)。

    npm install husky --save-dev
  5. 配置 Husky 来使用 commitlint:

    • 初始化 husky:
    npx husky install
    • 添加 commit-msg 钩子来使用 commitlint。执行非交互式的命令配置钩子脚本:
    npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

    这行代码会在.husky/commit-msg文件中创建一个钩子,并且在你试图创建提交时,会调用 commitlint 来检查你的提交信息。

  6. 提交代码: 当你提交代码时,Husky 会触发 commit-msg 钩子调用 commitlint 检查提交信息。如果信息不符合规范,提交将被拒绝,并显示错误信息。

  7. 配置 CI/CD 流水线: 为了确保规范被强制执行,可以在 CI/CD 流水线中添加一步来执行 commitlint。这样,如果提交的信息不符合规范,构建将会失败。

@yanlele yanlele added 工程化 web 项目工程化 腾讯 公司标签 labels May 26, 2024
@yanlele yanlele added this to the milestone May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
工程化 web 项目工程化 腾讯 公司标签
Projects
None yet
Development

No branches or pull requests

1 participant