|
1 | | -name: Rust CLI Release |
| 1 | +name: Rust Build and Release |
2 | 2 |
|
3 | 3 | on: |
4 | | - # 当一个新的 Tag (例如 v1.0.0) 被推送到仓库时触发 |
5 | 4 | push: |
6 | 5 | tags: |
7 | | - - "v[0-9]+.*" # 匹配所有以 'v' 开头的版本标签 |
| 6 | + - "v*" # 仅在推送 v 开头的 tag 时触发 |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +env: |
| 12 | + # !!! 请在这里修改你的 Rust 项目名称 (即 Cargo.toml 中的 name 字段) !!! |
| 13 | + BINARY_NAME: transfer |
8 | 14 |
|
9 | 15 | jobs: |
10 | | - build_and_release: |
11 | | - name: Build & Release on ${{ matrix.os }} |
| 16 | + build: |
| 17 | + name: Build on ${{ matrix.os }} |
12 | 18 | runs-on: ${{ matrix.os }} |
13 | | - |
14 | | - # 定义编译平台矩阵 |
15 | 19 | strategy: |
| 20 | + fail-fast: false |
16 | 21 | matrix: |
17 | | - os: [ubuntu-latest, macos-latest, windows-latest] |
| 22 | + include: |
| 23 | + # Linux |
| 24 | + - os: ubuntu-latest |
| 25 | + target: x86_64-unknown-linux-gnu |
| 26 | + artifact_name: linux-amd64 |
| 27 | + # Linux 编译产物没有后缀 |
| 28 | + bin_suffix: "" |
| 29 | + |
| 30 | + # Windows |
| 31 | + - os: windows-latest |
| 32 | + target: x86_64-pc-windows-msvc |
| 33 | + artifact_name: windows-amd64 |
| 34 | + # Windows 编译产物有 .exe 后缀 |
| 35 | + bin_suffix: ".exe" |
| 36 | + |
| 37 | + # macOS (Intel) - 如果需要 Apple Silicon,可增加 macos-14 和 aarch64 目标 |
| 38 | + - os: macos-latest |
| 39 | + target: x86_64-apple-darwin |
| 40 | + artifact_name: macos-amd64 |
| 41 | + bin_suffix: "" |
18 | 42 |
|
19 | 43 | steps: |
20 | | - - name: ⬇️ Checkout code |
| 44 | + - name: Checkout code |
21 | 45 | uses: actions/checkout@v4 |
22 | 46 |
|
23 | | - # ------------------------------------------------------------------ |
24 | | - # 步骤 1:设置 Rust 编译环境 |
25 | | - # ------------------------------------------------------------------ |
26 | | - - name: 🛠️ Setup Rust environment |
27 | | - uses: actions-rs/toolchain@v1 |
| 47 | + # 安装 Rust 工具链 (社区推荐方式) |
| 48 | + - name: Install Rust toolchain |
| 49 | + uses: dtolnay/rust-toolchain@stable |
28 | 50 | with: |
29 | | - toolchain: stable |
30 | | - profile: minimal |
31 | | - override: true |
32 | | - |
33 | | - # ------------------------------------------------------------------ |
34 | | - # 步骤 2:编译程序 |
35 | | - # ------------------------------------------------------------------ |
36 | | - - name: 🏗️ Compile CLI Tool (Release) |
37 | | - id: compile |
38 | | - shell: bash |
39 | | - run: | |
40 | | - VERSION=${{ github.ref_name }} |
41 | | - # 假设你的项目名在 Cargo.toml 中是 'mytool' |
42 | | - PROJECT_NAME="transfer" |
43 | | -
|
44 | | - # 使用 --release 编译,生成优化的二进制文件 |
45 | | - cargo build --release |
| 51 | + targets: ${{ matrix.target }} |
46 | 52 |
|
47 | | - # 确定可执行文件路径和最终名称 |
48 | | - if [[ "${{ matrix.os }}" == "windows-latest" ]]; then |
49 | | - EXECUTABLE_PATH="target/release/${PROJECT_NAME}.exe" |
50 | | - OUTPUT_NAME="${PROJECT_NAME}-${VERSION}-windows-x64.zip" |
51 | | - else |
52 | | - EXECUTABLE_PATH="target/release/${PROJECT_NAME}" |
53 | | - OUTPUT_NAME="${PROJECT_NAME}-${VERSION}-${{ matrix.os }}-x64.tar.gz" |
54 | | - fi |
| 53 | + # (可选) 缓存 Cargo 依赖,加速构建 |
| 54 | + - name: Rust Cache |
| 55 | + uses: swatinem/rust-cache@v2 |
55 | 56 |
|
56 | | - # 设置输出变量 |
57 | | - echo "project_name=${PROJECT_NAME}" >> $GITHUB_OUTPUT |
58 | | - echo "executable_path=${EXECUTABLE_PATH}" >> $GITHUB_OUTPUT |
59 | | - echo "output_name=${OUTPUT_NAME}" >> $GITHUB_OUTPUT |
| 57 | + # 编译 Release 版本 |
| 58 | + - name: Build |
| 59 | + run: cargo build --release --target ${{ matrix.target }} |
60 | 60 |
|
61 | | - # ------------------------------------------------------------------ |
62 | | - # 步骤 3:打包可执行文件 |
63 | | - # ------------------------------------------------------------------ |
64 | | - - name: 📦 Archive (Tar for Linux/macOS, Zip for Windows) |
65 | | - id: archive |
| 61 | + # 准备产物:重命名文件以便上传 |
| 62 | + - name: Prepare artifact |
66 | 63 | shell: bash |
67 | 64 | run: | |
68 | | - OUTPUT_NAME=${{ steps.compile.outputs.output_name }} |
69 | | - EXECUTABLE_PATH=${{ steps.compile.outputs.executable_path }} |
70 | | - PROJECT_NAME=${{ steps.compile.outputs.project_name }} |
| 65 | + mkdir -p build_assets |
| 66 | + # 将原始二进制文件复制并重命名,例如:my-rust-app-linux-amd64 |
| 67 | + cp target/${{ matrix.target }}/release/${{ env.BINARY_NAME }}${{ matrix.bin_suffix }} build_assets/${{ env.BINARY_NAME }}-${{ matrix.artifact_name }}${{ matrix.bin_suffix }} |
71 | 68 |
|
72 | | - if [[ "${{ matrix.os }}" == "windows-latest" ]]; then |
73 | | - # Windows 使用 zip |
74 | | - # 需要安装 zip 工具 |
75 | | - # Add-Type -AssemblyName System.IO.Compression.FileSystem |
76 | | - # [System.IO.Compression.ZipFile]::CreateFromDirectory(".", "$OUTPUT_NAME") |
77 | | - # 使用 powershell 的 Compress-Archive |
78 | | - Compress-Archive -Path $EXECUTABLE_PATH -DestinationPath $OUTPUT_NAME |
79 | | - else |
80 | | - # Linux/macOS 使用 tar.gz |
81 | | - tar -czvf $OUTPUT_NAME -C "$(dirname $EXECUTABLE_PATH)" "$(basename $EXECUTABLE_PATH)" |
82 | | - fi |
| 69 | + # 上传处理好的文件到 GitHub Actions 临时存储 |
| 70 | + - name: Upload artifact |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_name }} |
| 74 | + path: build_assets/* |
83 | 75 |
|
84 | | - # 设置打包文件的路径供下一步上传使用 |
85 | | - echo "asset_path=$OUTPUT_NAME" >> $GITHUB_OUTPUT |
86 | | - echo "asset_name=$OUTPUT_NAME" >> $GITHUB_OUTPUT |
| 76 | + release: |
| 77 | + name: Create Release |
| 78 | + needs: build |
| 79 | + runs-on: ubuntu-latest |
| 80 | + steps: |
| 81 | + - name: Download all artifacts |
| 82 | + uses: actions/download-artifact@v4 |
| 83 | + with: |
| 84 | + path: release_assets |
| 85 | + merge-multiple: true |
87 | 86 |
|
88 | | - # ------------------------------------------------------------------ |
89 | | - # 步骤 4:创建和上传 Release Assets |
90 | | - # ------------------------------------------------------------------ |
91 | | - - name: 🚀 Upload Release Asset |
92 | | - uses: softprops/action-gh-release@v1 |
| 87 | + - name: Create Release |
| 88 | + uses: softprops/action-gh-release@v2 |
93 | 89 | if: startsWith(github.ref, 'refs/tags/') |
94 | 90 | with: |
| 91 | + files: release_assets/* |
| 92 | + generate_release_notes: true |
95 | 93 | draft: false |
96 | 94 | prerelease: false |
97 | | - files: ${{ steps.archive.outputs.asset_path }} |
| 95 | + env: |
| 96 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments