Product Help

Technical Procedure

GitHub Action For Incrementing Tags

Setup

You must create the directory .github/workfows at the top level of the project. Create a file in .github/workfows named action_name.yml that will hold the code for the action.

Top Level Sections

Include the following sections at the top level of the file, and replace the env variables with appropriate values for the repository.

name: Test Action permissions: contents: write on: push: branches: - main env: user: jweisnerAOH name: Jackson Weisner email: jweisner@aohwv.com repo: github-actions-test

Job Structure

The structure of the rest of the file is as following:

jobs: IncrementBuild: runs-on: ubuntu-latest steps: # steps for the IncrementBuild job

Increment Build Steps

Put each of these steps under the steps: section described above.

Checkout repository and fetch the latest tags.

- name: Checkout Repository uses: actions/checkout@v4 - name: Fetch Tags run: git fetch --tags

Get the most recently created tag, or default to v0.0.0 if no tags exist.

- name: Determine Latest Tag run: | latestTag=$(git for-each-ref --sort=-creatordate --format '%(refname:short)' refs/tags 2>/dev/null | head -n 1 || echo "v0.0.0") echo "Latest tag: $latestTag" echo "latestTag=$latestTag" >> $GITHUB_ENV

Parse the most recently created tag into three components major.minor.patch.

- name: Parse Latest Tag run: | echo "major=$(echo ${{ env.latestTag }} | cut -d '.' -f1 | sed 's/v//')" >> $GITHUB_ENV echo "minor=$(echo ${{ env.latestTag }} | cut -d '.' -f2)" >> $GITHUB_ENV echo "patch=$(echo ${{ env.latestTag }} | cut -d '.' -f3 | sed 's/-dev\|-test//')" >> $GITHUB_ENV

Grabs only the line of the commit message that contains information about the tag to create.

- name: Get Information Line From Commit Message run: | commitMessage=$(git log -1 --pretty=%B | grep -E "^Type: (patch|major|minor)(, Branch: (dev|test))?$") echo "commitMessage=$commitMessage" >> $GITHUB_ENV

Increment the tag version depending on what was passed into the first line of the commit. If a parent version is incremented, reset the child version (a major increment would reset both minor and patch to 0).

- name: Increment Version run: | if [[ "${{ env.commitMessage }}" =~ ^Type:\ patch ]]; then echo "patch=$((${{ env.patch }} + 1))" >> $GITHUB_ENV elif [[ "${{ env.commitMessage }}" =~ ^Type:\ minor ]]; then echo "minor=$((${{ env.minor }} + 1))" >> $GITHUB_ENV echo "patch=0" >> $GITHUB_ENV elif [[ "${{ env.commitMessage }}" =~ ^Type:\ major ]]; then echo "major=$((${{ env.major }} + 1))" >> $GITHUB_ENV echo "minor=0" >> $GITHUB_ENV echo "patch=0" >> $GITHUB_ENV fi

Get the optional suffix for the new tag. If no Branch is specified in the commit, there will be no tag suffix and the tag will be treated as a production version.

- name: Get Tag Suffix run: | suffix="" if [[ "${{ env.commitMessage }}" =~ Branch:\ dev$ ]]; then suffix="dev" elif [[ "${{ env.commitMessage }}" =~ Branch:\ test$ ]]; then suffix="test" fi echo "suffix=$suffix" >> $GITHUB_ENV

Put all parts together to form the new tag that will be pushed. Composes the tag in the form v<major>.<minor>.<patch>[-dev|test].

- name: Compose New Tag run: | newTag=$(echo "v${{ env.major }}.${{ env.minor }}.${{ env.patch }}") if [[ -n "${{ env.suffix }}" ]]; then newTag="${newTag}-${{ env.suffix }}" fi echo "newTag=$newTag" >> $GITHUB_ENV

Push the tag to the repository.

- name: Push New Tag run: | git config user.name ${{ env.name }} git config user.email ${{ env.email }} git remote set-url origin https://@github.com/${{ env.user }}/${{ env.repo }}.git git tag ${{ env.newTag }} git push origin tag ${{ env.newTag }}
Last modified: 28 January 2025