Skip to content

Commit

Permalink
chore: fix auto versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Jan 15, 2025
1 parent 4bd97d1 commit 3e25829
Showing 1 changed file with 68 additions and 57 deletions.
125 changes: 68 additions & 57 deletions .github/workflows/auto-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ on:
- '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)
Expand All @@ -33,7 +44,7 @@ jobs:
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
Expand Down Expand Up @@ -88,67 +99,67 @@ jobs:
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 }}
- 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"
# Remove 'v' prefix and split into array
VERSION=${LATEST_TAG#v}
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
echo "current_version=${VERSION}" >> $GITHUB_OUTPUT
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 increment logic
case $BUMP_TYPE in
"major")
((VERSION_PARTS[0]++)) # Increment major
VERSION_PARTS[1]=0 # Reset minor
VERSION_PARTS[2]=0 # Reset patch
else
((VERSION_PARTS[2]++)) # Increment patch for everything else
fi
;;
esac
;;
"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
# 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
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}
# 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
# 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
# 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

0 comments on commit 3e25829

Please sign in to comment.