Skip to content

Commit e04f5f4

Browse files
committed
Edit Workflows for custom Version details
1 parent 289b720 commit e04f5f4

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

.github/workflows/build.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,51 @@ jobs:
1212
steps:
1313
- name: Checkout repository
1414
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"
1560
1661
- name: Set up JDK 21
1762
uses: actions/setup-java@v4

.github/workflows/publish.yml

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@ permissions:
33
contents: write
44
on:
55
workflow_dispatch:
6-
inputs:
6+
increment_major:
7+
description: 'Increment Major Version by?'
8+
required: true
9+
default: '0'
10+
type: string
11+
increment_minor:
12+
description: 'Increment Minor Version by?'
13+
required: true
14+
default: '0'
15+
type: string
16+
increment_patch:
17+
description: 'Increment Patch Version by?'
18+
required: true
19+
default: '0'
20+
type: string
721
changes_in_release:
822
description: 'Changes in release'
923
required: true
@@ -17,6 +31,47 @@ jobs:
1731
steps:
1832
- name: Checkout repository
1933
uses: actions/checkout@v4
34+
with:
35+
fetch-depth: 0 # Important: get all tags!
36+
37+
- name: Get Latest Tag
38+
run: |
39+
latest_tag=$(git describe --tags --abbrev=0)
40+
echo "LATEST_TAG=${latest_tag}" >> $GITHUB_ENV
41+
42+
- name: Generate Version
43+
run: |
44+
# Extract version components
45+
version_base="${LATEST_TAG#v}"
46+
major=$(echo $version_base | cut -d. -f1)
47+
minor=$(echo $version_base | cut -d. -f2)
48+
patch=$(echo $version_base | cut -d. -f3)
49+
50+
# Increment versions by the specified input values
51+
new_major=$((major + ${{ github.event.inputs.increment_major }}))
52+
new_minor=$((minor + ${{ github.event.inputs.increment_minor }}))
53+
new_patch=$((patch + ${{ github.event.inputs.increment_patch }}))
54+
55+
# Construct the new version string
56+
new_version="$major.$minor.$new_patch"
57+
58+
# Calculate the version code (e.g., 1.0.5 -> 10005)
59+
version_code=$((major * 10000 + minor * 100 + new_patch))
60+
61+
# Set environment variables
62+
echo "NEW_VERSION_NAME=${new_version}" >> $GITHUB_ENV
63+
echo "NEW_VERSION_CODE=${version_code}" >> $GITHUB_ENV
64+
65+
- name: Update build.gradle with new version code and version name
66+
run: |
67+
# Update versionCode and versionName in build.gradle
68+
sed -i "s/versionCode [0-9]\+/versionCode ${NEW_VERSION_CODE}/" app/build.gradle
69+
sed -i "s/versionName \"[^\"]*\"/versionName \"${NEW_VERSION_NAME}\"/" app/build.gradle
70+
71+
- name: Print Version
72+
run: |
73+
echo "Version Name: $NEW_VERSION_NAME"
74+
echo "Version Code: $NEW_VERSION_CODE"
2075
2176
- name: Set up JDK 21
2277
uses: actions/setup-java@v4

0 commit comments

Comments
 (0)