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
0 commit comments