使用预提交挂钩更新 package.json 版本
我正在尝试自动化托管在私人 github 存储库上的 Node.js 项目的版本更新。
该项目旨在本地运行,因此不会发布,也不会发布。我组织中的人员只需拉出 main
分支并使用 yarn && 在自己的计算机上运行它即可。纱线开始
。
我想要实现的目标
我想要实现的是,在预提交阶段,对该项目的 package.json
进行版本提升(主要、补丁或次要),并与代码根据提交消息进行更改。
我想要做的就是我的 PR 将更改包含在 package.json
中,而无需我手动执行。我不需要为此发布版本或 CI。
我成功地
设置了 Husky 和 commitlint 来验证传统的提交消息,并且它工作正常。
我尝试过,但失败了,
我尝试使用semantic-release
和其他包来提供此功能,但它们都暗示在某个地方有 CI 构建或发布,所以我陷入了困境。
有什么想法吗?
I'm trying to automate the version bump of a Node.js project hosted on a private github repo.
The project is meant to run locally, so it's not published, neither released. People in my organization just pull the main
branch and run it on their machines with yarn && yarn start
.
What I want to achieve
What I want to achieve is that in the pre-commit phase a version bump is made (major, patch or minor) to the package.json
of this project and committed together with the code changed according to the commit message.
All I want to do is that my PR is including the change in the package.json
without me having to do it manually. I don't need releases or CI for this.
What I did successfully
I have setup Husky and commitlint in order to validate conventional commit message and it works fine.
What I tried, but failed
I tried to use semantic-release
and other packages to provide this functionality, but they all imply there's a CI build or release somewhere so I'm stuck.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用
commit-msg
挂钩,而不是pre-commit
。pre-commit
钩子在有提交消息之前触发,但如果使用commit-msg
钩子,它将在有提交消息后触发。该钩子将触发您的脚本,并且您的脚本将收到的第一个参数将是提交消息本身。
然后您可以在提交消息之上编写任何逻辑。您可以查看该消息,并根据您的请求根据消息更新版本。有很多方法可以实现
Instead of
pre-commit
you should use thecommit-msg
hook.The
pre-commit
hook is triggered before there is a commit message yet, but if you use thecommit-msg
hook, it will be triggered after there is a commit message.The hook will trigered your script and the first argument your script will receive will be the commit message itself.
Then you can write any logic on top of the commit message. You can check the message, and update version based of the message as you requested. There are many ways to it
您可以使用 updateCommitId 模块编写自定义节点 JS 代码以在预提交挂钩期间运行。示例工作代码如下所示。请根据需要升级。
you can user updateCommitId modules to write custom node JS code to run during pre-commit hook. Sample working code can look like below. Please upgrade as deemed required.
如果您有任何类型的 CICD 管道(不完全限于 git hooks 范围),您可以使用 gitversion cli
它支持每个提交消息正则表达式的版本碰撞 配置示例:
意味着提交信息
我的提交消息+semver:修复
将触发补丁
版本碰撞,请参阅本文 包含详细的设置说明。
in case you have any kind of CICD pipeline (not entirely limited to git hooks scope) you can use gitversion cli
it supports version bump per commit message regex configuration example:
means that a commit with message
my commit msg +semver: fix
will trigger apatch
version bumpsee this article with detailed explanation for setup.