Skip to content

Commit 4f38828

Browse files
committed
Sync release versions from tags
1 parent 902d0eb commit 4f38828

2 files changed

Lines changed: 109 additions & 6 deletions

File tree

.github/workflows/build-release.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: Build Release Packages
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
release_tag:
7+
description: Release tag to build, for example v1.1.13-beta
8+
required: false
9+
type: string
510
push:
611
tags:
712
- "v*"
@@ -40,6 +45,18 @@ jobs:
4045
steps:
4146
- uses: actions/checkout@v4
4247

48+
- name: Sync build version from tag
49+
id: sync_version
50+
shell: bash
51+
env:
52+
RELEASE_TAG: ${{ github.event.inputs.release_tag || github.ref_name }}
53+
run: |
54+
if command -v python3 >/dev/null 2>&1; then
55+
python3 ./scripts/ci-sync-version.py
56+
else
57+
python ./scripts/ci-sync-version.py
58+
fi
59+
4360
- uses: dtolnay/rust-toolchain@stable
4461

4562
- name: Use crates.io in CI
@@ -81,7 +98,7 @@ jobs:
8198
- name: Upload artifacts
8299
uses: actions/upload-artifact@v4
83100
with:
84-
name: linuxdo-accelerator-${{ matrix.artifact_name }}
101+
name: linuxdo-accelerator-${{ steps.sync_version.outputs.package_version }}-${{ matrix.artifact_name }}
85102
path: |
86103
dist/**
87104
target/release/linuxdo-accelerator*
@@ -94,18 +111,28 @@ jobs:
94111
- abi: arm64-v8a
95112
rust_target: aarch64-linux-android
96113
artifact_name: android-arm64-v8a
97-
apk_name: linuxdo-accelerator-android-arm64-v8a.apk
98114
- abi: x86_64
99115
rust_target: x86_64-linux-android
100116
artifact_name: android-x86_64
101-
apk_name: linuxdo-accelerator-android-x86_64.apk
102117

103118
runs-on: ubuntu-latest
104119
name: Build ${{ matrix.artifact_name }}
105120

106121
steps:
107122
- uses: actions/checkout@v4
108123

124+
- name: Sync build version from tag
125+
id: sync_version
126+
shell: bash
127+
env:
128+
RELEASE_TAG: ${{ github.event.inputs.release_tag || github.ref_name }}
129+
run: |
130+
if command -v python3 >/dev/null 2>&1; then
131+
python3 ./scripts/ci-sync-version.py
132+
else
133+
python ./scripts/ci-sync-version.py
134+
fi
135+
109136
- uses: actions/setup-java@v4
110137
with:
111138
distribution: temurin
@@ -150,20 +177,20 @@ jobs:
150177
ANDROID_ABI: ${{ matrix.abi }}
151178
RUST_TARGET: ${{ matrix.rust_target }}
152179
ANDROID_BUILD_TYPE: release
153-
APK_OUTPUT_NAME: ${{ matrix.apk_name }}
180+
APK_OUTPUT_NAME: linuxdo-accelerator-${{ steps.sync_version.outputs.package_version }}-android-${{ matrix.abi }}.apk
154181
run: |
155182
export ANDROID_NDK_HOME="${ANDROID_SDK_ROOT}/ndk/27.2.12479018"
156183
./scripts/build-android-apk.sh
157184
158185
- name: Upload Android artifacts
159186
uses: actions/upload-artifact@v4
160187
with:
161-
name: linuxdo-accelerator-${{ matrix.artifact_name }}
188+
name: linuxdo-accelerator-${{ steps.sync_version.outputs.package_version }}-${{ matrix.artifact_name }}
162189
path: |
163190
android/dist/*.apk
164191
165192
publish-release:
166-
if: startsWith(github.ref, 'refs/tags/v')
193+
if: startsWith(github.ref, 'refs/tags/v') || (github.event_name == 'workflow_dispatch' && startsWith(github.event.inputs.release_tag, 'v'))
167194
needs:
168195
- build
169196
- build-android
@@ -184,6 +211,7 @@ jobs:
184211
- name: Upload assets to GitHub Release
185212
uses: softprops/action-gh-release@v2
186213
with:
214+
tag_name: ${{ github.event.inputs.release_tag || github.ref_name }}
187215
files: |
188216
release-artifacts/**/dist/**
189217
release-artifacts/**/*.apk

scripts/ci-sync-version.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import pathlib
4+
import re
5+
import sys
6+
7+
8+
def replace_once(path: pathlib.Path, pattern: str, replacement: str) -> None:
9+
content = path.read_text(encoding="utf-8")
10+
updated, count = re.subn(pattern, replacement, content, count=1, flags=re.MULTILINE | re.DOTALL)
11+
if count != 1:
12+
raise SystemExit(f"failed to update {path}")
13+
path.write_text(updated, encoding="utf-8")
14+
15+
16+
def main() -> int:
17+
raw_tag = (
18+
os.environ.get("LINUXDO_RELEASE_TAG")
19+
or os.environ.get("RELEASE_TAG")
20+
or os.environ.get("GITHUB_REF_NAME")
21+
or ""
22+
).strip()
23+
if not raw_tag:
24+
raise SystemExit("missing release tag; set RELEASE_TAG or run this workflow from a tag ref")
25+
if not raw_tag.startswith("v"):
26+
raise SystemExit(f"release tag must start with 'v': {raw_tag}")
27+
28+
package_version = raw_tag[1:]
29+
match = re.fullmatch(r"(\d+)\.(\d+)\.(\d+)(?:[-+][0-9A-Za-z.-]+)?", package_version)
30+
if not match:
31+
raise SystemExit(f"unsupported release tag format: {raw_tag}")
32+
33+
major, minor, patch = (int(part) for part in match.groups())
34+
android_version_code = (major * 1_000_000) + (minor * 1_000) + patch
35+
36+
repo_root = pathlib.Path(
37+
os.environ.get("REPO_ROOT", pathlib.Path(__file__).resolve().parents[1])
38+
)
39+
40+
replace_once(
41+
repo_root / "Cargo.toml",
42+
r'(^\[package\]\n.*?^version = ")[^"]+(")',
43+
rf'\g<1>{package_version}\2',
44+
)
45+
replace_once(
46+
repo_root / "Cargo.lock",
47+
r'(^\[\[package\]\]\nname = "linuxdo-accelerator"\nversion = ")[^"]+(")',
48+
rf'\g<1>{package_version}\2',
49+
)
50+
replace_once(
51+
repo_root / "android/app/build.gradle.kts",
52+
r'^(\s*versionCode\s*=\s*)\d+$',
53+
rf"\g<1>{android_version_code}",
54+
)
55+
replace_once(
56+
repo_root / "android/app/build.gradle.kts",
57+
r'^(\s*versionName\s*=\s*")[^"]+(")$',
58+
rf'\g<1>{package_version}\2',
59+
)
60+
61+
github_output = os.environ.get("GITHUB_OUTPUT")
62+
if github_output:
63+
with open(github_output, "a", encoding="utf-8") as handle:
64+
handle.write(f"release_tag={raw_tag}\n")
65+
handle.write(f"package_version={package_version}\n")
66+
handle.write(f"android_version_code={android_version_code}\n")
67+
68+
print(f"release tag: {raw_tag}")
69+
print(f"package version: {package_version}")
70+
print(f"android versionCode: {android_version_code}")
71+
return 0
72+
73+
74+
if __name__ == "__main__":
75+
sys.exit(main())

0 commit comments

Comments
 (0)