Skip to content

Commit 7139139

Browse files
VincentZyuwin11台式机VincentZyuwin11台式机
authored andcommitted
feat: auto-versioning for artifacts and test build release
1 parent 7018604 commit 7139139

4 files changed

Lines changed: 1505 additions & 27 deletions

File tree

.github/workflows/build.yml

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,49 @@ jobs:
1717
outputs:
1818
should_build: ${{ steps.flags.outputs.should_build }}
1919
should_release: ${{ steps.flags.outputs.should_release }}
20+
should_publish: ${{ steps.flags.outputs.should_publish }}
21+
version: ${{ steps.flags.outputs.version }}
2022
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
2126
- name: Parse commit message
2227
id: flags
2328
run: |
2429
MSG="${{ github.event.head_commit.message }}"
30+
31+
# 从 Cargo.toml 提取版本号
32+
VERSION="v$(grep '^version' rust/Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')"
33+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
34+
echo "📦 Version: $VERSION"
35+
2536
# PR 始终构建
2637
if [ "${{ github.event_name }}" = "pull_request" ]; then
27-
echo "should_build=true" >> "$GITHUB_OUTPUT"
38+
echo "should_build=true" >> "$GITHUB_OUTPUT"
2839
echo "should_release=false" >> "$GITHUB_OUTPUT"
40+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
2941
exit 0
3042
fi
31-
# "build release" 同时构建 + 发布
32-
if echo "$MSG" | grep -qi "build release"; then
33-
echo "should_build=true" >> "$GITHUB_OUTPUT"
34-
echo "should_release=true" >> "$GITHUB_OUTPUT"
35-
# "build action" 只构建
43+
44+
# "build publish" = 完整发布流程
45+
if echo "$MSG" | grep -qi "build publish"; then
46+
echo "should_build=true" >> "$GITHUB_OUTPUT"
47+
echo "should_release=true" >> "$GITHUB_OUTPUT"
48+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
49+
# "build release" = 构建 + GitHub Release
50+
elif echo "$MSG" | grep -qi "build release"; then
51+
echo "should_build=true" >> "$GITHUB_OUTPUT"
52+
echo "should_release=true" >> "$GITHUB_OUTPUT"
53+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
54+
# "build action" = 仅构建
3655
elif echo "$MSG" | grep -qi "build action"; then
37-
echo "should_build=true" >> "$GITHUB_OUTPUT"
56+
echo "should_build=true" >> "$GITHUB_OUTPUT"
3857
echo "should_release=false" >> "$GITHUB_OUTPUT"
58+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
3959
else
40-
echo "should_build=false" >> "$GITHUB_OUTPUT"
60+
echo "should_build=false" >> "$GITHUB_OUTPUT"
4161
echo "should_release=false" >> "$GITHUB_OUTPUT"
62+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
4263
fi
4364
4465
# ── 多平台构建 ───────────────────────────────────────────
@@ -100,13 +121,32 @@ jobs:
100121

101122
- name: Prepare artifact
102123
shell: bash
103-
run: cp "rust/target/${{ matrix.target }}/release/${{ matrix.binary }}" "${{ matrix.asset }}"
124+
run: |
125+
VERSION="${{ needs.check.outputs.version }}"
126+
ASSET_BASE="${{ matrix.asset }}"
127+
128+
# 在扩展名前插入版本号
129+
# winload-linux-x86_64 -> winload-linux-x86_64-v0.1.0
130+
# winload-windows-x86_64.exe -> winload-windows-x86_64-v0.1.0.exe
131+
if [[ "$ASSET_BASE" == *.* ]]; then
132+
# 有扩展名
133+
BASE_NAME="${ASSET_BASE%.*}"
134+
EXTENSION="${ASSET_BASE##*.}"
135+
ASSET_WITH_VERSION="${BASE_NAME}-${VERSION}.${EXTENSION}"
136+
else
137+
# 无扩展名
138+
ASSET_WITH_VERSION="${ASSET_BASE}-${VERSION}"
139+
fi
140+
141+
echo "📦 Output: $ASSET_WITH_VERSION"
142+
cp "rust/target/${{ matrix.target }}/release/${{ matrix.binary }}" "$ASSET_WITH_VERSION"
143+
echo "asset_name=$ASSET_WITH_VERSION" >> "$GITHUB_ENV"
104144
105145
- name: Upload build artifact
106146
uses: actions/upload-artifact@v4
107147
with:
108-
name: ${{ matrix.asset }}
109-
path: ${{ matrix.asset }}
148+
name: ${{ env.asset_name }}
149+
path: ${{ env.asset_name }}
110150

111151
# ── 发布到 GitHub Releases ─────────────────────────────
112152
release:
@@ -115,29 +155,26 @@ jobs:
115155
if: needs.check.outputs.should_release == 'true'
116156
runs-on: ubuntu-latest
117157
steps:
118-
- name: Checkout (for version info)
158+
- name: Checkout
119159
uses: actions/checkout@v4
120160
with:
121161
fetch-depth: 0
122162

