@@ -2,8 +2,10 @@ name: Release
22
33on :
44 push :
5- tags :
6- - ' v*'
5+ branches :
6+ - main
7+ paths :
8+ - ' config.gradle'
79 workflow_dispatch :
810 inputs :
911 version :
@@ -39,33 +41,131 @@ jobs:
3941 outputs :
4042 version : ${{ steps.version.outputs.version }}
4143 version_code : ${{ steps.version_info.outputs.version_code }}
44+ should_release : ${{ steps.check_version.outputs.should_release }}
4245
4346 steps :
4447 - name : Checkout code
4548 uses : actions/checkout@v4
49+ with :
50+ fetch-depth : 2 # Need history for version comparison
51+ token : ${{ secrets.GITHUB_TOKEN }}
4652
53+ - name : Check version change
54+ id : check_version
55+ if : github.event_name == 'push'
56+ run : |
57+ # Get current version from config.gradle with error handling
58+ CURRENT_VERSION=$(grep versionName config.gradle | sed -E 's/.*versionName:\s*"([^"]+)".*/\1/' || echo "")
59+ CURRENT_CODE=$(grep versionCode config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/' || echo "")
60+
61+ # Validate extraction was successful
62+ if [ -z "$CURRENT_VERSION" ] || [ -z "$CURRENT_CODE" ]; then
63+ echo "❌ Error: Failed to extract version information from config.gradle"
64+ echo "CURRENT_VERSION='$CURRENT_VERSION', CURRENT_CODE='$CURRENT_CODE'"
65+ echo "should_release=false" >> $GITHUB_OUTPUT
66+ exit 1
67+ fi
68+
69+ # Validate CURRENT_CODE is a valid number
70+ if ! [[ "$CURRENT_CODE" =~ ^[0-9]+$ ]]; then
71+ echo "❌ Error: Version code is not a valid number: $CURRENT_CODE"
72+ echo "should_release=false" >> $GITHUB_OUTPUT
73+ exit 1
74+ fi
75+
76+ # Get previous version - handle first commit case
77+ if git rev-parse HEAD~1 >/dev/null 2>&1; then
78+ git show HEAD~1:config.gradle > prev_config.gradle 2>/dev/null || echo "No previous config.gradle"
79+ if [ -f prev_config.gradle ]; then
80+ PREV_VERSION=$(grep versionName prev_config.gradle | sed -E 's/.*versionName:\s*"([^"]+)".*/\1/' || echo "")
81+ PREV_CODE=$(grep versionCode prev_config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/' || echo "0")
82+ rm prev_config.gradle
83+ else
84+ PREV_VERSION=""
85+ PREV_CODE="0"
86+ fi
87+ else
88+ # First commit in repo
89+ PREV_VERSION=""
90+ PREV_CODE="0"
91+ fi
92+
93+ # Validate PREV_CODE is a valid number (default to 0 if not)
94+ if ! [[ "$PREV_CODE" =~ ^[0-9]+$ ]]; then
95+ PREV_CODE="0"
96+ fi
97+
98+ echo "Previous: v$PREV_VERSION (code: $PREV_CODE)"
99+ echo "Current: v$CURRENT_VERSION (code: $CURRENT_CODE)"
100+
101+ # Check if version increased
102+ if [ "$CURRENT_CODE" -gt "$PREV_CODE" ]; then
103+ echo "✅ Version increased from $PREV_VERSION to $CURRENT_VERSION"
104+ echo "should_release=true" >> $GITHUB_OUTPUT
105+ echo "version=v$CURRENT_VERSION" >> $GITHUB_OUTPUT
106+ echo "version_code=$CURRENT_CODE" >> $GITHUB_OUTPUT
107+ else
108+ echo "ℹ️ No version increase detected"
109+ echo "should_release=false" >> $GITHUB_OUTPUT
110+ exit 0
111+ fi
112+
47113 - name : Determine version
48114 id : version
49115 run : |
50116 if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
51117 VERSION="${{ github.event.inputs.version }}"
118+ echo "should_release=true" >> $GITHUB_OUTPUT
119+ elif [ "${{ steps.check_version.outputs.should_release }}" = "true" ]; then
120+ VERSION="${{ steps.check_version.outputs.version }}"
52121 else
53- VERSION="${{ github.ref_name }}"
122+ echo "No release needed"
123+ exit 0
54124 fi
55125 echo "version=$VERSION" >> $GITHUB_OUTPUT
56126 echo "Version: $VERSION"
57127
58128 - name : Extract version code
59129 id : version_info
60130 run : |
61- # Extract version code from config.gradle
62- VERSION_CODE=$(grep "versionCode:" config.gradle | sed 's/.*versionCode: \([0-9]*\).*/\1/')
131+ # Use version code from check_version step if available, otherwise extract it
132+ if [ -n "${{ steps.check_version.outputs.version_code }}" ]; then
133+ VERSION_CODE="${{ steps.check_version.outputs.version_code }}"
134+ else
135+ # Only extract if not already available (for workflow_dispatch)
136+ VERSION_CODE=$(grep "versionCode:" config.gradle | sed -E 's/.*versionCode:\s*([0-9]+).*/\1/')
137+ fi
63138 echo "version_code=$VERSION_CODE" >> $GITHUB_OUTPUT
64139 echo "Version Code: $VERSION_CODE"
140+
141+ - name : Create and push tag
142+ if : steps.check_version.outputs.should_release == 'true' && github.event_name == 'push'
143+ id : create_tag
144+ run : |
145+ TAG_NAME="${{ steps.version.outputs.version }}"
146+
147+ # Check if tag already exists
148+ if git ls-remote --tags origin | grep -q "refs/tags/${TAG_NAME}$"; then
149+ echo "⚠️ Tag $TAG_NAME already exists, skipping tag creation"
150+ echo "tag_created=false" >> $GITHUB_OUTPUT
151+ echo "tag_exists=true" >> $GITHUB_OUTPUT
152+ else
153+ git config user.name "github-actions[bot]"
154+ git config user.email "github-actions[bot]@users.noreply.github.com"
155+
156+ # Create annotated tag
157+ git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME} (version code: ${{ steps.version_info.outputs.version_code }})"
158+ git push origin "${TAG_NAME}"
159+
160+ echo "🏷️ Created and pushed tag: ${TAG_NAME}"
161+ echo "tag_created=true" >> $GITHUB_OUTPUT
162+ echo "tag_exists=false" >> $GITHUB_OUTPUT
163+ fi
65164
66165 build-apk :
67166 name : Build Release APK
68167 needs : prepare
168+ if : needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch'
69169 runs-on : ubuntu-latest
70170
71171 steps :
@@ -141,6 +241,7 @@ jobs:
141241 build-aab :
142242 name : Build Release Bundle
143243 needs : prepare
244+ if : needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch'
144245 runs-on : ubuntu-latest
145246
146247 steps :
@@ -203,6 +304,7 @@ jobs:
203304 release :
204305 name : Create GitHub Release
205306 needs : [prepare, build-apk, build-aab]
307+ if : needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch'
206308 runs-on : ubuntu-latest
207309
208310 steps :
@@ -276,7 +378,7 @@ jobs:
276378 play-store-upload :
277379 name : Upload to Play Store
278380 needs : [prepare, build-aab]
279- if : ${{ vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
381+ if : ${{ (needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch') && vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
280382 runs-on : ubuntu-latest
281383
282384 steps :
@@ -386,7 +488,7 @@ jobs:
386488 download-signed-apk :
387489 name : Download Google Play Signed APK
388490 needs : [prepare, play-store-upload]
389- if : ${{ vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
491+ if : ${{ (needs.prepare.outputs.should_release == 'true' || github.event_name == 'workflow_dispatch') && vars.ENABLE_PLAY_STORE_UPLOAD == 'true' && vars.ENABLE_SIGNING == 'true' }}
390492 runs-on : ubuntu-latest
391493
392494 steps :
0 commit comments