name: Publish to crates.io
on:
push:
branches:
- main
- master
paths:
- 'Cargo.toml'
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Run tests
run: cargo test --all-features
- name: Check if version is already published
id: version_check
run: |
CARGO_VERSION=$(grep '^version =' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
echo "cargo_version=$CARGO_VERSION" >> $GITHUB_OUTPUT
# Query crates.io for published version
PUBLISHED_VERSION=$(curl -s https://crates.io/api/v1/crates/finter | jq -r '.crate.max_version // "0.0.0"')
echo "published_version=$PUBLISHED_VERSION" >> $GITHUB_OUTPUT
if [ "$CARGO_VERSION" = "$PUBLISHED_VERSION" ]; then
echo "Version $CARGO_VERSION already published, skipping"
echo "should_publish=false" >> $GITHUB_OUTPUT
else
echo "New version detected: $CARGO_VERSION (current: $PUBLISHED_VERSION)"
echo "should_publish=true" >> $GITHUB_OUTPUT
fi
- name: Publish to crates.io
if: steps.version_check.outputs.should_publish == 'true'
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --allow-dirty