diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..9538b6d --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,154 @@ +name: 构建发布 + +on: + push: + tags: + - 'v*' # 推送 tag 时触发发布 + workflow_dispatch: # 支持手动触发 + inputs: + publish-to-pypi: + description: '发布至 PyPI (true/false)' + required: false + default: 'false' + +env: + CARGO_TERM_COLOR: always + +jobs: + # ============================================================ + # Linux wheels (x86_64 + aarch64) + # ============================================================ + linux: + runs-on: ubuntu-latest + strategy: + matrix: + target: [x86_64, aarch64] + steps: + - uses: actions/checkout@v4 + + - name: 安装 Rust 工具链 + uses: dtolnay/rust-toolchain@stable + + - name: 构建 wheel + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + working-directory: chanlun-py + args: --release --out dist + manylinux: auto + before-script-linux: | + if [ "${{ matrix.target }}" = "aarch64" ]; then + sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu + fi + + - name: 上传 wheel 产物 + uses: actions/upload-artifact@v4 + with: + name: wheels-linux-${{ matrix.target }} + path: chanlun-py/dist/ + + # ============================================================ + # macOS wheels (x86_64 + arm64) + # ============================================================ + macos: + runs-on: macos-latest + strategy: + matrix: + target: [x86_64, aarch64] + steps: + - uses: actions/checkout@v4 + + - name: 安装 Rust 工具链 + uses: dtolnay/rust-toolchain@stable + + - name: 构建 wheel + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + working-directory: chanlun-py + args: --release --out dist + + - name: 上传 wheel 产物 + uses: actions/upload-artifact@v4 + with: + name: wheels-macos-${{ matrix.target }} + path: chanlun-py/dist/ + + # ============================================================ + # Windows wheels (x86_64) + # ============================================================ + windows: + runs-on: windows-latest + strategy: + matrix: + target: [x64] + steps: + - uses: actions/checkout@v4 + + - name: 安装 Rust 工具链 + uses: dtolnay/rust-toolchain@stable + + - name: 构建 wheel + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + working-directory: chanlun-py + args: --release --out dist + + - name: 上传 wheel 产物 + uses: actions/upload-artifact@v4 + with: + name: wheels-windows-${{ matrix.target }} + path: chanlun-py/dist/ + + # ============================================================ + # 源码分发包 (sdist) + # ============================================================ + sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: 构建 sdist + uses: PyO3/maturin-action@v1 + with: + command: sdist + working-directory: chanlun-py + args: --out dist + + - name: 上传 sdist 产物 + uses: actions/upload-artifact@v4 + with: + name: wheels-sdist + path: chanlun-py/dist/ + + # ============================================================ + # 发布至 PyPI + # ============================================================ + publish: + needs: [linux, macos, windows, sdist] + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') || github.event.inputs.publish-to-pypi == 'true' + permissions: + id-token: write # PyPI 信任发布(推荐) + + steps: + - name: 下载所有产物 + uses: actions/download-artifact@v4 + with: + path: dist/ + pattern: wheels-* + merge-multiple: true + + - name: 列出产物 + run: ls -la dist/ + + - name: 发布至 PyPI + uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: dist/ + # 使用 OIDC 信任发布(推荐),无需配置 token + # 若使用 API token,在 GitHub Secrets 中设置: + # PYPI_TOKEN: pypi-xxxxxxxxxxxx + # 然后将下一行改为: + # password: ${{ secrets.PYPI_TOKEN }}