Skip to content

chore: fix auto versioning #3

chore: fix auto versioning

chore: fix auto versioning #3

Workflow file for this run

name: Auto Version
on:
push:
branches:
- main
- develop
- 'chore/**'
- 'hotfix/**'
- 'fix/**'
- 'feature/**'
- 'release/**'
pull_request:
types: [ closed ]
jobs:
auto-version:
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.merged == true)
permissions:
contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Get version info
id: get_version_info
run: |
# Handle both PR and push events for branch name
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
BRANCH_NAME="${{ github.event.pull_request.head.ref }}"
else
BRANCH_NAME=${GITHUB_REF#refs/heads/}
fi
# Determine branch type prefix first
if [[ $BRANCH_NAME == "develop" ]]; then
PREFIX="dev"
elif [[ $BRANCH_NAME == "main" ]]; then
PREFIX="main"
elif [[ $BRANCH_NAME == feature/* ]]; then
PREFIX="feat"
elif [[ $BRANCH_NAME == hotfix/* ]]; then
PREFIX="fix"
elif [[ $BRANCH_NAME == release/* ]]; then
PREFIX="rel"
else
PREFIX="build"
fi
# Get latest version tag globally, ignoring branch prefixes
LATEST_TAG=$(git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" | head -n1 || echo "v0.0.0")
COMMIT_SHA=$(git rev-parse --short HEAD)
PR_NUMBER=${{ github.event.pull_request.number }}
COMMIT_MSG=$(git log -1 --pretty=%B)
# Version bump detection
if [[ $COMMIT_MSG == *"#major"* ]] || [[ $COMMIT_MSG == "BREAKING CHANGE:"* ]] || [[ $COMMIT_MSG == *"!"* ]]; then
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [[ $COMMIT_MSG == *"#minor"* ]] || [[ $COMMIT_MSG == "feat:"* ]]; then
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [[ $COMMIT_MSG == *"#patch"* ]] || [[ $COMMIT_MSG == "fix:"* ]]; then
echo "bump_type=patch" >> $GITHUB_OUTPUT
else
echo "bump_type=auto" >> $GITHUB_OUTPUT
fi
echo "latest_tag=${LATEST_TAG}" >> $GITHUB_OUTPUT
echo "commit_sha=${COMMIT_SHA}" >> $GITHUB_OUTPUT
echo "prefix=${PREFIX}" >> $GITHUB_OUTPUT
echo "pr_number=${PR_NUMBER}" >> $GITHUB_OUTPUT
- name: Bump version
id: bump_version
run: |
LATEST_TAG=$(git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" | head -n1)
if [ -z "$LATEST_TAG" ]; then
LATEST_TAG="v0.0.0"
echo "No existing version tag found, starting from ${LATEST_TAG}"
fi
COMMIT_SHA=${{ steps.get_version_info.outputs.commit_sha }}
PREFIX=${{ steps.get_version_info.outputs.prefix }}
PR_NUMBER=${{ steps.get_version_info.outputs.pr_number }}
BUMP_TYPE=${{ steps.get_version_info.outputs.bump_type }}
# Remove 'v' prefix and split into array
VERSION=${LATEST_TAG#v}
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
# Version increment logic
case $BUMP_TYPE in
"major")
((VERSION_PARTS[0]++)) # Increment major
VERSION_PARTS[1]=0 # Reset minor
VERSION_PARTS[2]=0 # Reset patch
;;
"minor")
((VERSION_PARTS[1]++)) # Increment minor
VERSION_PARTS[2]=0 # Reset patch
;;
"patch")
((VERSION_PARTS[2]++)) # Increment patch
;;
"auto")
# Default behavior based on branch type
if [[ $PREFIX == "rel" ]]; then
((VERSION_PARTS[1]++)) # Increment minor for release
VERSION_PARTS[2]=0 # Reset patch
else
((VERSION_PARTS[2]++)) # Increment patch for everything else
fi
;;
esac
# Create new version string
if [[ -n $PR_NUMBER ]]; then
NEW_VERSION="v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}-${PREFIX}.pr${PR_NUMBER}.${COMMIT_SHA}"
else
NEW_VERSION="v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}-${PREFIX}.${COMMIT_SHA}"
fi
echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT
# Configure git
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
# Update version in build.gradle.kts
VERSION_WITHOUT_V=${NEW_VERSION#v}
BASE_VERSION=$(echo $VERSION_WITHOUT_V | cut -d'-' -f1)
sed -i "s/version = \".*\"/version = \"$BASE_VERSION\"/" build.gradle.kts
# Stage the version update
git add build.gradle.kts
# Create commit and tag
git commit -m "chore: release version ${NEW_VERSION} [skip ci]"
git tag -a ${NEW_VERSION} -m "Release ${NEW_VERSION}"
# Push both the commit and tag
git push origin HEAD ${NEW_VERSION}