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
name: Publish
# Publishes the crate to crates.io when a version tag is pushed.
#
# Setup (one-time):
# 1. Create a crates.io API token: https://crates.io/settings/tokens
# 2. Add it to the repo as a secret named `CARGO_REGISTRY_TOKEN`
# (Settings -> Secrets and variables -> Actions -> New repository secret).
#
# Release flow:
# 1. Bump `version` in Cargo.toml and commit.
# 2. Tag the commit, e.g. git tag v0.1.0 && git push origin v0.1.0
# 3. This workflow verifies, then publishes the crate.
on:
push:
tags:
- "v*.*.*"
env:
CARGO_TERM_COLOR: always
jobs:
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo
uses: Swatinem/rust-cache@v2
- name: Verify tag matches Cargo.toml version
run: |
TAG="${GITHUB_REF_NAME#v}"
CRATE_VERSION="$(cargo metadata --no-deps --format-version 1 \
| python3 -c 'import sys, json; print(json.load(sys.stdin)["packages"][0]["version"])')"
echo "tag=$TAG crate=$CRATE_VERSION"
if [ "$TAG" != "$CRATE_VERSION" ]; then
echo "::error::Tag v$TAG does not match Cargo.toml version $CRATE_VERSION"
exit 1
fi
- name: Test
run: cargo test --verbose
- name: Package (dry run)
run: cargo publish --dry-run
- name: Publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish