name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  BBDOX__PG__PORT: 8301

jobs:
  build-and-test:

    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        postgres-version: [ "12", "13", "14", "15", "latest" ]
    
    services:
      postgres:
        image: postgres:${{ matrix.postgres-version }}
        networks:
        - runner_overlay
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
        - "8301:5432"
    
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET 6
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: "6.0.x"
    - name: Setup .NET 7
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: "7.0.x"
    - name: Setup .NET 8
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: "8.0.x"
    - name: Restore dependencies
      run: dotnet restore src/BitBadger.Documents.sln
    - name: Build
      run: dotnet build src/BitBadger.Documents.sln --no-restore
    - name: Test .NET 6 against PostgreSQL ${{ matrix.postgres-version }}
      run: dotnet run --project src/Tests/BitBadger.Documents.Tests.fsproj -f net6.0
    - name: Test .NET 7 against PostgreSQL ${{ matrix.postgres-version }}
      run: dotnet run --project src/Tests/BitBadger.Documents.Tests.fsproj -f net7.0
    - name: Test .NET 8 against PostgreSQL ${{ matrix.postgres-version }}
      run: dotnet run --project src/Tests/BitBadger.Documents.Tests.fsproj -f net8.0
  publish:
    runs-on: ubuntu-latest
    needs: build-and-test
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: "8.0"
    - name: Package Common Library
      run: dotnet pack src/Common/BitBadger.Documents.Common.fsproj -c Release
    - name: Move Common package
      run: cp src/Common/bin/Release/BitBadger.Documents.Common.*.nupkg .
    - name: Package PostgreSQL Library
      run: dotnet pack src/Postgres/BitBadger.Documents.Postgres.fsproj -c Release
    - name: Move PostgreSQL package
      run: cp src/Postgres/bin/Release/BitBadger.Documents.Postgres.*.nupkg .
    - name: Package SQLite Library
      run: dotnet pack src/Sqlite/BitBadger.Documents.Sqlite.fsproj -c Release
    - name: Move SQLite package
      run: cp src/Sqlite/bin/Release/BitBadger.Documents.Sqlite.*.nupkg .
    - name: Save Packages
      uses: actions/upload-artifact@v3
      with:
        name: packages
        path: |
          *.nupkg