chore: fix auto versioning #15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto Version | |
on: | |
push: | |
branches: | |
- main | |
- develop | |
- 'chore/**' | |
- 'hotfix/**' | |
- 'fix/**' | |
- 'feature/**' | |
- 'release/**' | |
pull_request: | |
types: [ closed ] | |
jobs: | |
wait-for-ci: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Wait for CI workflow | |
uses: lewagon/[email protected] | |
with: | |
ref: ${{ github.ref }} | |
check-name: 'gradle' | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
wait-interval: 10 | |
auto-version: | |
needs: [ wait-for-ci ] | |
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 | |
fetch-tags: true | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Get version info | |
id: get_version_info | |
run: | | |
git config --local user.email "${GITHUB_ACTOR}@users.noreply.github.com" | |
git config --local user.name "${GITHUB_ACTOR}" | |
# Handle both PR and push events for branch name | |
EVENT_NAME="${{ github.event_name }}" | |
if [[ "$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 | |
echo "Available tags:" | |
if ! git tag --list | grep -q "^v"; then | |
echo "No version tags found, creating initial tag v0.0.1" | |
git tag -a "v0.0.1" -m "Initial version" | |
git push origin v0.0.1 | |
LATEST_TAG="v0.0.1" | |
else | |
git tag --sort=-v:refname | |
echo "Filtered tags:" | |
git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" || echo "No matching tags" | |
LATEST_TAG=$(git tag --sort=-v:refname | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+" | head -n1 || echo "v0.0.1") | |
fi | |
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=${{ steps.get_version_info.outputs.latest_tag }} | |
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" | |
echo "current_version=${VERSION}" >> $GITHUB_OUTPUT | |
# 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 | |
# Create tag for version tracking | |
git tag -a ${NEW_VERSION} -m "Release ${NEW_VERSION}" | |
git push origin ${NEW_VERSION} | |
# Only update build.gradle.kts version on develop or main branch | |
if [[ $BRANCH_NAME == "develop" ]] || [[ $BRANCH_NAME == "main" ]]; then | |
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 | |
# Amend the current commit with the version update | |
git add build.gradle.kts | |
git commit --amend --no-edit | |
git push --force-with-lease origin HEAD | |
fi |