Skip to content

Commit d2e6cda

Browse files
committed
feat(ci): implement dynamic versioning and github actions build workflow
1 parent 165603e commit d2e6cda

3 files changed

Lines changed: 167 additions & 5 deletions

File tree

.github/workflows/build.yml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Build and Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- master
8+
tags:
9+
- 'v*'
10+
pull_request:
11+
branches:
12+
- main
13+
- master
14+
15+
permissions:
16+
contents: write
17+
18+
jobs:
19+
build:
20+
name: Build Release APK
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Set up JDK 21
30+
uses: actions/setup-java@v4
31+
with:
32+
java-version: '21'
33+
distribution: 'temurin'
34+
cache: gradle
35+
36+
- name: Decode Keystore
37+
env:
38+
SIGNING_KEY_STORE_BASE64: ${{ secrets.SIGNING_KEY_STORE_BASE64 }}
39+
run: |
40+
if [ ! -z "$SIGNING_KEY_STORE_BASE64" ]; then
41+
echo "$SIGNING_KEY_STORE_BASE64" | base64 --decode > app/release.jks
42+
fi
43+
44+
- name: Grant execute permission for gradlew
45+
run: chmod +x gradlew
46+
47+
- name: Build Release and Debug APKs
48+
env:
49+
SIGNING_STORE_PASSWORD: ${{ secrets.SIGNING_STORE_PASSWORD }}
50+
SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
51+
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
52+
run: ./gradlew assembleRelease assembleDebug
53+
54+
- name: Calculate Version
55+
id: version
56+
run: |
57+
COMMIT_COUNT=$(git rev-list --count HEAD)
58+
COMMIT_HASH=$(git rev-parse --short HEAD)
59+
VERSION_NAME="1.$COMMIT_COUNT.$COMMIT_HASH"
60+
echo "VERSION_NAME=$VERSION_NAME" >> $GITHUB_ENV
61+
echo "VERSION_NAME=$VERSION_NAME" >> $GITHUB_OUTPUT
62+
echo "Computed Version: $VERSION_NAME"
63+
64+
- name: Upload APK Artifacts
65+
uses: actions/upload-artifact@v4
66+
with:
67+
name: HotBell-Radio-v${{ steps.version.outputs.VERSION_NAME }}
68+
path: |
69+
app/build/outputs/apk/release/*.apk
70+
app/build/outputs/apk/debug/*.apk
71+
72+
- name: Create Release
73+
if: startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main'
74+
uses: softprops/action-gh-release@v1
75+
with:
76+
files: |
77+
app/build/outputs/apk/release/*.apk
78+
app/build/outputs/apk/debug/*.apk
79+
tag_name: v${{ steps.version.outputs.VERSION_NAME }}
80+
name: Release v${{ steps.version.outputs.VERSION_NAME }}
81+
prerelease: false
82+
env:
83+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

app/build.gradle.kts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,70 @@ android {
1212
applicationId = "com.hotbell.radio"
1313
minSdk = 26
1414
targetSdk = 34
15-
versionCode = 1
16-
versionName = "1.0"
15+
16+
// Git-based Versioning
17+
fun getGitCommitCount(): Int {
18+
return try {
19+
val process = ProcessBuilder("git", "rev-list", "--count", "HEAD").start()
20+
process.inputStream.bufferedReader().readText().trim().toInt()
21+
} catch (e: Exception) {
22+
1 // Fallback
23+
}
24+
}
25+
26+
fun getGitCommitHash(): String {
27+
return try {
28+
val process = ProcessBuilder("git", "rev-parse", "--short", "HEAD").start()
29+
process.inputStream.bufferedReader().readText().trim()
30+
} catch (e: Exception) {
31+
"unknown"
32+
}
33+
}
34+
35+
val commitCount = getGitCommitCount()
36+
val commitHash = getGitCommitHash()
37+
38+
versionCode = commitCount
39+
versionName = "1.$commitCount.$commitHash"
1740

1841
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1942
vectorDrawables {
2043
useSupportLibrary = true
2144
}
2245
}
2346

47+
signingConfigs {
48+
create("release") {
49+
storeFile = file("release.jks")
50+
storePassword = System.getenv("SIGNING_STORE_PASSWORD") ?: "123456"
51+
keyAlias = System.getenv("SIGNING_KEY_ALIAS") ?: "key"
52+
keyPassword = System.getenv("SIGNING_KEY_PASSWORD") ?: "123456"
53+
}
54+
}
55+
2456
buildTypes {
2557
release {
2658
isMinifyEnabled = true
59+
isShrinkResources = true
2760
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
61+
signingConfig = signingConfigs.getByName("release")
2862
}
2963
}
3064
compileOptions {
31-
sourceCompatibility = JavaVersion.VERSION_17
32-
targetCompatibility = JavaVersion.VERSION_17
65+
sourceCompatibility = JavaVersion.VERSION_21
66+
targetCompatibility = JavaVersion.VERSION_21
3367
}
3468
kotlinOptions {
35-
jvmTarget = "17"
69+
jvmTarget = "21"
70+
}
71+
72+
applicationVariants.all {
73+
val variant = this
74+
variant.outputs
75+
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
76+
.forEach { output ->
77+
output.outputFileName = "HotBell-Radio-${variant.versionName}-${variant.name}.apk"
78+
}
3679
}
3780
buildFeatures {
3881
compose = true

generate_secrets.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
# Configuration
4+
KEYSTORE_FILE="app/release.jks"
5+
ALIAS="hotbell"
6+
PASSWORD="hotbell_password" # Change this or use it as a default for now
7+
8+
# Check if keystore already exists
9+
if [ -f "$KEYSTORE_FILE" ]; then
10+
echo "Keystore $KEYSTORE_FILE already exists. Skipping generation."
11+
else
12+
echo "Generating new keystore..."
13+
keytool -genkey -v -keystore "$KEYSTORE_FILE" -alias "$ALIAS" -keyalg RSA -keysize 2048 -validity 10000 \
14+
-storepass "$PASSWORD" -keypass "$PASSWORD" \
15+
-dname "CN=HotBell, OU=Dev, O=HotBell, L=Internet, S=Internet, C=HB"
16+
fi
17+
18+
# Generate Base64
19+
if [ -f "$KEYSTORE_FILE" ]; then
20+
BASE64_KEYSTORE=$(base64 -w 0 "$KEYSTORE_FILE")
21+
echo "------------------------------------------------------------"
22+
echo "GITHUB SECRETS CONFIGURATION"
23+
echo "------------------------------------------------------------"
24+
echo "SIGNING_KEY_STORE_BASE64: (copied to clipboard or shown below)"
25+
echo "$BASE64_KEYSTORE"
26+
echo ""
27+
echo "SIGNING_STORE_PASSWORD: $PASSWORD"
28+
echo "SIGNING_KEY_ALIAS: $ALIAS"
29+
echo "SIGNING_KEY_PASSWORD: $PASSWORD"
30+
echo "------------------------------------------------------------"
31+
echo "Please add these to your GitHub Repository Secrets."
32+
33+
# Save to a temporary file for the user to copy easily
34+
echo "$BASE64_KEYSTORE" > /tmp/hotbell_keystore_base64.txt
35+
echo "Base64 also saved to /tmp/hotbell_keystore_base64.txt"
36+
fi

0 commit comments

Comments
 (0)