name: Release
on:
push:
tags:
- 'v*'
jobs:
checks:
name: Checks
uses: ./.github/workflows/ci-checks.yml
secrets: inherit
publish:
name: Publish to crates.io
runs-on: ubuntu-latest
needs: checks
permissions:
id-token: write contents: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Verify tag is on main branch
id: verify_main
run: |
git fetch origin main
if git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
echo "Tag commit is on main branch. Proceeding with publish."
echo "on_main=true" >> $GITHUB_OUTPUT
else
echo "Tag is NOT on the main branch. Skipping publish (not an error)."
echo "on_main=false" >> $GITHUB_OUTPUT
fi
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libpq-dev pkg-config libssl-dev
- name: Extract version from tag
id: get_version
run: |
TAG=${GITHUB_REF#refs/tags/}
VERSION=${TAG#v}
echo "version=${VERSION}" >> $GITHUB_OUTPUT
echo "tag=${TAG}" >> $GITHUB_OUTPUT
- name: Verify tag matches Cargo.toml version
run: |
CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/.*= "//' | sed 's/".*//')
TAG_VERSION="${{ steps.get_version.outputs.version }}"
echo "Cargo.toml version: $CARGO_VERSION"
echo "Git tag version: $TAG_VERSION"
if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
echo "Error: Version mismatch between Cargo.toml ($CARGO_VERSION) and git tag ($TAG_VERSION)"
exit 1
fi
- name: Authenticate to crates.io
id: auth
if: steps.verify_main.outputs.on_main == 'true'
uses: rust-lang/crates-io-auth-action@v1
- name: Dry run publish
if: steps.verify_main.outputs.on_main == 'true'
run: cargo publish --dry-run
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
- name: Publish to crates.io
if: steps.verify_main.outputs.on_main == 'true'
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
- name: Create GitHub Release
if: steps.verify_main.outputs.on_main == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create "${GITHUB_REF_NAME}" \
--title "Release ${GITHUB_REF_NAME}" \
--generate-notes