1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Cargo publish workflow
# 推送 v* 标签时:全 feature 下 fmt / clippy / test,通过后 cargo publish。
# 发版前务必将 Cargo.toml 的 version 与标签一致(例如标签 v0.2.1 对应 version = "0.2.1")。
name: Cargo Publish
on:
push:
tags:
- 'v*'
permissions:
contents: read
jobs:
common-test:
name: Tests & Checks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Validate version matches
shell: bash
run: |
tag_version="${GITHUB_REF_NAME#v}"
cargo_version="$(python -c 'import tomllib; from pathlib import Path; print(tomllib.load(Path("Cargo.toml").open("rb"))["package"]["version"])')"
if [ "$cargo_version" != "$tag_version" ]; then
echo "::error title=Version mismatch::Cargo.toml version '$cargo_version' does not match tag '$GITHUB_REF_NAME'"
exit 1
fi
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache Cargo dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
- name: Check code formatting
run: cargo fmt --all -- --check
- name: Run Clippy checks
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run tests
run: cargo test --all-features
publish-cargo:
name: Publish to crates.io
needs: common-test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache Cargo dependencies
uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
- name: Publish to crates.io
run: cargo publish --token ${{ secrets.CARGO_ACCESS_TOKEN }}