Skip to content

Commit c3003a7

Browse files
ShuttleLabclaude
andcommitted
Derive versionName/versionCode from git tag in release CI (no more manual build.gradle edits)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0220dad commit c3003a7

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

.github/workflows/release.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ jobs:
3030
- name: Setup Android SDK
3131
uses: android-actions/setup-android@v3
3232

33+
# 版本号从 tag 自动算出,无需改 build.gradle:
34+
# v1.2.3 -> versionName=1.2.3, versionCode=1*10000+2*100+3=10203
35+
- name: Derive version from tag
36+
run: |
37+
TAG="${GITHUB_REF_NAME}" # 例:v1.2.3
38+
VNAME="${TAG#v}" # 1.2.3
39+
if ! echo "$VNAME" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
40+
echo "::error::tag 必须是 vMAJOR.MINOR.PATCH(如 v1.2.3),当前为 '$TAG'"
41+
exit 1
42+
fi
43+
IFS=. read -r MA MI PA <<< "$VNAME"
44+
VCODE=$((MA*10000 + MI*100 + PA))
45+
echo "VERSION_NAME=$VNAME" >> "$GITHUB_ENV"
46+
echo "VERSION_CODE=$VCODE" >> "$GITHUB_ENV"
47+
echo "versionName=$VNAME versionCode=$VCODE"
48+
3349
# Release 必须正式签名:缺少签名密钥直接失败,绝不回退 debug 签名
3450
- name: Require & decode keystore
3551
env:

app/build.gradle

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ android {
77
namespace 'com.smsforwarder'
88
compileSdk 36
99

10+
// 版本号由 CI 从 git tag 注入(见 .github/workflows/release.yml);
11+
// 本地 / debug 构建没有这两个环境变量时,回退到占位版本,保证能编译
12+
def ciVersionName = System.getenv("VERSION_NAME")
13+
def ciVersionCode = System.getenv("VERSION_CODE")
14+
1015
defaultConfig {
1116
applicationId "com.smsforwarder"
1217
minSdk 26
1318
targetSdk 36
14-
versionCode 1
15-
versionName "1.0.0"
19+
versionCode ciVersionCode ? ciVersionCode.toInteger() : 1
20+
versionName ciVersionName ?: "0.0.0-dev"
1621
}
1722

1823
// 发布签名:CI 通过环境变量注入;本地/无密钥时回退到 debug 签名,保证 assembleRelease 不报错

0 commit comments

Comments
 (0)