在GitHub Actions中实现无发布无提交的版本控制方案咨询
在GitHub Actions中实现带版本段重置的自增版本号(Monorepo场景)
由于GitHub Actions没有Azure Pipelines那样内置的持久化counter函数,我们可以利用仓库级环境变量模拟这个功能,实现修改major/minor时自动重置patch计数,且无需提交代码变更。
核心逻辑
- 用仓库变量存储当前的
major、minor、patch版本值 - 流水线中定义目标
major/minor版本 - 对比目标版本与存储的当前版本:若major或minor变化,重置patch为0;否则patch自增1
- 将计算后的新版本写回仓库变量,供下次流水线使用
具体实现步骤
1. 初始化仓库变量
先手动在仓库的「Settings → Variables → Actions」中创建三个变量:
CURRENT_MAJOR:初始值如1CURRENT_MINOR:初始值如0CURRENT_PATCH:初始值如0
2. 编写Workflow代码
以下是完整的流水线示例,包含版本计算、更新和使用的全流程:
name: Monorepo Versioning Pipeline on: push: branches: [ main ] # 定义本次流水线目标的major/minor版本 env: TARGET_MAJOR: 1 TARGET_MINOR: 0 jobs: versioning: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 - name: 安装GitHub CLI run: | type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \ && sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \ && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ && sudo apt update && sudo apt install gh -y - name: 认证GitHub CLI run: echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token - name: 获取当前存储的版本值 id: get_current_version run: | CURRENT_MAJOR=$(gh variable get CURRENT_MAJOR -R ${{ github.repository }}) CURRENT_MINOR=$(gh variable get CURRENT_MINOR -R ${{ github.repository }}) CURRENT_PATCH=$(gh variable get CURRENT_PATCH -R ${{ github.repository }}) echo "current_major=$CURRENT_MAJOR" >> $GITHUB_OUTPUT echo "current_minor=$CURRENT_MINOR" >> $GITHUB_OUTPUT echo "current_patch=$CURRENT_PATCH" >> $GITHUB_OUTPUT - name: 计算新版本号 id: calculate_version run: | # 若major或minor变更,重置patch为0;否则patch自增 if [ "${{ env.TARGET_MAJOR }}" != "${{ steps.get_current_version.outputs.current_major }}" ] || [ "${{ env.TARGET_MINOR }}" != "${{ steps.get_current_version.outputs.current_minor }}" ]; then NEW_PATCH=0 else NEW_PATCH=$(( ${{ steps.get_current_version.outputs.current_patch }} + 1 )) fi NEW_MAJOR=${{ env.TARGET_MAJOR }} NEW_MINOR=${{ env.TARGET_MINOR }} echo "new_major=$NEW_MAJOR" >> $GITHUB_OUTPUT echo "new_minor=$NEW_MINOR" >> $GITHUB_OUTPUT echo "new_patch=$NEW_PATCH" >> $GITHUB_OUTPUT echo "full_version=$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH" >> $GITHUB_OUTPUT - name: 更新仓库变量 run: | gh variable set CURRENT_MAJOR -R ${{ github.repository }} -b "${{ steps.calculate_version.outputs.new_major }}" gh variable set CURRENT_MINOR -R ${{ github.repository }} -b "${{ steps.calculate_version.outputs.new_minor }}" gh variable set CURRENT_PATCH -R ${{ github.repository }} -b "${{ steps.calculate_version.outputs.new_patch }}" - name: 使用新版本号 run: | echo "生成的版本号:${{ steps.calculate_version.outputs.full_version }}" # 这里添加后续步骤,比如给Monorepo里的项目打包、生成标记等
扩展说明
- 多项目版本隔离:如果Monorepo里的多个项目需要独立版本,可以为每个项目创建单独的GitHub环境,把版本变量存在对应环境中,修改CLI命令时加上
--env <项目环境名>参数即可。 - 权限说明:默认的
GITHUB_TOKEN已经拥有修改仓库变量的权限,不需要额外配置。 - 版本号来源:
TARGET_MAJOR和TARGET_MINOR也可以通过流水线输入参数、分支名规则或者配置文件读取,灵活适配不同的版本管理策略。
内容的提问来源于stack exchange,提问作者lpetrucci
相关产品推荐
相关产品推荐

