Build and Release Project #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Release Project | |
| on: | |
| workflow_dispatch: | |
| # 为工作流授予创建 Release 的权限 | |
| permissions: | |
| contents: write | |
| jobs: | |
| build: | |
| name: Build for ${{ matrix.target }} | |
| runs-on: ${{ matrix.os }} | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - target: x86_64-unknown-linux-gnu | |
| os: ubuntu-latest | |
| - target: x86_64-pc-windows-msvc | |
| os: windows-latest | |
| - target: x86_64-apple-darwin | |
| os: macos-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Install Rust toolchain | |
| uses: dtolnay/rust-toolchain@stable | |
| with: | |
| targets: ${{ matrix.target }} | |
| - name: Install dependencies (Linux) | |
| if: runner.os == 'Linux' | |
| run: sudo apt-get update && sudo apt-get install -y libssl-dev pkg-config | |
| - name: Cache dependencies | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.cargo/bin/ | |
| ~/.cargo/registry/index/ | |
| ~/.cargo/registry/cache/ | |
| ~/.cargo/git/db/ | |
| target/ | |
| key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} | |
| restore-keys: | | |
| ${{ runner.os }}-cargo- | |
| - name: Build binary with cargo | |
| run: cargo build --release --target ${{ matrix.target }} --verbose | |
| # 步骤 1: 将编译产物重命名为包含平台信息的唯一文件名 | |
| - name: Prepare artifact for upload | |
| id: prepare_artifact | |
| shell: bash | |
| run: | | |
| BIN_NAME="telembed" | |
| TARGET="${{ matrix.target }}" | |
| # 定义原始二进制文件的路径 | |
| SOURCE_PATH="target/${TARGET}/release/${BIN_NAME}" | |
| # 定义新的、包含平台信息的唯一文件名 | |
| FINAL_FILENAME="${BIN_NAME}-${TARGET}" | |
| # 根据操作系统处理 .exe 后缀 | |
| if [[ "${{ runner.os }}" == "Windows" ]]; then | |
| # 在原始位置重命名文件 | |
| mv "${SOURCE_PATH}.exe" "target/${TARGET}/release/${FINAL_FILENAME}.exe" | |
| # 将重命名后的完整路径和新文件名设置为输出 | |
| echo "bin_path=target/${TARGET}/release/${FINAL_FILENAME}.exe" >> $GITHUB_OUTPUT | |
| echo "artifact_name=${FINAL_FILENAME}.exe" >> $GITHUB_OUTPUT | |
| else | |
| # 在原始位置重命名文件 | |
| mv "$SOURCE_PATH" "target/${TARGET}/release/$FINAL_FILENAME" | |
| # 将重命名后的完整路径和新文件名设置为输出 | |
| echo "bin_path=target/${TARGET}/release/${FINAL_FILENAME}" >> $GITHUB_OUTPUT | |
| echo "artifact_name=${FINAL_FILENAME}" >> $GITHUB_OUTPUT | |
| fi | |
| # 步骤 2: 上传被唯一重命名后的二进制文件 | |
| - name: Upload artifact for release job | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| # artifact 的名字也是唯一的 | |
| name: ${{ steps.prepare_artifact.outputs.artifact_name }} | |
| # path 直接指向那个被重命名后的唯一文件 | |
| path: ${{ steps.prepare_artifact.outputs.bin_path }} | |
| release: | |
| name: Create GitHub Release | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Generate timestamp tag | |
| id: generate_tag | |
| run: echo "TAG=$(date +'%Y.%m.%d.%H%M%S')" >> $GITHUB_OUTPUT | |
| - name: Download all build artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| path: artifacts/ | |
| - name: Display structure of downloaded files | |
| run: ls -R artifacts | |
| - name: Create Release and Upload Assets | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: ${{ steps.generate_tag.outputs.TAG }} | |
| name: "Release ${{ steps.generate_tag.outputs.TAG }}" | |
| generate_release_notes: true | |
| files: | | |
| artifacts/**/* |