name: Release
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
validate:
name: Validate
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Extract version
id: version
run: |
VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Cargo.toml version: $VERSION"
- name: Validate tag matches version
run: |
TAG_VERSION="${GITHUB_REF_NAME#v}"
VERSION="${{ steps.version.outputs.version }}"
if [ "$TAG_VERSION" != "$VERSION" ]; then
echo "::error::Tag $GITHUB_REF_NAME does not match Cargo.toml version $VERSION"
exit 1
fi
echo "Tag $GITHUB_REF_NAME matches Cargo.toml version $VERSION"
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.toml') }}
restore-keys: ${{ runner.os }}-cargo-
- name: Check formatting
run: cargo fmt --all -- --check
- name: Run clippy (default)
run: cargo clippy --all-targets -- -D warnings
- name: Run clippy (all features)
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Run tests (default)
run: cargo test
- name: Run tests (all features)
run: cargo test --all-features
publish-crate:
name: Publish to crates.io
needs: validate
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- name: Check if version exists on crates.io
id: check
run: |
VERSION="${{ needs.validate.outputs.version }}"
PUBLISHED=$(cargo search datavalue-rs --limit 1 2>/dev/null | grep "^datavalue-rs" | sed 's/.*= "\(.*\)".*/\1/' || echo "")
if [ "$VERSION" = "$PUBLISHED" ]; then
echo "Version $VERSION already published to crates.io, skipping"
echo "skip=true" >> $GITHUB_OUTPUT
else
echo "Publishing version $VERSION to crates.io"
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Publish
if: steps.check.outputs.skip != 'true'
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}
github-release:
name: Create GitHub Release
needs: [validate, publish-crate]
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create release
run: |
VERSION="${{ needs.validate.outputs.version }}"
gh release create "v$VERSION" \
--title "v$VERSION" \
--generate-notes \
--verify-tag
env:
GH_TOKEN: ${{ github.token }}