Skip to content

Commit 7112548

Browse files
committed
update version branch on release via gh actions
1 parent 204d94a commit 7112548

File tree

5 files changed

+140
-7
lines changed

5 files changed

+140
-7
lines changed

.github/workflows/android.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ on:
55
paths-ignore:
66
- '**.md'
77
- LICENSE
8+
- 'fastlane/**'
89
pull_request:
910
paths-ignore:
1011
- '**.md'
1112
- LICENSE
13+
- 'fastlane/**'
1214

1315
jobs:
1416
build:

.github/workflows/release.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: Android Release
2+
3+
on:
4+
push:
5+
tags:
6+
- v[0-9]+.[0-9]+.[0-9]+**
7+
workflow_dispatch:
8+
9+
env:
10+
VERSION_BRANCH: version
11+
VERSION_FILE: version.properties
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
- name: set up JDK 17
22+
uses: actions/setup-java@v3
23+
with:
24+
java-version: '17'
25+
distribution: 'temurin'
26+
cache: 'gradle'
27+
- name: Grant execute permission for gradlew
28+
run: chmod +x gradlew
29+
30+
- name: Generate Version Info
31+
run: ./gradlew appVersion
32+
33+
- name: Upload version.properties
34+
uses: actions/upload-artifact@v3
35+
with:
36+
name: ${{ env.VERSION_FILE }}
37+
path: ${{ env.VERSION_FILE }}
38+
if-no-files-found: error
39+
retention-days: 1
40+
41+
42+
# commit the version file to 'version' branch so that F-Droid can utilize it for new version detecting
43+
update-version:
44+
runs-on: ubuntu-latest
45+
needs: build
46+
permissions:
47+
contents: write
48+
49+
steps:
50+
- name: Checkout version branch
51+
uses: actions/checkout@v4
52+
with:
53+
ref: ${{ env.VERSION_BRANCH }}
54+
fetch-depth: 0
55+
56+
- name: Delete version.properties
57+
run: rm -f $VERSION_FILE
58+
59+
- name: Download version.properties
60+
uses: actions/download-artifact@v3
61+
with:
62+
name: ${{ env.VERSION_FILE }}
63+
64+
- name: Show version info
65+
run: cat $VERSION_FILE
66+
67+
- name: Commit and push
68+
uses: stefanzweifel/git-auto-commit-action@v5
69+
with:
70+
commit_message: Update version.properties
71+
push_options: '--force'
72+
skip_fetch: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.DS_Store
44
release/
55
/build
6+
/version.properties
67

78
# IntelliJ
89
*.iml

app/build.gradle.kts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import org.jetbrains.kotlin.config.JvmTarget
22
import java.io.ByteArrayOutputStream
3+
import java.util.Properties
34

45
plugins {
56
id("com.android.application")
67
id("org.jetbrains.kotlin.android")
78
}
89

9-
// The last code is 20001, make sure the result is greater than it.
10-
val vCode = 20000 + getVersionCode()
11-
val vName = getVersionName()
12-
logger.lifecycle("App Version: $vName ($vCode)")
10+
val versionProperties = Properties().apply {
11+
val f = rootProject.file("version.properties")
12+
if (f.isFile) {
13+
load(f.reader())
14+
}
15+
}
1316

1417
android {
1518
namespace = "cc.chenhe.qqnotifyevo"
@@ -18,8 +21,8 @@ android {
1821
applicationId = "cc.chenhe.qqnotifyevo"
1922
minSdk = 26
2023
targetSdk = 33
21-
versionCode = vCode
22-
versionName = vName
24+
versionCode = versionProperties.getProperty("code", "1").toIntOrNull() ?: 1
25+
versionName = versionProperties.getProperty("name", "UNKNOWN")
2326
}
2427
buildFeatures {
2528
viewBinding = true

build.gradle.kts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.io.FileWriter
2+
13
plugins {
24
id("com.android.application") version "8.1.3" apply false
35

@@ -14,12 +16,65 @@ allprojects {
1416
//noinspection JcenterRepositoryObsolete
1517
jcenter {
1618
content {
17-
includeModule("com.oasisfeng.nevo","sdk")
19+
includeModule("com.oasisfeng.nevo", "sdk")
1820
}
1921
}
2022
}
2123
}
2224

25+
tasks.register("appVersion") {
26+
description = "Calculate app version and save to version.properties"
27+
28+
// execute on configuration phase
29+
val name = getVersionName()
30+
val code = 20000 + getVersionCode()
31+
logger.lifecycle("AppVersionName: {}\nAppVersionCode: {}", name, code)
32+
FileWriter(File(rootProject.projectDir, "version.properties")).use { fw ->
33+
fw.write("name=$name\ncode=$code\n")
34+
fw.flush()
35+
}
36+
}
37+
2338
tasks.register("clean", Delete::class) {
2439
delete(rootProject.buildDir)
2540
}
41+
42+
fun String.runCommand(currentWorkingDir: File = file("./")): String {
43+
val byteOut = java.io.ByteArrayOutputStream()
44+
project.exec {
45+
workingDir = currentWorkingDir
46+
commandLine = this@runCommand.split(" ")
47+
standardOutput = byteOut
48+
}
49+
return String(byteOut.toByteArray()).trim()
50+
}
51+
52+
fun getVersionCode(): Int {
53+
val cmd = "git rev-list HEAD --count"
54+
return try {
55+
cmd.runCommand().toInt()
56+
} catch (e: Exception) {
57+
logger.error("Failed to get version code with git, return 1 by default.", e)
58+
1
59+
}
60+
}
61+
62+
fun getVersionName(): String {
63+
val cmd = "git describe --tags --long --abbrev=7 --dirty=_dev"
64+
try {
65+
val v = cmd.runCommand()
66+
val pattern = """^v(?<v>[\d|.]+)-\d+-g[A-Za-z0-9]{7}(?<s>_dev)?$""".toRegex()
67+
val g = pattern.matchEntire(v)?.groups
68+
if (g == null || g["v"] == null) {
69+
logger.error(
70+
"Failed to get version name with git.\n" +
71+
"Cannot match git tag describe, return <UNKNOWN> by default. raw=$v"
72+
)
73+
return "UNKNOWN"
74+
}
75+
return g["v"]!!.value + (g["s"]?.value ?: "")
76+
} catch (e: Exception) {
77+
logger.error("Failed to get version name with git, return <UNKNOWN> by default.", e)
78+
return "UNKNOWN"
79+
}
80+
}

0 commit comments

Comments
 (0)