Skip to content

Commit 5ee9d95

Browse files
committed
更新工作流配置
1 parent d955ac1 commit 5ee9d95

File tree

3 files changed

+203
-1
lines changed

3 files changed

+203
-1
lines changed

.github/workflows/release.yml

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
name: Create and Publish Release
2+
3+
# 触发条件:仅在推送 'v*' 开头的 tag 时触发
4+
on:
5+
push:
6+
tags:
7+
- 'v*'
8+
9+
# 设置环境变量
10+
env:
11+
# 项目名称
12+
PROJECT_NAME: baitts-cli-rs
13+
14+
jobs:
15+
# 为目标平台构建二进制文件
16+
build_binaries:
17+
name: Build Binaries
18+
# 使用策略矩阵为多个平台并行构建
19+
strategy:
20+
matrix:
21+
include:
22+
# x86 平台的64位 Linux 系统
23+
- target: x86_64-unknown-linux-gnu
24+
os: ubuntu-latest
25+
shorthand: linux-amd64
26+
# x86 平台的64位 Windows 系统
27+
- target: x86_64-pc-windows-msvc
28+
os: windows-latest
29+
shorthand: windows-amd64
30+
# Arm 平台的64位 Linux 系统
31+
- target: aarch64-unknown-linux-gnu
32+
os: ubuntu-latest
33+
shorthand: linux-arm64
34+
use_cross: true # 标记此平台需要使用 cross-rs
35+
# Arm 平台的64位 MacOS 系统
36+
- target: aarch64-apple-darwin
37+
os: macos-latest
38+
shorthand: macos-arm64
39+
40+
# 多平台共同执行步骤
41+
runs-on: ${{ matrix.os }}
42+
steps:
43+
# 步骤1: 检出代码
44+
- name: Checkout repository
45+
uses: actions/checkout@v4
46+
47+
# 步骤2: 安装 Rust 工具链,并添加目标平台支持
48+
- name: Install Rust toolchain
49+
uses: dtolnay/rust-toolchain@stable
50+
with:
51+
toolchain: stable
52+
targets: ${{ matrix.target }}
53+
54+
# 步骤3: (可选) 如果需要,安装 cross-rs 用于交叉编译
55+
- name: Install cross-rs
56+
if: matrix.use_cross
57+
uses: taiki-e/install-action@cross
58+
59+
# 步骤4: (优化) 设置 Cargo 缓存,加速构建
60+
- name: Cache Cargo dependencies
61+
uses: Swatinem/rust-cache@v2
62+
63+
# 步骤5: 构建二进制文件
64+
- name: Build binary
65+
run: |
66+
if [ "${{ matrix.use_cross }}" = "true" ]; then
67+
cross build --release --target ${{ matrix.target }}
68+
else
69+
cargo build --release --target ${{ matrix.target }}
70+
fi
71+
shell: bash
72+
73+
# 步骤6: 准备用于打包的文件
74+
- name: Prepare package
75+
shell: bash
76+
run: |
77+
# 定义二进制文件路径和名称
78+
BINARY_PATH="target/${{ matrix.target }}/release/${{ env.PROJECT_NAME }}"
79+
if [ "${{ matrix.os }}" = "windows-latest" ]; then
80+
BINARY_PATH+=".exe"
81+
fi
82+
83+
# 创建一个临时目录用于打包
84+
mkdir staging
85+
86+
# 将二进制文件复制到临时目录
87+
cp "$BINARY_PATH" staging/
88+
89+
# 步骤7: 打包文件
90+
- name: Package file
91+
shell: bash
92+
run: |
93+
# 从 git ref 中获取 tag 名称 (例如 v0.1.0)
94+
TAG_NAME=${{ github.ref_name }}
95+
# 组合最终的压缩包名称
96+
ASSET_NAME="${{ env.PROJECT_NAME }}-${{ matrix.shorthand }}-${TAG_NAME}"
97+
98+
# 根据操作系统选择不同的打包方式
99+
if [ "${{ matrix.os }}" = "windows-latest" ]; then
100+
# Windows 使用 zip
101+
7z a "${ASSET_NAME}.zip" ./staging/*
102+
echo "ASSET_PATH=${ASSET_NAME}.zip" >> $GITHUB_ENV
103+
else
104+
# 其他系统使用 tar.gz
105+
tar -czf "${ASSET_NAME}.tar.gz" -C staging .
106+
echo "ASSET_PATH=${ASSET_NAME}.tar.gz" >> $GITHUB_ENV
107+
fi
108+
109+
# 步骤8: 上传打包好的文件作为构建产物
110+
- name: Upload artifact
111+
uses: actions/upload-artifact@v4
112+
with:
113+
name: ${{ matrix.shorthand }}-artifact
114+
path: ${{ env.ASSET_PATH }}
115+
116+
# 创建 GitHub Release 并上传所有附件
117+
publish_release:
118+
name: Publish Release
119+
# 依赖 build_binaries ,确保所有平台都构建完成后再执行
120+
needs: build_binaries
121+
runs-on: ubuntu-latest
122+
# 明确为该作业授予写入仓库内容的权
123+
permissions:
124+
contents: write
125+
126+
steps:
127+
# 步骤1: 下载所有由 build_binaries 作业上传的构建产物
128+
- name: Download all artifacts
129+
uses: actions/download-artifact@v4
130+
with:
131+
# 将所有产物下载到这个目录下
132+
path: release-assets
133+
134+
# 步骤2: 创建 Release 并上传所有文件
135+
- name: Create Release and Upload Assets
136+
uses: softprops/action-gh-release@v2
137+
with:
138+
# 从下载的产物中查找所有压缩包进行上传
139+
files: release-assets/*/*
140+
# 自动根据本次发布的 commit 历史生成 Release Notes
141+
generate_release_notes: true

