From 3e258296e6cead855ca7f6f35b800ed991102d1c Mon Sep 17 00:00:00 2001 From: John Melati Date: Wed, 15 Jan 2025 18:52:44 +0100 Subject: [PATCH] chore: fix auto versioning --- .github/workflows/auto-version.yml | 125 ++++++++++++++++------------- 1 file changed, 68 insertions(+), 57 deletions(-) diff --git a/.github/workflows/auto-version.yml b/.github/workflows/auto-version.yml index 4b65624..ffbe707 100644 --- a/.github/workflows/auto-version.yml +++ b/.github/workflows/auto-version.yml @@ -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/wait-on-check-action@v1.3.1 + 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) @@ -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 @@ -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 \ No newline at end of file + # 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 \ No newline at end of file