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
79
name: Release and Publish
on:
push:
branches:
- release
permissions:
contents: write
jobs:
publish:
name: Create release and publish crate
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Read package metadata
shell: bash
run: |
python - <<'PY' >> "$GITHUB_ENV"
import pathlib
import tomllib
cargo_toml = tomllib.loads(pathlib.Path("Cargo.toml").read_text(encoding="utf-8"))
package = cargo_toml["package"]
name = package["name"]
version = package["version"]
print(f"PACKAGE_NAME={name}")
print(f"PACKAGE_VERSION={version}")
print(f"RELEASE_TAG={name}-v{version}")
print(f"RELEASE_TITLE={name} v{version}")
PY
- name: Create GitHub release
shell: bash
run: |
if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then
echo "Release $RELEASE_TAG already exists."
else
gh release create "$RELEASE_TAG" \
--target "$GITHUB_SHA" \
--title "$RELEASE_TITLE" \
--generate-notes
fi
- name: Check crates.io for published version
id: crates
shell: bash
run: |
status="$(curl -s -o /dev/null -w "%{http_code}" "https://crates.io/api/v1/crates/$PACKAGE_NAME/$PACKAGE_VERSION")"
case "$status" in
200)
echo "already_published=true" >> "$GITHUB_OUTPUT"
;;
404)
echo "already_published=false" >> "$GITHUB_OUTPUT"
;;
*)
echo "Unexpected crates.io response: $status"
exit 1
;;
esac
- name: Publish to crates.io
if: steps.crates.outputs.already_published != 'true'
run: cargo publish --locked --package "$PACKAGE_NAME" --token "$CARGO_REGISTRY_TOKEN"