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
name: Release
# Cut a release by pushing a version tag, e.g.:
# git tag v0.2.0 && git push origin v0.2.0
#
# The publish job runs only after the gates pass AND a reviewer approves the
# `crates-io` environment, so a tag push alone never publishes unattended.
#
# FIRST RELEASE BOOTSTRAP: crates.io has no "pending" trusted publisher, so the
# very first `cargo publish` of this crate must be done manually with an API
# token (see RELEASING.md). Only after that can you register the trusted
# publisher on crates.io and let this workflow publish v0.2.0 onward.
on:
push:
tags:
permissions:
contents: read
env:
CARGO_TERM_COLOR: always
jobs:
# Re-run the full gate on the exact tagged commit before anything is published.
verify:
name: verify
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- run: cargo fmt --all -- --check
- run: cargo clippy --all-targets -- -D warnings
- run: cargo test --all-targets
- run: cargo test --doc
- name: Check the tag matches the crate version
run: |
tag="${GITHUB_REF_NAME#v}"
crate="$(cargo metadata --no-deps --format-version 1 \
| jq -r '.packages[0].version')"
if [ "$tag" != "$crate" ]; then
echo "::error::tag $GITHUB_REF_NAME does not match Cargo.toml version $crate"
exit 1
fi
publish:
name: publish to crates.io
needs: verify
runs-on: ubuntu-latest
# Gate behind a protected environment: add required reviewers in
# Settings > Environments > crates-io so a human approves each publish.
environment:
name: crates-io
url: https://crates.io/crates/rust-j2k
permissions:
contents: write # create the GitHub Release
id-token: write # OIDC exchange for crates.io trusted publishing
steps:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable
# Trusted publishing: mint a short-lived crates.io token via OIDC, no
# long-lived CARGO_REGISTRY_TOKEN secret. Configure the trusted publisher
# on crates.io (repo + this workflow + the crates-io environment) first.
- uses: rust-lang/crates-io-auth-action@v1
id: auth
- run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
- name: Create GitHub Release
env:
GH_TOKEN: ${{ github.token }}
run: |
gh release create "$GITHUB_REF_NAME" \
--title "$GITHUB_REF_NAME" \
--notes "See [CHANGELOG.md](https://github.com/${GITHUB_REPOSITORY}/blob/main/CHANGELOG.md)."