Categorygithub.com/leonsteinhaeuser/git-tag-bump
modulepackage
1.1.2
Repository: https://github.com/leonsteinhaeuser/git-tag-bump.git
Documentation: pkg.go.dev

# README

git-tag-bump

testing

A simple tool to bump git tags (semver). It can be used to bump the patch, minor or major version of a tag. It can also be used to automatically determine the next version based on the last tag and the current branch name.

Arguments

FlagTypeRequiredDefaultDescription
--auto-bumpboolfalsefalseAutomatically determine the next version based on the last tag and the branch name passed to it.
--bumpstringfalsepatchThe part of the version to bump. Can be patch, minor, major or none. none is a special indicator to keep the current tag version, e.g. to bump the pre-release version. none should always be used together with --git-base-tag, which can based on a branch name, for example.
--configstringfalse``The path to the config file. If not defined, the default config will be used.
--pre-releaseboolfalsefalseWhether to create a pre-release tag.
--pre-release-formatstringfalsesemverThe format of the pre-release tag. Can be semver, date or datetime
--pre-release-prefixstringfalsercThe prefix of the pre-release tag. Example: When defining the following tag v1.0.0-rc.1, rc would be the prefix and the number after it the format semver.
--repo-pathstringfalse.The path to the git repository. If not defined, the current working directory will be used.
--createboolfalsefalseWhether to create and push the tag if it does not exist. Requires --actor-name, --actor-mail and the env variable GITHUB_TOKEN to be set.
--actor-namestringfalse``The name of the actor used to create the tag. Only used if --create is set.
--actor-emailstringfalse``The mail of the actor used to create the tag. Only used if --create is set.
--branch-namestringfalse``The name of the branch to use.
--v-prefixboolfalsetrueWhether to prefix the tag with v. Example: v1.0.0 instead of 1.0.0.
--git-base-tagstringfalse``Override the base tag to use for the bump. If not set, the latest tag will be used.

Environment Variables

VariableDescription
GITHUB_TOKENThe GitHub token used to authenticate with git in order to push the tag. Only necessary if --create is set.

Config

The config file is a simple YAML file. It can be used to define the branch name rules to determine the next version. The following example shows the default config:

major:
  branch:
    name:
      regex: '^(feat|feature|enh|enhanc|enhancement|fix|bugfix|chore)(\([a-z0-9-]+\)){0,1}!\/'

minor:
  branch:
    name:
      regex: '^(feat|feature)(\([a-z0-9-]+\)){0,1}\/'

patch:
  branch:
    name:
      regex: '(enh|enhanc|enhancement|fix|bugfix|chore)(\([a-z0-9-]+\)){0,1}\/'

The config file can be passed to the tool using the --config flag. If no config file is passed, the default config will be used.

The config file consists of three parts: major, minor and patch. Each part defines the rules for the corresponding version part. The rules are defined using regular expressions. The first rule that matches the branch name will be used to determine the next version.

What this means is that the following branch names will result in the following versions. In the following example, we assume that the last tag is v1.0.0.

Branch nameRelease TypeVersion
feat!/abcmajorv2.0.0
feat(ctx)!/abcmajorv2.0.0
feature!/abcmajorv2.0.0
feature(ctx)!/abcmajorv2.0.0
enh!/abcmajorv2.0.0
enh(ctx)!/abcmajorv2.0.0
enhanc!/abcmajorv2.0.0
enhanc(ctx)!/abcmajorv2.0.0
enhancement!/abcmajorv2.0.0
enhancement(ctx)!/abcmajorv2.0.0
fix!/abcmajorv2.0.0
fix(ctx)!/abcmajorv2.0.0
bugfix!/abcmajorv2.0.0
bugfix(ctx)!/abcmajorv2.0.0
chore!/abcmajorv2.0.0
chore(ctx)!/abcmajorv2.0.0
feat/abcminorv1.1.0
feat(ctx)/abcminorv1.1.0
feature/abcminorv1.1.0
feature(ctx)/abcminorv1.1.0
enhpatchv1.0.1
enh(ctx)patchv1.0.1
enhancpatchv1.0.1
enhanc(ctx)patchv1.0.1
enhancementpatchv1.0.1
enhancement(ctx)patchv1.0.1
fixpatchv1.0.1
fix(ctx)patchv1.0.1
bugfixpatchv1.0.1
bugfix(ctx)patchv1.0.1
chorepatchv1.0.1
chore(ctx)patchv1.0.1

Using the tool in a CI/CD pipeline

The tool can be used in a CI/CD pipeline to automatically determine the next version and create a tag for it. The following example shows how to use the tool in a GitHub CI/CD pipeline:

Manually create a tag based on the branch name:

name: Release

on:
  pull_request:
    types: [closed]

