name: Release

on: 
  push:
    tags:
    - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  archive:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15
        env:
          POSTGRES_DB: test_db
          POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
          --tmpfs /var/lib/postgresql/data
        ports:
          - 5432:5432
    env:
      POSTGRES_CONNECTION_STRING: "host=postgres user=postgres password=${{ secrets.POSTGRES_PASSWORD }} dbname=test_db"
    container:
      image: xd009642/tarpaulin:develop-nightly
      options: --security-opt seccomp=unconfined
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4
    - uses: ncipollo/release-action@v1
      with:
        bodyFile: "release.md"
    - name: Build
      run: cargo build --verbose
    - name: Run tests
      run: cargo test --verbose

  publish:
    needs: archive
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4
    - name: Publish
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
      run: |
        cargo login $CARGO_REGISTRY_TOKEN
        cargo publish
