name: CI
on:
push:
branches: [main]
tags: ['v*.*.*']
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
override: true
- name: Format
run: cargo fmt --all -- --check
- name: Build
run: cargo build --verbose
- name: Clippy
run: cargo clippy -- -D warnings
- name: Test
run: cargo test --verbose
publish:
if: github.ref == 'refs/heads/main'
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Install cargo-edit
run: cargo install cargo-edit
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Check current version
id: current_version
run: echo "VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')" >> $GITHUB_OUTPUT
- name: Check if version is already published
id: check_published
run: |
PACKAGE_NAME=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
CURRENT_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
echo "Checking if $PACKAGE_NAME version $CURRENT_VERSION is already published..."
if cargo search --limit 1 "$PACKAGE_NAME" | grep -q "\"$CURRENT_VERSION\""; then
echo "PUBLISHED=true" >> $GITHUB_OUTPUT
echo "Version $CURRENT_VERSION of $PACKAGE_NAME is already published"
else
echo "PUBLISHED=false" >> $GITHUB_OUTPUT
echo "Version $CURRENT_VERSION of $PACKAGE_NAME is not published yet"
fi
- name: Bump version
if: steps.check_published.outputs.PUBLISHED == 'true'
run: |
echo "Current version is already published, bumping to next patch version"
cargo set-version --bump patch
NEW_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
echo "New version: $NEW_VERSION"
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --no-verify --allow-dirty