|
12 | 12 | steps:
|
13 | 13 | - name: Checkout repository
|
14 | 14 | uses: actions/checkout@v4
|
| 15 | + with: |
| 16 | + fetch-depth: 0 # Important: get all tags! |
| 17 | + |
| 18 | + - name: Get Latest Tag |
| 19 | + run: | |
| 20 | + latest_tag=$(git describe --tags --abbrev=0) |
| 21 | + echo "LATEST_TAG=${latest_tag}" >> $GITHUB_ENV |
| 22 | +
|
| 23 | + - name: Count Commits Since Latest Tag |
| 24 | + run: | |
| 25 | + # Get the number of commits since the latest tag |
| 26 | + commits_since_tag=$(git rev-list ${LATEST_TAG}..HEAD --count) |
| 27 | + echo "COMMITS_SINCE_TAG=${commits_since_tag}" >> $GITHUB_ENV |
| 28 | +
|
| 29 | + - name: Generate Version |
| 30 | + run: | |
| 31 | + # Extract version components |
| 32 | + version_base="${LATEST_TAG#v}" |
| 33 | + major=$(echo $version_base | cut -d. -f1) |
| 34 | + minor=$(echo $version_base | cut -d. -f2) |
| 35 | + patch=$(echo $version_base | cut -d. -f3) |
| 36 | + |
| 37 | + # Increment patch by the number of commits since the last tag |
| 38 | + new_patch=$((patch + $COMMITS_SINCE_TAG)) |
| 39 | + |
| 40 | + # Construct the new version string |
| 41 | + new_version="$major.$minor.$new_patch" |
| 42 | + |
| 43 | + # Calculate the version code (e.g., 1.0.5 -> 10005) |
| 44 | + version_code=$((major * 10000 + minor * 100 + new_patch)) |
| 45 | + |
| 46 | + # Set environment variables |
| 47 | + echo "NEW_VERSION_NAME=${new_version}" >> $GITHUB_ENV |
| 48 | + echo "NEW_VERSION_CODE=${version_code}" >> $GITHUB_ENV |
| 49 | +
|
| 50 | + - name: Update build.gradle with new version code and version name |
| 51 | + run: | |
| 52 | + # Update versionCode and versionName in build.gradle |
| 53 | + sed -i "s/versionCode [0-9]\+/versionCode ${NEW_VERSION_CODE}/" app/build.gradle |
| 54 | + sed -i "s/versionName \"[^\"]*\"/versionName \"${NEW_VERSION_NAME}\"/" app/build.gradle |
| 55 | +
|
| 56 | + - name: Print Version |
| 57 | + run: | |
| 58 | + echo "Version Name: $NEW_VERSION_NAME" |
| 59 | + echo "Version Code: $NEW_VERSION_CODE" |
15 | 60 |
|
16 | 61 | - name: Set up JDK 21
|
17 | 62 | uses: actions/setup-java@v4
|
|
0 commit comments