Skip to content

Commit 94f389e

Browse files
committed
feat: Add automated crate publishing workflow
A new GitHub Actions workflow named `publish-crate.yml` has been added to automate the process of publishing the Rust crate to `crates.io`. This workflow is triggered either when a new release is published on GitHub or can be manually invoked via `workflow_dispatch`. The workflow includes a crucial validation step that checks if the GitHub release tag (e.g., `v1.2.3`) precisely matches the crate's version (e.g., `1.2.3`) found in `Cargo.toml`, prefixed with 'v'. This ensures version consistency between the Git tag and the published crate. It uses `cargo publish --locked` to guarantee that the published crate is built with the exact dependency versions specified in `Cargo.lock`, enhancing build reproducibility and stability. The publishing operation requires the `CARGO_REGISTRY_TOKEN` secret for authentication with `crates.io`. This automation streamlines the release process, minimizes potential human errors, and maintains version integrity. Influence: 1. Manually trigger the "Publish to crates.io" workflow for testing purposes. 2. Create a GitHub release (e.g., `v1.0.0`) and publish it to verify automatic workflow invocation. 3. Test with a release tag that matches the `Cargo.toml` version (e.g., `v1.0.0` for `version = "1.0.0"`) to ensure successful publishing. 4. Test with a release tag that does NOT match the `Cargo.toml` version (e.g., `v1.0.1` for `version = "1.0.0"`) to confirm the validation step correctly fails the workflow. 5. Verify that the crate is successfully published to `crates.io` after a successful workflow run. 6. Ensure the `CARGO_REGISTRY_TOKEN` repository secret is correctly configured with a valid token. feat: 添加自动化 crate 发布工作流 新增了 GitHub Actions 工作流 `publish-crate.yml`,用于自动化
1 parent d63c23a commit 94f389e

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Publish to crates.io
2+
3+
on:
4+
release:
5+
types: [published]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
11+
concurrency:
12+
group: publish-crate
13+
cancel-in-progress: false
14+
15+
jobs:
16+
publish:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Install Rust
23+
uses: dtolnay/rust-toolchain@stable
24+
25+
- name: Validate tag matches crate version
26+
if: startsWith(github.ref, 'refs/tags/')
27+
run: |
28+
tag="${GITHUB_REF#refs/tags/}"
29+
version=$(cargo metadata --no-deps --format-version 1 |
30+
python3 - <<'PY'
31+
import json,sys
32+
meta=json.load(sys.stdin)
33+
print(meta['packages'][0]['version'])
34+
PY
35+
)
36+
if [ "${tag}" != "v${version}" ]; then
37+
echo "Tag ${tag} does not match crate version v${version}" >&2
38+
exit 1
39+
fi
40+
41+
- name: Publish to crates.io
42+
env:
43+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
44+
run: |
45+
if [ -z "${CARGO_REGISTRY_TOKEN}" ]; then
46+
echo "CARGO_REGISTRY_TOKEN is not set" >&2
47+
exit 1
48+
fi
49+
cargo publish --locked --token "${CARGO_REGISTRY_TOKEN}"

0 commit comments

Comments
 (0)