jobs:
  tag-branch:
    if: github.event.pull_request.merged == true && (github.base_ref == 'main' || github.base_ref == 'staging')
    runs-on: ubuntu-latest
    env:
      FROM_BRANCH: ${{ github.head_ref }}
      SHORT_BRANCH_NAME: ${{ github.base_ref }}
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          branch: ${{ env.SHORT_BRANCH_NAME }}

      - name: Extract branch name prefix
        run: |
          echo "BRANCH_NAME_PREFIX=$(echo ${{ env.FROM_BRANCH }} | cut -d'/' -f1)" >> $GITHUB_ENV

      - name: Is feature branch and does not contain a breaking change and is staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'feat') ||
          contains(env.BRANCH_NAME_PREFIX, 'feature') ||
          contains(env.BRANCH_NAME_PREFIX, 'chore') ||
          contains(env.BRANCH_NAME_PREFIX, 'refactor') ||
          contains(env.BRANCH_NAME_PREFIX, 'revert')) &&
          !endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'staging'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump patch --v-prefix --pre-release --create"
      - name: Is feature branch and does not contain a breaking change and is not staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'feat') ||
          contains(env.BRANCH_NAME_PREFIX, 'feature') ||
          contains(env.BRANCH_NAME_PREFIX, 'chore') ||
          contains(env.BRANCH_NAME_PREFIX, 'refactor') ||
          contains(env.BRANCH_NAME_PREFIX, 'revert')) &&
          !endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'main'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump patch --v-prefix --create"

      - name: Is patch branch and is staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'fix') ||
          contains(env.BRANCH_NAME_PREFIX, 'bugfix') ||
          contains(env.BRANCH_NAME_PREFIX, 'hotfix')) &&
          !endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'staging'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump minor --v-prefix --pre-release --create"
      - name: Is patch branch and is not staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'fix') ||
          contains(env.BRANCH_NAME_PREFIX, 'bugfix') ||
          contains(env.BRANCH_NAME_PREFIX, 'hotfix')) &&
          !endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'main'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump minor --v-prefix --create"

      - name: Is a breaking change and is staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'feat') ||
          contains(env.BRANCH_NAME_PREFIX, 'feature') ||
          contains(env.BRANCH_NAME_PREFIX, 'chore') ||
          contains(env.BRANCH_NAME_PREFIX, 'refactor') ||
          contains(env.BRANCH_NAME_PREFIX, 'revert') ||
          contains(env.BRANCH_NAME_PREFIX, 'fix') ||
          contains(env.BRANCH_NAME_PREFIX, 'bugfix') ||
          contains(env.BRANCH_NAME_PREFIX, 'hotfix')) &&
          endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'staging'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump major --v-prefix --pre-release --create"
      - name: Is a breaking change and is not staging
        if: |
          (contains(env.BRANCH_NAME_PREFIX, 'feat') ||
          contains(env.BRANCH_NAME_PREFIX, 'feature') ||
          contains(env.BRANCH_NAME_PREFIX, 'chore') ||
          contains(env.BRANCH_NAME_PREFIX, 'refactor') ||
          contains(env.BRANCH_NAME_PREFIX, 'revert') ||
          contains(env.BRANCH_NAME_PREFIX, 'fix') ||
          contains(env.BRANCH_NAME_PREFIX, 'bugfix') ||
          contains(env.BRANCH_NAME_PREFIX, 'hotfix')) &&
          endsWith(env.BRANCH_NAME_PREFIX, '!') && github.base_ref == 'main'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: "--bump major --v-prefix --create"

Automatically determine the next version and create a tag for it:

name: Release

on:
  pull_request:
    types: [closed]

jobs:
  tag-branch:
    if: github.event.pull_request.merged == true && (github.base_ref == 'main' || github.base_ref == 'staging')
    runs-on: ubuntu-latest
    env:
      FROM_BRANCH: ${{ github.head_ref }}
      SHORT_BRANCH_NAME: ${{ github.base_ref }}
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          branch: ${{ env.SHORT_BRANCH_NAME }}

      # feature --> staging
      - name: Pre-release
        if: github.base_ref == 'staging'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: >-
            --v-prefix
            --pre-release
            --pre-release-format 'datetime'
            --auto-bump
            --branch-name "${{ env.FROM_BRANCH }}"
            --create

      # staging --> main
      - name: Release
        if: github.head_ref == 'staging'
        uses: leonsteinhaeuser/[email protected]
        with:
          args: >-
            --v-prefix
            --pre-release
            --pre-release-format 'datetime'
            --auto-bump
            --branch-name "${{ env.FROM_BRANCH }}"
            --create

Create a Tag based on a release branch:

name: Create Tag

on:
  pull_request:
    types: [closed, opened, synchronize]

jobs:
  create-tag:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Split branch name
        shell: bash
        env:
          HEAD_REF: ${{ github.head_ref }}
        run: echo "RELEASE_TAG=${HEAD_REF##*/}" >> $GITHUB_ENV

      - name: Create Release Tag
        if: github.base_ref == 'main' && github.event.pull_request.merged == true
        uses: leonsteinhaeuser/[email protected]
        with:
          args: >-
            --v-prefix --create --actor-name "GitHub Actions" --actor-mail "github-actions[bot]@users.noreply.github.com"
            --git-base-tag "$RELEASE_TAG" --bump none

      - name: Create Pre-Release Tag
        if: contains(github.head_ref, 'release/') && github.event.pull_request.merged == false
        uses: leonsteinhaeuser/[email protected]
        with:
          args: >-
            --v-prefix --create --actor-name "GitHub Actions" --actor-mail "github-actions[bot]@users.noreply.github.com"
            --git-base-tag "$RELEASE_TAG" --bump none --pre-release

# Packages

No description provided by the author
No description provided by the author