feat(deploy): add release automation and systemd service #1
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: | |
| push: | |
| branches: | |
| - main # 仅在 main 分支有 push 时触发 | |
| 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 | |
| - name: Prepare artifact name and path | |
| id: prepare_artifact | |
| shell: bash | |
| run: | | |
| BIN_NAME="telembed" | |
| TARGET="${{ matrix.target }}" | |
| if [[ "${{ runner.os }}" == "Windows" ]]; then | |
| BIN_PATH="target/${TARGET}/release/${BIN_NAME}.exe" | |
| ARTIFACT_NAME="${BIN_NAME}-${TARGET}.exe" | |
| else | |
| BIN_PATH="target/${TARGET}/release/${BIN_NAME}" | |
| ARTIFACT_NAME="${BIN_NAME}-${TARGET}" | |
| fi | |
| echo "artifact_name=${ARTIFACT_NAME}" >> $GITHUB_OUTPUT | |
| echo "bin_path=${BIN_PATH}" >> $GITHUB_OUTPUT | |
| # 此步骤上传的 artifact 会被自动压缩成 zip,但这只是为了在 job 之间传递 | |
| - name: Upload artifact for release job | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: ${{ steps.prepare_artifact.outputs.artifact_name }} | |
| 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 | |
| # 重要提示: | |
| # 上一步的 download-artifact 会自动解压在 build 任务中生成的压缩包。 | |
| # 因此,'artifacts' 目录中现在存放的是原始的、未压缩的二进制文件。 | |
| # 下面的步骤会将这些原始二进制文件直接作为附件上传到 Release 中。 | |
| - 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' 目录下的所有文件 | |
| # 并将它们作为独立的、未压缩的文件上传 | |
| files: | | |
| artifacts/**/* |