Skip to content

Commit 1198305

Browse files
committed
feat(build): 支持从环境变量或配置文件读取版本信息
- 新增 version.properties 文件管理默认版本号与版本名称 - 修改 build.gradle.kts 优先读取环境变量中的版本信息 - 更新 GitHub Actions 工作流支持手动指定版本号与版本名称 - 在构建过程中打印当前使用的版本信息以便调试 - 优化版本读取逻辑,增强灵活性与可维护性
1 parent f1cf739 commit 1198305

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

.github/workflows/release.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ on:
77
tags:
88
- 'v*' # 当一个以 'v' 开头的标签被推送到仓库时触发 (例如 v1.0, v2.1.0)
99
workflow_dispatch: # 支持手动触发工作流
10+
inputs:
11+
version_code:
12+
description: '版本号 (versionCode),留空则使用 version.properties 中的值'
13+
required: false
14+
type: string
15+
default: ''
16+
version_name:
17+
description: '版本名称 (versionName),留空则使用 version.properties 中的值'
18+
required: false
19+
type: string
20+
default: ''
1021

1122
# 定义一个或多个作业
1223
jobs:
@@ -58,10 +69,12 @@ jobs:
5869
# 第6步:构建 Release 版本的 APK
5970
# 运行 Gradle 命令来编译和打包带签名的 APK。
6071
- name: 构建 Release APK
61-
# 临时调试,使用 Debug 模式
6272
run: ./gradlew :app:assembleRelease --stacktrace
6373
env:
6474
GRADLE_OPTS: -Xmx8g -Dfile.encoding=UTF-8
75+
# 手动触发时传递版本参数,留空则使用 version.properties 中的值
76+
APP_VERSION_CODE: ${{ github.event.inputs.version_code }}
77+
APP_VERSION_NAME: ${{ github.event.inputs.version_name }}
6578

6679
# 调试步骤(暂时保留,用于确认构建是否生成目录)
6780
- name: 调试:列出构建输出
@@ -72,13 +85,28 @@ jobs:
7285
- name: 生成 appConfig.json
7386
run: ./gradlew :app:printReleaseAppConfig
7487

75-
# 第7步:从 build.gradle.kts 获取版本名
76-
# 通过脚本从 build.gradle.kts 文件中提取 versionName
88+
# 第7步:获取版本信息
89+
# 从 version.properties 文件中读取版本信息,如果有手动输入则优先使用手动输入的值
7790
- name: 获取版本名称
7891
id: get_version
7992
run: |
80-
VERSION_NAME=$(grep 'versionName =' build.gradle.kts | sed -n 's/.*versionName = \"\(.*\)\".*/\1/p')
93+
# 读取 version.properties 中的值作为默认值
94+
VERSION_NAME=$(grep 'versionName=' version.properties | cut -d'=' -f2)
95+
VERSION_CODE=$(grep 'versionCode=' version.properties | cut -d'=' -f2)
96+
97+
# 如果提供了手动输入的版本名称,则使用手动输入的值
98+
if [ -n "${{ github.event.inputs.version_name }}" ]; then
99+
VERSION_NAME="${{ github.event.inputs.version_name }}"
100+
fi
101+
102+
# 如果提供了手动输入的版本号,则使用手动输入的值
103+
if [ -n "${{ github.event.inputs.version_code }}" ]; then
104+
VERSION_CODE="${{ github.event.inputs.version_code }}"
105+
fi
106+
81107
echo "version=$VERSION_NAME" >> $GITHUB_OUTPUT
108+
echo "version_code=$VERSION_CODE" >> $GITHUB_OUTPUT
109+
echo "::notice::Building with versionName=$VERSION_NAME, versionCode=$VERSION_CODE"
82110
83111
# 第8步:获取 APK 路径
84112
# 从输出目录中找到构建生成的 APK 文件。

build.gradle.kts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,23 @@
33
import com.android.build.gradle.BaseExtension
44
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
55
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
6+
import java.util.Properties
67

78
// 导入配置文件
89
apply(from = "configs.gradle.kts")
910

11+
// 读取版本配置
12+
val versionPropsFile = file("version.properties")
13+
val versionProps = Properties().apply {
14+
if (versionPropsFile.exists()) {
15+
versionPropsFile.inputStream().use { load(it) }
16+
}
17+
}
18+
19+
// 优先使用环境变量,否则使用 version.properties 中的值
20+
val appVersionCode: Int = (System.getenv("APP_VERSION_CODE") ?: versionProps.getProperty("versionCode", "22")).toInt()
21+
val appVersionName: String = System.getenv("APP_VERSION_NAME") ?: versionProps.getProperty("versionName", "5.4.1")
22+
1023
buildscript {
1124
repositories {
1225
google()
@@ -102,8 +115,8 @@ subprojects {
102115
defaultConfig {
103116
minSdkVersion(26)
104117
targetSdkVersion(33)
105-
versionName = "5.4.1"
106-
versionCode = 22
118+
versionName = appVersionName
119+
versionCode = appVersionCode
107120
}
108121

109122
// 支持 Java JDK 21

version.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
versionCode=23
2+
versionName=5.4.2

0 commit comments

Comments
 (0)