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