.github/workflows/test.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Rust Test
2+
3+
# 触发条件:
4+
# 1. 推送到 main 分支
5+
# 2. 向 main 分支发起 Pull Request
6+
# 3. 推送任何 'v*' 格式的 tag (例如 v0.1.0, v1.2.3)
7+
on:
8+
push:
9+
branches: [ "main" ]
10+
tags:
11+
- 'v*'
12+
pull_request:
13+
branches: [ "main" ]
14+
15+
# 测试目标平台
16+
jobs:
17+
test:
18+
name: Test on ${{ matrix.platform }}
19+
runs-on: ${{ matrix.os }}
20+
strategy:
21+
matrix:
22+
include:
23+
# x86 平台的64位 Linux 系统
24+
- os: ubuntu-latest
25+
platform: x86_64-unknown-linux-gnu
26+
# x86 平台的64位 Windows 系统
27+
- os: windows-latest
28+
platform: x86_64-pc-windows-msvc
29+
# Arm 平台的64位 Linux 系统
30+
- os: ubuntu-latest
31+
platform: aarch64-unknown-linux-gnu
32+
# Arm 平台的64位 MacOS 系统
33+
- os: macos-latest
34+
platform: aarch64-apple-darwin
35+
36+
steps:
37+
# 步骤1: 检出代码
38+
- name: Checkout code
39+
uses: actions/checkout@v4
40+
# 步骤2: 安装 Rust 工具链
41+
- name: Install Rust
42+
uses: actions-rust-lang/setup-rust-toolchain@v1
43+
with:
44+
toolchain: stable
45+
target: ${{ matrix.platform }}
46+
# 步骤3: 设置 Cargo 缓存,加速后续构建
47+
- name: Cache Cargo dependencies
48+
uses: Swatinem/rust-cache@v2
49+
50+
# 步骤4: 运行代码静态分析 (Clippy)
51+
# 使用 -D warnings 参数将所有警告视为错误,保证代码质量
52+
- name: Run Clippy
53+
run: cargo clippy -- -D warnings
54+
55+
# 步骤5: 检查代码格式
56+
- name: Check formatting
57+
run: cargo fmt -- --check
58+
59+
# 步骤6: 运行测试
60+
- name: Run tests
61+
run: cargo test --verbose

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "baitts-cli-rs"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
edition = "2024"
55

66
[dependencies]

0 commit comments

Comments
 (0)