From 5a2023e94a2a30917d083aee81d8da0accebe8df Mon Sep 17 00:00:00 2001 From: Rui <102453770+ruixhuang@users.noreply.github.com> Date: Wed, 22 May 2024 18:21:46 -0700 Subject: [PATCH] Add Github Action to automatically bump version at PR merge (#378) --- .github/workflows/bump_version.yml | 31 ++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 4 ++-- bump_version_gh_action.sh | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/bump_version.yml create mode 100755 bump_version_gh_action.sh diff --git a/.github/workflows/bump_version.yml b/.github/workflows/bump_version.yml new file mode 100644 index 000000000..f73020cf9 --- /dev/null +++ b/.github/workflows/bump_version.yml @@ -0,0 +1,31 @@ +name: Bump version on PR merge +on: + pull_request: + branches: + - main + types: closed + +permissions: + contents: write + +jobs: + update_version: + if: github.event.pull_request.merged == true + runs-on: macos-latest + steps: + - name: checkout + uses: actions/checkout@v4 + with: + # Fetch full depth, otherwise the last step overwrites the last commit's parent, essentially removing the graph. + fetch-depth: 0 + + - name: Run bump_version_gh_action.sh + run: | + bash ./bump_version_gh_action.sh + - name: Amend the last commit + run: | + git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com" + git config --global user.name "${GITHUB_ACTOR}" + git commit -a --amend --no-edit + git push --force-with-lease + echo "Complete" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c44991181..6771876f0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,7 +10,7 @@ jobs: runs-on: macos-latest steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v3 @@ -25,7 +25,7 @@ jobs: runs-on: macos-latest steps: - name: Checkout Repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v3 diff --git a/bump_version_gh_action.sh b/bump_version_gh_action.sh new file mode 100755 index 000000000..069819e32 --- /dev/null +++ b/bump_version_gh_action.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# search for the first line that starts with "version" in build.gradle.kts +# get the value in the quotes +VERSION=$(grep "^version = " build.gradle.kts | sed -n 's/version = "\(.*\)"/\1/p') + +# increment the version number +NEW_VERSION=$(echo $VERSION | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g') + +#if NEW_VERSION is not empty, replace the version in build.gradle.kts +if [ -n "$NEW_VERSION" ]; then + sed -i '' "s/version = \"$VERSION\"/version = \"$NEW_VERSION\"/" build.gradle.kts + echo "Version bumped to $NEW_VERSION" +fi