conventional commits实践:配合GitHub Tag Action实现自动化版本管理
2026/7/28 23:14:51 网站建设 项目流程

conventional commits实践:配合GitHub Tag Action实现自动化版本管理

【免费下载链接】github-tag-actionA Github Action to automatically bump and tag master, on merge, with the latest SemVer formatted version. Works on any platform.项目地址: https://gitcode.com/gh_mirrors/git/github-tag-action

GitHub Tag Action是一款强大的工具,能在代码合并时自动为master分支生成符合SemVer规范的标签,实现自动化版本管理。本文将详细介绍如何结合Conventional Commits规范与GitHub Tag Action,轻松搞定项目版本管理。

什么是Conventional Commits规范?

Conventional Commits规范是一种基于提交消息的轻量级约定。它规定了提交消息的格式,使得自动化工具能够据此确定版本号变更和生成变更日志。遵循该规范的提交消息通常包含类型、作用域和描述等部分,例如feat(pencil): add 'graphiteWidth' option

GitHub Tag Action简介

GitHub Tag Action是一个GitHub Action,可在合并代码到指定分支时自动更新和推送Git标签。它基于SemVer格式化版本号,适用于任何平台。该工具通过分析提交消息来确定版本变更类型,如major、minor、patch等,并生成相应的标签。

GitHub Tag Action的主要功能

  • 根据提交消息自动确定版本变更类型
  • 生成符合SemVer规范的标签
  • 支持自定义标签前缀、分支规则等
  • 输出变更日志、新旧版本号等信息

快速开始:GitHub Tag Action的基本使用

要在项目中使用GitHub Tag Action,只需在工作流文件中添加以下步骤:

name: Bump version on: push: branches: - master jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Bump version and push tag id: tag_version uses: mathieudutour/github-tag-action@v6.2 with: github_token: ${{ secrets.GITHUB_TOKEN }} - name: Create a GitHub release uses: ncipollo/release-action@v1 with: tag: ${{ steps.tag_version.outputs.new_tag }} name: Release ${{ steps.tag_version.outputs.new_tag }} body: ${{ steps.tag_version.outputs.changelog }}

Conventional Commits与GitHub Tag Action的完美结合

提交消息格式详解

GitHub Tag Action使用semantic-release conventions来解析提交消息,默认采用Angular Commit Message Conventions。以下是不同提交消息对应的版本变更类型:

提交消息示例版本变更类型
fix(pencil): stop graphite breaking when too much pressure appliedPatch Release
feat(pencil): add 'graphiteWidth' optionMinor Release
perf(pencil): remove graphiteWidth option
BREAKING CHANGE: The graphiteWidth option has been removed.
Major Release

自定义提交规则

如果默认的提交规则不符合项目需求,可以通过custom_release_rules参数自定义。例如:

with: custom_release_rules: "hotfix:patch,pre-feat:preminor,bug:patch:Bug Fixes,chore:patch:Chores"

GitHub Tag Action的高级配置

分支管理

GitHub Tag Action允许通过参数指定哪些分支生成正式版本标签,哪些生成预发布标签:

  • release_branches: 生成正式版本标签的分支,默认值为master,main
  • pre_release_branches: 生成预发布版本标签的分支

标签自定义

可以通过以下参数自定义生成的标签:

  • tag_prefix: 标签前缀,默认值为v
  • append_to_pre_release_tag: 预发布标签后缀,默认值为分支名
  • create_annotated_tag: 是否创建带注释的标签,默认值为false

其他实用配置

  • fetch_all_tags: 是否获取所有标签,默认只获取最近100个标签
  • dry_run: 仅计算版本和变更日志,不实际创建标签
  • default_bump: 当没有明确的提交消息时使用的默认版本变更类型,默认值为patch

常见问题与解决方案

没有提交指定版本变更时如何处理?

如果没有提交指定版本变更,GitHub Tag Action会输出No commit specifies the version bump. Skipping the tag creation.。此时可以通过设置default_bump参数来指定默认的版本变更类型。

如何获取生成的版本信息?

GitHub Tag Action提供了多个输出参数,可以在后续步骤中使用:

  • new_tag: 新生成的标签
  • new_version: 新生成的版本号(不带前缀)
  • previous_tag: 上一个标签
  • release_type: 版本变更类型
  • changelog: 变更日志

总结

通过Conventional Commits规范与GitHub Tag Action的结合,我们可以实现版本管理的自动化,减少手动操作,提高开发效率。只需遵循简单的提交消息规范,GitHub Tag Action就能自动为我们生成版本标签和变更日志,让版本管理变得轻松简单。

要开始使用GitHub Tag Action,只需克隆仓库:git clone https://gitcode.com/gh_mirrors/git/github-tag-action,然后按照本文介绍的方法进行配置即可。

希望本文能帮助你更好地理解和使用Conventional Commits与GitHub Tag Action,实现高效的版本管理!

【免费下载链接】github-tag-actionA Github Action to automatically bump and tag master, on merge, with the latest SemVer formatted version. Works on any platform.项目地址: https://gitcode.com/gh_mirrors/git/github-tag-action

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询