name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published, edited]
  workflow_dispatch:

jobs:
  test:
    runs-on: depot-ubuntu-24.04-16

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_DB: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_HOST_AUTH_METHOD: trust
        options: >-
          --health-cmd pg_isready
          --health-interval 1s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

      - uses: Swatinem/rust-cache@v2

      - name: Install sqlx-cli and cargo-llvm-cov
        run: |
          cargo install sqlx-cli --no-default-features --features native-tls,postgres --locked
          cargo install cargo-llvm-cov --locked

      - name: Install just
        uses: extractions/setup-just@v3

      - name: Run CI pipeline
        env:
          DB_HOST: localhost
          DB_USER: postgres
          DB_PASS: postgres
        run: just ci

  publish:
    if: github.event_name == 'release' && !github.event.release.prerelease
    runs-on: depot-ubuntu-24.04
    needs: test

    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
