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
name: Release
on:
push:
branches:
paths:
- 'Cargo.toml'
workflow_dispatch:
inputs:
force:
description: 'Force publish even if version unchanged'
required: false
default: 'true'
type: boolean
env:
CARGO_TERM_COLOR: always
jobs:
check-version:
name: Check Version Change
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get_version.outputs.version }}
changed: ${{ steps.check.outputs.changed }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Get current version
id: get_version
run: |
VERSION=$(grep -m1 'version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
- name: Check if version is unpublished on crates.io
id: check
# Compare against the registry, not "git diff HEAD~1": a multi-commit
# push whose last commit lacks the version bump silently skipped
# publishing (u-routing 0.2.3 incident).
run: |
CRATE=$(grep -m1 '^name = ' Cargo.toml | sed 's/name = "\(.*\)"/\1/')
VERSION=${{ steps.get_version.outputs.version }}
HTTP=$(curl -s -o /dev/null -w '%{http_code}' -H "User-Agent: ${CRATE}-ci/1.0" "https://crates.io/api/v1/crates/${CRATE}/${VERSION}")
if [ "$HTTP" = "200" ]; then
echo "changed=false" >> $GITHUB_OUTPUT
echo "${CRATE} ${VERSION} already on crates.io — skipping"
else
echo "changed=true" >> $GITHUB_OUTPUT
echo "${CRATE} ${VERSION} not on crates.io (HTTP ${HTTP}) — will publish"
fi
publish:
name: Publish to crates.io
needs: check-version
if: needs.check-version.outputs.changed == 'true' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
# No "|| true": a failed publish must fail the run. The check-version
# gate (crates.io API) already prevents same-version re-publish.
run: cargo publish --allow-dirty