Skip to content

Commit d996637

Browse files
committed
feat: implement version-change-triggered release workflow
- Add release-on-version-change.yml that monitors config.gradle changes - Auto-creates tags when version is updated in config.gradle - Simplifies release.yml by removing version update logic - Better workflow: update version -> auto tag -> auto release
1 parent 1d293ed commit d996637

File tree

2 files changed

+117
-47
lines changed

2 files changed

+117
-47
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Release on Version Change
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'config.gradle'
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
check-version-and-release:
15+
name: Check Version and Release
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 2 # Need previous commit for comparison
23+
24+
- name: Check if version changed
25+
id: version_check
26+
run: |
27+
# Get version from current commit
28+
CURRENT_VERSION=$(grep versionName config.gradle | cut -d'"' -f2)
29+
CURRENT_CODE=$(grep versionCode config.gradle | grep -o '[0-9]*')
30+
31+
# Get version from previous commit
32+
git checkout HEAD~1 config.gradle 2>/dev/null || true
33+
PREV_VERSION=$(grep versionName config.gradle | cut -d'"' -f2 || echo "")
34+
PREV_CODE=$(grep versionCode config.gradle | grep -o '[0-9]*' || echo "0")
35+
36+
# Restore current version
37+
git checkout HEAD config.gradle
38+
39+
echo "Previous: v$PREV_VERSION (code: $PREV_CODE)"
40+
echo "Current: v$CURRENT_VERSION (code: $CURRENT_CODE)"
41+
42+
# Check if version actually changed
43+
if [ "$CURRENT_VERSION" != "$PREV_VERSION" ] || [ "$CURRENT_CODE" != "$PREV_CODE" ]; then
44+
echo "✅ Version changed from $PREV_VERSION to $CURRENT_VERSION"
45+
echo "version_changed=true" >> $GITHUB_OUTPUT
46+
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
47+
echo "version_code=$CURRENT_CODE" >> $GITHUB_OUTPUT
48+
49+
# Check if this is a version increment (not decrement)
50+
if [ "$CURRENT_CODE" -gt "$PREV_CODE" ]; then
51+
echo "release_type=production" >> $GITHUB_OUTPUT
52+
else
53+
echo "⚠️ Version code decreased or same - skipping release"
54+
echo "version_changed=false" >> $GITHUB_OUTPUT
55+
fi
56+
else
57+
echo "ℹ️ No version change detected"
58+
echo "version_changed=false" >> $GITHUB_OUTPUT
59+
fi
60+
61+
- name: Check if tag exists
62+
if: steps.version_check.outputs.version_changed == 'true'
63+
id: tag_check
64+
run: |
65+
TAG_NAME="v${{ steps.version_check.outputs.version }}"
66+
if git ls-remote --tags origin | grep -q "refs/tags/${TAG_NAME}$"; then
67+
echo "⚠️ Tag ${TAG_NAME} already exists"
68+
echo "tag_exists=true" >> $GITHUB_OUTPUT
69+
else
70+
echo "✅ Tag ${TAG_NAME} does not exist, will create"
71+
echo "tag_exists=false" >> $GITHUB_OUTPUT
72+
echo "tag_name=${TAG_NAME}" >> $GITHUB_OUTPUT
73+
fi
74+
75+
- name: Create and push tag
76+
if: steps.version_check.outputs.version_changed == 'true' && steps.tag_check.outputs.tag_exists == 'false'
77+
run: |
78+
TAG_NAME="${{ steps.tag_check.outputs.tag_name }}"
79+
80+
git config user.name "github-actions[bot]"
81+
git config user.email "github-actions[bot]@users.noreply.github.com"
82+
83+
# Create annotated tag
84+
git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME} (version code: ${{ steps.version_check.outputs.version_code }})"
85+
git push origin "${TAG_NAME}"
86+
87+
echo "🏷️ Created and pushed tag: ${TAG_NAME}"
88+
echo "🚀 This will trigger the release workflow automatically"
89+
90+
- name: Summary
91+
if: always()
92+
run: |
93+
echo "## 📦 Version Change Detection" >> $GITHUB_STEP_SUMMARY
94+
echo "" >> $GITHUB_STEP_SUMMARY
95+
96+
if [ "${{ steps.version_check.outputs.version_changed }}" == "true" ]; then
97+
echo "✅ **Version changed to: v${{ steps.version_check.outputs.version }}**" >> $GITHUB_STEP_SUMMARY
98+
echo "- Version Code: ${{ steps.version_check.outputs.version_code }}" >> $GITHUB_STEP_SUMMARY
99+
100+
if [ "${{ steps.tag_check.outputs.tag_exists }}" == "false" ]; then
101+
echo "- Tag Created: ${{ steps.tag_check.outputs.tag_name }}" >> $GITHUB_STEP_SUMMARY
102+
echo "" >> $GITHUB_STEP_SUMMARY
103+
echo "### Next Steps" >> $GITHUB_STEP_SUMMARY
104+
echo "The release workflow will be triggered automatically by the new tag." >> $GITHUB_STEP_SUMMARY
105+
else
106+
echo "- Tag Status: Already exists (skipped)" >> $GITHUB_STEP_SUMMARY
107+
fi
108+
else
109+
echo "ℹ️ No version change detected in config.gradle" >> $GITHUB_STEP_SUMMARY
110+
fi

