From 0eb52cc06a7dafdea943fa3761c1f76d86aa0787 Mon Sep 17 00:00:00 2001 From: YuWuKunCheng Date: Sat, 30 May 2026 11:42:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8F=91=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/publish-crates.yml | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 .github/workflows/publish-crates.yml diff --git a/.github/workflows/publish-crates.yml b/.github/workflows/publish-crates.yml new file mode 100644 index 0000000..6447b9f --- /dev/null +++ b/.github/workflows/publish-crates.yml @@ -0,0 +1,85 @@ +name: 发布至 crates.io + +on: + push: + tags: + - 'v*' + workflow_dispatch: + +env: + CARGO_TERM_COLOR: always + +jobs: + # ============================================================ + # 发布 chanlun 核心库至 crates.io + # ============================================================ + publish-chanlun: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: 安装 Rust 工具链 + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt, clippy + + - name: 缓存依赖 + uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: ${{ runner.os }}-cargo-${{ hashFiles('chanlun/Cargo.lock') }} + + - name: 提取版本号 + id: version + working-directory: chanlun + run: | + VER=$(cargo metadata --format-version 1 --no-deps 2>/dev/null \ + | jq -r '.packages[] | select(.name == "chanlun") | .version') + echo "version=$VER" >> $GITHUB_OUTPUT + echo "当前版本: $VER" + + - name: 检查版本是否已存在 + id: check + run: | + VER="${{ steps.version.outputs.version }}" + EXISTS=$(curl -sS "https://crates.io/api/v1/crates/chanlun" \ + | jq -r --arg v "$VER" '.versions[]?.num // empty | select(. == $v)') + if [ -n "$EXISTS" ]; then + echo "版本 $VER 已存在于 crates.io,跳过发布" + echo "exists=true" >> $GITHUB_OUTPUT + else + echo "版本 $VER 未发布,继续" + echo "exists=false" >> $GITHUB_OUTPUT + fi + + - name: 格式检查 + if: steps.check.outputs.exists == 'false' + working-directory: chanlun + run: cargo fmt --check + + - name: Lint 检查 + if: steps.check.outputs.exists == 'false' + working-directory: chanlun + run: cargo clippy -- -D warnings + + - name: 运行测试 + if: steps.check.outputs.exists == 'false' + working-directory: chanlun + run: cargo test + + - name: 验证打包 + if: steps.check.outputs.exists == 'false' + working-directory: chanlun + run: cargo package --dry-run + + - name: 登录 crates.io + if: steps.check.outputs.exists == 'false' + run: cargo login ${{ secrets.CARGO_TOKEN }} + + - name: 发布 chanlun 至 crates.io + if: steps.check.outputs.exists == 'false' + working-directory: chanlun + run: cargo publish