Skip to content

Commit 86227c8

Browse files
committed
refactor: integrate version monitoring into single release workflow
- Merge version change detection directly into release.yml - Remove separate release-on-version-change.yml workflow - Fix Copilot review comments: - Handle first commit case properly (no HEAD~1) - Use more robust regex patterns for gradle parsing - Add tag existence check to prevent race conditions - Workflow now triggers on: - Tag push (v*) - config.gradle changes on main branch - Manual workflow dispatch - Auto-creates tags when version increases in config.gradle
1 parent d996637 commit 86227c8

File tree

2 files changed

+80
-115
lines changed

2 files changed

+80
-115
lines changed

.github/workflows/release-on-version-change.yml

Lines changed: 0 additions & 110 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
push:
55
tags:
66
- 'v*'
7+
branches:
8+
- main
9+
paths:
10+
- 'config.gradle'
711
workflow_dispatch:
812
inputs:
913
version:
@@ -39,33 +43,102 @@ jobs:
3943
outputs:
4044
version: ${{ steps.version.outputs.version }}
4145
version_code: ${{ steps.version_info.outputs.version_code }}
46+
should_release: ${{ steps.check_version.outputs.should_release }}
4247

4348
steps:
4449
- name: Checkout code
4550
uses: actions/checkout@v4
51+
with:
52+
fetch-depth: 2 # Need history for version comparison
4653

54+
- name: Check if version changed (for config.gradle trigger)
55+
id: check_version
56+
if: github.event_name == 'push' && contains(github.event.head_commit.modified, 'config.gradle')
57+
run: |
58+
# Get current version from config.gradle
59+
CURRENT_VERSION=$(grep versionName config.gradle | sed -E 's/.*versionName:\s*"([^"]+)".*/\1/')
60+
CURRENT_CODE=$(grep versionCode config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/')
61+
62+
# Get previous version - handle first commit case
63+
if git rev-parse HEAD~1 >/dev/null 2>&1; then
64+
git show HEAD~1:config.gradle > prev_config.gradle 2>/dev/null || echo "No previous config.gradle"
65+
if [ -f prev_config.gradle ]; then
66+
PREV_VERSION=$(grep versionName prev_config.gradle | sed -E 's/.*versionName:\s*"([^"]+)".*/\1/' || echo "")
67+
PREV_CODE=$(grep versionCode prev_config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/' || echo "0")
68+
rm prev_config.gradle
69+
else
70+
PREV_VERSION=""
71+
PREV_CODE="0"
72+
fi
73+
else
74+
# First commit in repo
75+
PREV_VERSION=""
76+
PREV_CODE="0"
77+
fi
78+
79+
echo "Previous: v$PREV_VERSION (code: $PREV_CODE)"
80+
echo "Current: v$CURRENT_VERSION (code: $CURRENT_CODE)"
81+
82+
# Check if version increased
83+
if [ -n "$CURRENT_VERSION" ] && [ "$CURRENT_CODE" -gt "$PREV_CODE" ]; then
84+
echo "✅ Version increased from $PREV_VERSION to $CURRENT_VERSION"
85+
echo "should_release=true" >> $GITHUB_OUTPUT
86+
echo "version=v$CURRENT_VERSION" >> $GITHUB_OUTPUT
87+
88+
# Check if tag already exists
89+
if git ls-remote --tags origin | grep -q "refs/tags/v${CURRENT_VERSION}$"; then
90+
echo "⚠️ Tag v$CURRENT_VERSION already exists, skipping release"
91+
echo "should_release=false" >> $GITHUB_OUTPUT
92+
fi
93+
else
94+
echo "ℹ️ No version increase detected or invalid version"
95+
echo "should_release=false" >> $GITHUB_OUTPUT
96+
fi
97+
4798
- name: Determine version
4899
id: version
49100
run: |
50101
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
51102
VERSION="${{ github.event.inputs.version }}"
52-
else
103+
echo "should_release=true" >> $GITHUB_OUTPUT
104+
elif [ "${{ steps.check_version.outputs.should_release }}" = "true" ]; then
105+
VERSION="${{ steps.check_version.outputs.version }}"
106+
elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
53107
VERSION="${{ github.ref_name }}"
108+
echo "should_release=true" >> $GITHUB_OUTPUT
109+
else
110+
VERSION="v0.0.0"
111+
echo "should_release=false" >> $GITHUB_OUTPUT
54112
fi
55113
echo "version=$VERSION" >> $GITHUB_OUTPUT
56114
echo "Version: $VERSION"
57115
58116
- name: Extract version code
59117
id: version_info
60118
run: |
61-
# Extract version code from config.gradle
62-
VERSION_CODE=$(grep "versionCode:" config.gradle | sed 's/.*versionCode: \([0-9]*\).*/\1/')
119+
# Extract version code from config.gradle - handle both formats
120+
VERSION_CODE=$(grep "versionCode:" config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/')
63121
echo "version_code=$VERSION_CODE" >> $GITHUB_OUTPUT
64122
echo "Version Code: $VERSION_CODE"
123+
124+
- name: Create tag if version changed
125+
if: steps.check_version.outputs.should_release == 'true' && github.event_name == 'push' && contains(github.event.head_commit.modified, 'config.gradle')
126+
run: |
127+
TAG_NAME="${{ steps.version.outputs.version }}"
128+
129+
git config user.name "github-actions[bot]"
130+
git config user.email "github-actions[bot]@users.noreply.github.com"
131+
132+
# Create annotated tag
133+
git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME} (version code: ${{ steps.version_info.outputs.version_code }})"
134+
git push origin "${TAG_NAME}"
135+
136+
echo "🏷️ Created and pushed tag: ${TAG_NAME}"
65137
66138
build-apk:
67139
name: Build Release APK
68140
needs: prepare
141+
if: needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
69142
runs-on: ubuntu-latest
70143

71144
steps:
@@ -141,6 +214,7 @@ jobs:
141214
build-aab:
142215
name: Build Release Bundle
143216
needs: prepare
217+
if: needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
144218
runs-on: ubuntu-latest
145219

146220
steps:
@@ -203,6 +277,7 @@ jobs:
203277
release:
204278
name: Create GitHub Release
205279
needs: [prepare, build-apk, build-aab]
280+
if: needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
206281
runs-on: ubuntu-latest
207282

208283
steps:
@@ -276,7 +351,7 @@ jobs:
276351
play-store-upload:
277352
name: Upload to Play Store
278353
needs: [prepare, build-aab]
279-
if: ${{ vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
354+
if: ${{ (needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')) && vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
280355
runs-on: ubuntu-latest
281356

282357
steps:
@@ -386,7 +461,7 @@ jobs:
386461
download-signed-apk:
387462
name: Download Google Play Signed APK
388463
needs: [prepare, play-store-upload]
389-
if: ${{ vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
464+
if: ${{ (needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')) && vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
390465
runs-on: ubuntu-latest
391466

392467
steps:

0 commit comments

Comments
 (0)