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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
name: Release
on:
workflow_dispatch:
push:
branches:
paths:
- "Cargo.toml"
permissions:
contents: write
jobs:
release:
name: Release crate
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
- name: Read version from Cargo.toml
id: version
run: |
version=$(python3 - << 'PY'
import tomllib
from pathlib import Path
data = tomllib.loads(Path("Cargo.toml").read_text())
print(data["package"]["version"])
PY
)
echo "value=$version" >> "$GITHUB_OUTPUT"
echo "Crate version: $version"
- name: Determine tag name
id: tag
run: |
TAG="v${{ steps.version.outputs.value }}"
echo "value=$TAG" >> "$GITHUB_OUTPUT"
echo "Tag: $TAG"
- name: Check if tag already exists
id: check_tag
run: |
git fetch --tags
TAG="${{ steps.tag.outputs.value }}"
if git tag -l "$TAG" | grep -q "^$TAG$"; then
echo "exists=true" >> "$GITHUB_OUTPUT"
echo "Tag $TAG already exists. Skipping release."
else
echo "exists=false" >> "$GITHUB_OUTPUT"
echo "Tag $TAG does not exist yet."
fi
- name: Set Git identity
if: steps.check_tag.outputs.exists == 'false'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Run publish dry-run
if: steps.check_tag.outputs.exists == 'false'
run: cargo publish --dry-run
- name: Create and push tag
if: steps.check_tag.outputs.exists == 'false'
run: |
TAG="${{ steps.tag.outputs.value }}"
echo "Creating annotated tag $TAG"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
- name: Create GitHub Release
if: steps.check_tag.outputs.exists == 'false'
env:
GH_TOKEN: ${{ github.token }}
run: |
TAG="${{ steps.tag.outputs.value }}"
gh release create "$TAG" \
--title "$TAG" \
--generate-notes
- name: Publish to crates.io
if: steps.check_tag.outputs.exists == 'false'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
run: cargo publish