|
| 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 |
0 commit comments