.github/workflows/release.yml

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ jobs:
3838
runs-on: ubuntu-latest
3939
outputs:
4040
version: ${{ steps.version.outputs.version }}
41-
version_code: ${{ steps.auto_update.outputs.version_code }}
41+
version_code: ${{ steps.version_info.outputs.version_code }}
4242

4343
steps:
4444
- name: Checkout code
4545
uses: actions/checkout@v4
46-
with:
47-
token: ${{ secrets.GITHUB_TOKEN }}
48-
ref: main
49-
fetch-depth: 0
5046

5147
- name: Determine version
5248
id: version
@@ -59,45 +55,13 @@ jobs:
5955
echo "version=$VERSION" >> $GITHUB_OUTPUT
6056
echo "Version: $VERSION"
6157
62-
- name: Auto update version in config.gradle
63-
id: auto_update
58+
- name: Extract version code
59+
id: version_info
6460
run: |
65-
VERSION="${{ steps.version.outputs.version }}"
66-
VERSION_NAME="${VERSION#v}" # Remove 'v' prefix if present
67-
68-
# Get current version info from config.gradle
69-
CURRENT_VERSION=$(grep versionName config.gradle | cut -d'"' -f2)
70-
CURRENT_CODE=$(grep versionCode config.gradle | grep -o '[0-9]*')
71-
72-
echo "Current version: $CURRENT_VERSION (code: $CURRENT_CODE)"
73-
echo "Target version: $VERSION_NAME"
74-
75-
# Check if version needs updating
76-
if [ "$CURRENT_VERSION" = "$VERSION_NAME" ]; then
77-
echo "✅ Version already matches $VERSION_NAME"
78-
echo "version_code=$CURRENT_CODE" >> $GITHUB_OUTPUT
79-
echo "updated=false" >> $GITHUB_OUTPUT
80-
else
81-
# Auto-increment version code
82-
NEW_CODE=$((CURRENT_CODE + 1))
83-
echo "📝 Updating version to $VERSION_NAME (code: $NEW_CODE)"
84-
85-
# Update config.gradle
86-
sed -i "s/versionCode: [0-9]*/versionCode: $NEW_CODE/" config.gradle
87-
sed -i "s/versionName: \"[^\"]*\"/versionName: \"$VERSION_NAME\"/" config.gradle
88-
89-
# Commit and push the changes
90-
git config user.name "github-actions[bot]"
91-
git config user.email "github-actions[bot]@users.noreply.github.com"
92-
93-
git add config.gradle
94-
git commit -m "chore: auto-update version to $VERSION_NAME (code: $NEW_CODE) [skip ci]"
95-
git push origin main
96-
97-
echo "✅ Version updated and pushed to main"
98-
echo "version_code=$NEW_CODE" >> $GITHUB_OUTPUT
99-
echo "updated=true" >> $GITHUB_OUTPUT
100-
fi
61+
# Extract version code from config.gradle
62+
VERSION_CODE=$(grep "versionCode:" config.gradle | sed 's/.*versionCode: \([0-9]*\).*/\1/')
63+
echo "version_code=$VERSION_CODE" >> $GITHUB_OUTPUT
64+
echo "Version Code: $VERSION_CODE"
10165
10266
build-apk:
10367
name: Build Release APK
@@ -107,8 +71,6 @@ jobs:
10771
steps:
10872
- name: Checkout code
10973
uses: actions/checkout@v4
110-
with:
111-
ref: main # Always use main branch with updated version
11274

11375
- name: Set up JDK 17
11476
uses: actions/setup-java@v4
@@ -184,8 +146,6 @@ jobs:
184146
steps:
185147
- name: Checkout code
186148
uses: actions/checkout@v4
187-
with:
188-
ref: main # Always use main branch with updated version
189149

190150
- name: Set up JDK 17
191151
uses: actions/setup-java@v4

0 commit comments

Comments
 (0)