1+ name : Version Change Trigger
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-and-tag :
15+ name : Check Version and Create Tag
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+ if [ "$CURRENT_VERSION" != "$PREV_VERSION" ] || [ "$CURRENT_CODE" != "$PREV_CODE" ]; then
43+ echo "version_changed=true" >> $GITHUB_OUTPUT
44+ echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
45+ echo "version_code=$CURRENT_CODE" >> $GITHUB_OUTPUT
46+ else
47+ echo "version_changed=false" >> $GITHUB_OUTPUT
48+ fi
49+
50+ - name : Check if tag exists
51+ if : steps.version_check.outputs.version_changed == 'true'
52+ id : tag_check
53+ run : |
54+ TAG_NAME="v${{ steps.version_check.outputs.version }}"
55+ if git ls-remote --tags origin | grep -q "refs/tags/${TAG_NAME}$"; then
56+ echo "tag_exists=true" >> $GITHUB_OUTPUT
57+ else
58+ echo "tag_exists=false" >> $GITHUB_OUTPUT
59+ fi
60+
61+ - name : Create and push tag
62+ if : steps.version_check.outputs.version_changed == 'true' && steps.tag_check.outputs.tag_exists == 'false'
63+ run : |
64+ TAG_NAME="v${{ steps.version_check.outputs.version }}"
65+
66+ git config user.name "github-actions[bot]"
67+ git config user.email "github-actions[bot]@users.noreply.github.com"
68+
69+ # Create annotated tag
70+ git tag -a "${TAG_NAME}" -m "Release ${TAG_NAME} (code: ${{ steps.version_check.outputs.version_code }})"
71+ git push origin "${TAG_NAME}"
72+
73+ echo "✅ Created and pushed tag: ${TAG_NAME}"
74+
75+ - name : Summary
76+ if : steps.version_check.outputs.version_changed == 'true'
77+ run : |
78+ echo "## 📦 Version Update Detected" >> $GITHUB_STEP_SUMMARY
79+ echo "" >> $GITHUB_STEP_SUMMARY
80+ echo "- **Version**: v${{ steps.version_check.outputs.version }}" >> $GITHUB_STEP_SUMMARY
81+ echo "- **Version Code**: ${{ steps.version_check.outputs.version_code }}" >> $GITHUB_STEP_SUMMARY
82+ if [ "${{ steps.tag_check.outputs.tag_exists }}" == "false" ]; then
83+ echo "- **Tag**: ✅ Created v${{ steps.version_check.outputs.version }}" >> $GITHUB_STEP_SUMMARY
84+ echo "" >> $GITHUB_STEP_SUMMARY
85+ echo "The release workflow will be triggered automatically by the new tag." >> $GITHUB_STEP_SUMMARY
86+ else
87+ echo "- **Tag**: Already exists" >> $GITHUB_STEP_SUMMARY
88+ fi
0 commit comments