123-
- name: Determine version tag
124-
id: version
125-
run: |
126-
VER=$(grep '^version' rust/Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
127-
TAG="v${VER}"
128-
echo "tag=${TAG}" >> "$GITHUB_OUTPUT"
129-
echo "Version: ${TAG}"
130-
131163
- name: Download all artifacts
132164
uses: actions/download-artifact@v4
133165
with:
134166
merge-multiple: true
135167

168+
- name: List downloaded files
169+
run: |
170+
echo "Downloaded artifacts:"
171+
ls -lh
172+
136173
- name: Create GitHub Release
137174
uses: softprops/action-gh-release@v2
138175
with:
139-
tag_name: ${{ steps.version.outputs.tag }}
140-
name: "winload ${{ steps.version.outputs.tag }}"
176+
tag_name: ${{ needs.check.outputs.version }}
177+
name: "winload ${{ needs.check.outputs.version }}"
141178
generate_release_notes: true
142179
make_latest: true
143-
files: winload-*
180+
files: winload-*-${{ needs.check.outputs.version }}*

rust/build.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import argparse
88
import os
9+
import re
910
import shutil
1011
import subprocess
1112
import sys
@@ -22,6 +23,27 @@
2223
]
2324

2425

26+
def extract_version_from_cargo_toml():
27+
"""从 Cargo.toml 提取版本号"""
28+
cargo_toml = RUST_DIR / "Cargo.toml"
29+
if not cargo_toml.exists():
30+
print("❌ Cargo.toml not found")
31+
return None
32+
33+
with open(cargo_toml, 'r', encoding='utf-8') as f:
34+
content = f.read()
35+
36+
# 匹配 version = "x.y.z" 格式
37+
match = re.search(r'^version\s*=\s*"([^"]+)"', content, re.MULTILINE)
38+
if match:
39+
version = match.group(1)
40+
print(f"📦 Extracted version from Cargo.toml: v{version}")
41+
return f"v{version}"
42+
43+
print("⚠️ Could not extract version from Cargo.toml")
44+
return None
45+
46+
2547
def run_command(cmd, cwd=None, check=True):
2648
"""运行命令并打印输出"""
2749
print(f"\n{' '.join(cmd)}")
@@ -51,7 +73,7 @@ def ensure_target_installed(target):
5173
print(f" ✓ {target} already installed")
5274

5375

54-
def build_target(target, binary_name, output_name):
76+
def build_target(target, binary_name, output_name, version=None):
5577
"""编译指定 target"""
5678
print(f"\n🔨 Building {target}...")
5779

@@ -74,9 +96,23 @@ def build_target(target, binary_name, output_name):
7496
print(f"❌ Build failed for {target}")
7597
return False
7698

99+
# 生成带版本号的输出文件名
100+
if version:
101+
# 在扩展名前插入版本号
102+
# winload-linux-x86_64 -> winload-linux-x86_64-v0.1.0
103+
# winload-windows-x86_64.exe -> winload-windows-x86_64-v0.1.0.exe
104+
base_name = output_name
105+
ext = ""
106+
if "." in output_name:
107+
base_name, ext = output_name.rsplit(".", 1)
108+
ext = "." + ext
109+
output_name_versioned = f"{base_name}-{version}{ext}"
110+
else:
111+
output_name_versioned = output_name
112+
77113
# 复制产物到 dist 目录
78114
source = RUST_DIR / "target" / target / "release" / binary_name
79-
dest = OUTPUT_DIR / output_name
115+
dest = OUTPUT_DIR / output_name_versioned
80116

81117
if not source.exists():
82118
print(f"❌ Binary not found: {source}")
@@ -87,7 +123,7 @@ def build_target(target, binary_name, output_name):
87123

88124
# 显示文件信息
89125
size_mb = dest.stat().st_size / 1024 / 1024
90-
print(f"✓ {output_name} ({size_mb:.2f} MB)")
126+
print(f"✓ {output_name_versioned} ({size_mb:.2f} MB)")
91127

92128
return True
93129

@@ -109,6 +145,11 @@ def main():
109145
print("🚀 Building winload for multiple platforms")
110146
print("=" * 60)
111147

148+
# 提取版本号
149+
version = extract_version_from_cargo_toml()
150+
if not version:
151+
print("⚠️ Building without version number in filename")
152+
112153
# 检查是否在 WSL 中
113154
if not Path("/proc/version").exists():
114155
print("❌ This script must be run in WSL")
@@ -138,7 +179,7 @@ def main():
138179
# 编译所有 target
139180
success_count = 0
140181
for target, binary, output in TARGETS:
141-
if build_target(target, binary, output):
182+
if build_target(target, binary, output, version):
142183
success_count += 1
143184

144185
# 总结

0 commit comments

Comments
 (0)