|
1 | | -# .github/workflows/rust.yml |
2 | | - |
3 | | -name: Rust CI |
| 1 | +name: Build and Release |
4 | 2 |
|
5 | 3 | on: |
6 | 4 | push: |
7 | 5 | tags: |
8 | | - - 'v*.*.*' |
9 | | - |
10 | | -env: |
11 | | - CARGO_TERM_COLOR: always # 让 cargo 输出带颜色,方便在日志中查看 |
| 6 | + - "v*.*.*" |
12 | 7 |
|
13 | 8 | jobs: |
14 | | - # --- 1. 测试和基础检查 --- |
15 | | - test: |
16 | | - name: Test Suite |
17 | | - runs-on: ubuntu-latest |
18 | | - steps: |
19 | | - - name: Checkout code |
20 | | - uses: actions/checkout@v4 |
21 | | - |
22 | | - - name: Install Rust |
23 | | - uses: dtolnay/rust-toolchain@stable |
24 | | - with: |
25 | | - components: rustfmt, clippy |
26 | | - |
27 | | - - name: Cache dependencies |
28 | | - uses: actions/cache@v3 |
29 | | - with: |
30 | | - path: | |
31 | | - ~/.cargo/bin/ |
32 | | - ~/.cargo/registry/index/ |
33 | | - ~/.cargo/registry/cache/ |
34 | | - ~/.cargo/git/db/ |
35 | | - target/ |
36 | | - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
37 | | - restore-keys: | |
38 | | - ${{ runner.os }}-cargo- |
39 | | -
|
40 | | - - name: Check code formatting |
41 | | - run: cargo fmt --all -- --check |
42 | | - |
43 | | - - name: Run linter (Clippy) |
44 | | - run: cargo clippy --all-targets --all-features -- -D warnings |
45 | | - |
46 | | - - name: Run tests |
47 | | - run: cargo test --verbose |
48 | | - |
49 | | - # --- 2. 构建 Release --- |
50 | 9 | build: |
51 | | - name: Build Release |
52 | | - # 只有在推送标签时才运行此 job |
53 | | - if: startsWith(github.ref, 'refs/tags/') |
54 | | - needs: test # 依赖于 test job 成功 |
| 10 | + runs-on: ${{ matrix.os }} |
55 | 11 | strategy: |
56 | 12 | matrix: |
57 | | - # 定义要构建的目标平台 |
58 | 13 | include: |
| 14 | + # Linux x86_64(添加 ubuntu 后缀) |
59 | 15 | - os: ubuntu-latest |
60 | 16 | target: x86_64-unknown-linux-gnu |
61 | | - artifact_name: fire-cli |
62 | | - asset_name: fire-cli-linux-x86_64 |
63 | | - - os: windows-latest |
64 | | - target: x86_64-pc-windows-msvc |
65 | | - artifact_name: fire-cli.exe |
66 | | - asset_name: fire-cli-windows-x86_64.exe |
| 17 | + asset_name: fire-linux-x86_64 |
| 18 | + original_binary: fire-cli # 原始产物名 |
| 19 | + renamed_binary: fire-cli-ubuntu # 带平台后缀的产物名 |
| 20 | + # macOS x86_64(添加 macos 后缀) |
67 | 21 | - os: macos-latest |
68 | 22 | target: x86_64-apple-darwin |
69 | | - artifact_name: fire-cli |
70 | | - asset_name: fire-cli-macos-x86_64 |
71 | | - - os: macos-latest |
72 | | - target: aarch64-apple-darwin |
73 | | - artifact_name: fire-cli |
74 | | - asset_name: fire-cli-macos-aarch64 |
| 23 | + asset_name: fire-macos-x86_64 |
| 24 | + original_binary: fire-cli |
| 25 | + renamed_binary: fire-cli-macos |
| 26 | + # Windows x86_64(保持原名) |
| 27 | + - os: windows-latest |
| 28 | + target: x86_64-pc-windows-msvc |
| 29 | + asset_name: fire-windows-x86_64.exe |
| 30 | + original_binary: fire-cli.exe |
| 31 | + renamed_binary: fire-cli.exe # 不修改 |
75 | 32 |
|
76 | | - runs-on: ${{ matrix.os }} |
77 | 33 | steps: |
78 | 34 | - name: Checkout code |
79 | 35 | uses: actions/checkout@v4 |
80 | 36 |
|
81 | | - - name: Install Rust |
82 | | - uses: dtolnay/rust-toolchain@stable |
| 37 | + - name: Set up Rust |
| 38 | + uses: actions-rs/toolchain@v1 |
83 | 39 | with: |
84 | | - # 为每个目标平台添加对应的工具链 |
85 | | - targets: ${{ matrix.target }} |
| 40 | + toolchain: stable |
| 41 | + profile: minimal |
| 42 | + override: true |
| 43 | + target: ${{ matrix.target }} |
86 | 44 |
|
87 | | - - name: Cache dependencies |
88 | | - uses: actions/cache@v3 |
89 | | - with: |
90 | | - path: | |
91 | | - ~/.cargo/bin/ |
92 | | - ~/.cargo/registry/index/ |
93 | | - ~/.cargo/registry/cache/ |
94 | | - ~/.cargo/git/db/ |
95 | | - target/ |
96 | | - key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }} |
97 | | - restore-keys: | |
98 | | - ${{ runner.os }}-${{ matrix.target }}-cargo- |
99 | | -
|
100 | | - - name: Build for target |
| 45 | + # 构建产物 |
| 46 | + - name: Build project |
101 | 47 | run: cargo build --release --target ${{ matrix.target }} |
102 | | - |
103 | | - - name: Strip binary (Unix) |
104 | | - # 在 Linux 和 macOS 上,strip 可以减小二进制文件大小 |
105 | | - if: matrix.os != 'windows-latest' |
106 | | - run: strip target/${{ matrix.target }}/release/${{ matrix.artifact_name }} |
107 | | - |
| 48 | + env: |
| 49 | + RUST_BACKTRACE: full |
| 50 | + |
| 51 | + # 调试:查看构建产物原始名称 |
| 52 | + - name: Debug build output (original) |
| 53 | + run: | |
| 54 | + echo "=== 构建目录: target/${{ matrix.target }}/release ===" |
| 55 | + ls -la target/${{ matrix.target }}/release/ |
| 56 | + shell: bash |
| 57 | + |
| 58 | + # 重命名产物(仅 Ubuntu 和 macOS) |
| 59 | + - name: Rename binary with platform suffix |
| 60 | + if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' |
| 61 | + run: | |
| 62 | + # 重命名原始产物为带平台后缀的名称 |
| 63 | + mv target/${{ matrix.target }}/release/${{ matrix.original_binary }} \ |
| 64 | + target/${{ matrix.target }}/release/${{ matrix.renamed_binary }} |
| 65 | + shell: bash |
| 66 | + |
| 67 | + # 调试:确认重命名后的产物 |
| 68 | + - name: Debug build output (renamed) |
| 69 | + run: | |
| 70 | + echo "=== 重命名后构建目录: target/${{ matrix.target }}/release ===" |
| 71 | + ls -la target/${{ matrix.target }}/release/ |
| 72 | + shell: bash |
| 73 | + |
| 74 | + # 上传重命名后的产物 |
108 | 75 | - name: Upload artifact |
109 | | - # 将构建好的二进制文件作为 GitHub Actions 的 artifact 保存 |
110 | | - uses: actions/upload-artifact@v3 |
| 76 | + uses: actions/upload-artifact@v4 |
111 | 77 | with: |
112 | 78 | name: ${{ matrix.asset_name }} |
113 | | - path: target/${{ matrix.target }}/release/${{ matrix.artifact_name }} |
| 79 | + path: target/${{ matrix.target }}/release/${{ matrix.renamed_binary }} # 上传重命名后的文件 |
| 80 | + if-no-files-found: error |
114 | 81 |
|
115 | | - # --- 3. 发布 Release --- |
116 | 82 | release: |
117 | | - name: Release |
118 | | - # 只有在推送标签时才运行 |
119 | | - if: startsWith(github.ref, 'refs/tags/') |
120 | | - needs: [test, build] # 依赖于 test 和 build job 成功 |
121 | 83 | runs-on: ubuntu-latest |
| 84 | + needs: build |
122 | 85 | steps: |
123 | | - - name: Checkout code |
124 | | - uses: actions/checkout@v4 |
125 | | - |
126 | 86 | - name: Download all artifacts |
127 | | - # 从 build job 下载所有构建好的二进制文件 |
128 | | - uses: actions/download-artifact@v3 |
| 87 | + uses: actions/download-artifact@v4 |
129 | 88 | with: |
130 | 89 | path: artifacts |
131 | 90 |
|
132 | | - - name: Display structure of downloaded files |
133 | | - run: ls -R artifacts/ |
| 91 | + # 验证最终产物路径(带后缀) |
| 92 | + - name: Debug artifacts structure |
| 93 | + run: | |
| 94 | + sudo apt-get install -y tree |
| 95 | + echo "=== 最终 artifacts 目录结构 ===" |
| 96 | + tree artifacts |
| 97 | + shell: bash |
134 | 98 |
|
135 | | - - name: Create Release |
136 | | - # 使用 GitHub CLI 创建一个 Release |
137 | | - id: create_release |
138 | | - uses: actions/create-release@v1 |
139 | | - env: |
140 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + - name: Create GitHub Release |
| 100 | + uses: softprops/action-gh-release@v2 |
141 | 101 | with: |
142 | | - tag_name: ${{ github.ref_name }} # 使用推送的标签名 |
143 | | - release_name: Release ${{ github.ref_name }} |
144 | | - draft: false # 是否是草稿 |
145 | | - prerelease: false # 是否是预发布版本 |
146 | | - |
147 | | - - name: Upload Release Assets |
148 | | - # 将下载的 artifacts 作为附件上传到刚刚创建的 Release |
149 | | - uses: softprops/action-gh-release@v1 |
| 102 | + tag_name: ${{ github.ref }} |
| 103 | + name: Release ${{ github.ref_name }} |
| 104 | + body: | |
| 105 | + ## Changes in this Release |
| 106 | + - detail to see [CHANGELOG](https://github.com/xiaoxustudio/fire-cli/blob/master/CHANGELOG.md) |
| 107 | + files: | |
| 108 | + artifacts/fire-linux-x86_64/fire-cli-ubuntu |
| 109 | + artifacts/fire-macos-x86_64/fire-cli-macos |
| 110 | + artifacts/fire-windows-x86_64.exe/fire-cli.exe |
150 | 111 | env: |
151 | 112 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
152 | | - with: |
153 | | - files: artifacts/**/* |
0 commit comments