name: Publish to Crates.io
on:
release:
types:
- created
jobs:
test:
uses: ./.github/workflows/test.yml
secrets: inherit
publish_to_cargo:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
token: ${{ secrets.GITHUB_TOKEN }}
- name: Extract and validate version
id: get_version
run: |
TAG="${{ github.event.release.tag_name }}"
VERSION="${TAG#v}"
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then
echo "Invalid version format: $VERSION (expected: X.Y.Z)" && exit 1
fi
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Install cargo-edit
run: cargo install cargo-edit
- name: Update version in Cargo.toml
run: cargo set-version ${{ steps.get_version.outputs.VERSION }}
- name: Commit version bump
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Bump version to ${{ steps.get_version.outputs.VERSION }}"
file_pattern: 'Cargo.toml Cargo.lock'
branch: main
push_options: '--force'
- name: Publish to Cargo
uses: actions-rs/cargo@v1
with:
command: publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}