on:
  push:
    tags:
      - '*'
  workflow_dispatch:

name: publish cargo

jobs:
  audit:
    name: Audit
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: rustsec/audit-check@v1.4.1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  build_and_test_linux:
    name: Build and Test (Linux)
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - uses: taiki-e/install-action@nextest
      - name: 'Build and test'
        run: cargo nextest run --workspace --all-targets --all-features


  build_and_test_macos:
    name: Build and Test (MacOS)
    runs-on: macos-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - uses: taiki-e/install-action@nextest
      - name: 'Build and test'
        run: cargo nextest run --workspace --all-targets --all-features

  crates_io_publish:
    name: Publish (crates.io)
    needs:
      - audit
      - build_and_test_linux
      - build_and_test_macos

    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - name: cargo-release Cache
        id: cargo_release_cache
        uses: actions/cache@v4
        with:
          path: ~/.cargo/bin/cargo-release
          key: ${{ runner.os }}-cargo-release

      - run: cargo install cargo-release
        if: steps.cargo_release_cache.outputs.cache-hit != 'true'

      - name: cargo login
        run: cargo login ${{ secrets.CARGO_API_TOKEN }}

      - name: "cargo release publish"
        run: |-
          cargo release \
            publish \
            --workspace \
            --all-features \
            --allow-branch main \
            --allow-branch HEAD \
            --no-confirm \
            --execute
