name: Release

on:
  push:
    tags:
      - "v*.*.*"
      - "v*.*.*-dryrun"

jobs:
  create-release:
    name: Create GitHub Release
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crate:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    needs: create-release
    steps:
      - uses: actions/checkout@v5
      - uses: dtolnay/rust-toolchain@stable
      - name: Cargo publish (dry run)
        if: ${{ endsWith(github.ref, '-dryrun') }}
        run: cargo publish --dry-run
      - name: Cargo publish
        if: ${{ !endsWith(github.ref, '-dryrun') }}
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
