name: Publish to crates.io S
on:
push:
tags:
- 'v*' workflow_dispatch: inputs:
dry_run:
description: 'Dry run (只检查不发布)'
required: false
default: false
type: boolean
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
jobs:
quality-check:
name: Quality Check
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- name: Check formatting
run: |
# cargo fmt
cargo fmt -- --check
- name: Clippy check
run: |
# cargo clippy -- -D warnings
cargo clippy --all-features -- -D warnings
- name: Run tests
run: |
# cargo test
cargo test --all-features
- name: Build documentation
run: |
cargo doc --no-deps --all-features
- name: Verify package
run: |
cargo package --allow-dirty
- name: Quality report
shell: bash
run: |
echo "✅ 质量检查通过"
NAME=$(grep '^name' Cargo.toml | cut -d '"' -f2)
VERSION=$(grep '^version' Cargo.toml | cut -d '"' -f2 | head -n 1)
echo "🎉 $NAME v$VERSION"
publish:
name: Publish to crates.io
runs-on: windows-latest needs: quality-check
if: >
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')) ||
(github.event_name == 'workflow_dispatch' && !inputs.dry_run)
environment: publish
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Publish to crates.io
shell: bash
run: |
cargo publish --verbose
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
- name: Verify publication
shell: bash
run: |
sleep 10 # 等待索引更新
NAME=$(grep '^name' Cargo.toml | cut -d '"' -f2)
VERSION=$(grep '^version' Cargo.toml | cut -d '"' -f2 | head -n 1)
echo "🎉 发布成功: $NAME v$VERSION"
echo "📚 文档: https://docs.rs/$NAME/$VERSION/"
echo "📦 页面: https://crates.io/crates/$NAME/$VERSION"
dry-run:
name: Dry Run
runs-on: windows-latest needs: quality-check
if: github.event_name == 'workflow_dispatch' && inputs.dry_run
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
- name: Dry run publish
run: cargo publish --dry-run
- name: Dry run report
run: |
echo "✅ Dry run 完成 - 包可以通过所有检查"
echo "🚀 下一步: 重新运行工作流并取消 dry run 选项来实际发布"